Fix nil-pointer panic when pkg.Fset.File returns nil#260
Merged
Conversation
When pkg.Fset.File(f.Package) returns nil (e.g. for files whose package position is not registered in the package's FileSet, as can happen when indexing directories with multiple conflicting 'package main' files such as the Go compiler's own test corpus), scip-go would panic with a nil pointer dereference at *token.File.Name(). Guard all five call sites of pkg.Fset.File(...).Name() in VisitPackageSyntax, ListMissing, Index, and indexVisitPackages, skipping syntax files with unknown positions instead of crashing.
…rywhere Replace the per-call-site nil guards added in the previous commit with a single filter in addImportsToPkgs that drops *ast.File entries whose f.Package has no corresponding *token.File in pkg.Fset. These are zero-decl placeholders produced by packages.Load for files it could not parse (e.g. conflicting 'package main' declarations in the Go compiler's test corpus). Restores VisitPackageSyntax, ListMissing, Index and indexVisitPackages to their original shape.
Minimal repro of the bug fixed in the previous commit: a directory containing a .go file with no 'package' declaration alongside a valid sibling file. Before the loader filter, indexing this directory panicked with a nil-pointer dereference at pkg.Fset.File(f.Package).Name(); afterwards the broken file is silently dropped and the valid file is indexed normally.
Pairs with no_package.go and makes the contrast between the indexable and the skipped file obvious from the filenames alone.
eseliger
approved these changes
May 20, 2026
Contributor
eseliger
left a comment
There was a problem hiding this comment.
nice test to visualize this problem, thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
packages.Loadreturns zero-decl*ast.Fileplaceholders for files it cannot parse, theirf.Packageistoken.NoPosandpkg.Fset.File(f.Package)is nil. Every call topkg.Fset.File(f.Package).Name()in the indexer then panics.Drop those entries once in
addImportsToPkgsso callers never see them.