Skip to content

Fix nil-pointer panic when pkg.Fset.File returns nil#260

Merged
jupblb merged 4 commits into
mainfrom
michal/bug
May 20, 2026
Merged

Fix nil-pointer panic when pkg.Fset.File returns nil#260
jupblb merged 4 commits into
mainfrom
michal/bug

Conversation

@jupblb
Copy link
Copy Markdown
Collaborator

@jupblb jupblb commented May 20, 2026

When packages.Load returns zero-decl *ast.File placeholders for files it cannot parse, their f.Package is token.NoPos and pkg.Fset.File(f.Package) is nil. Every call to pkg.Fset.File(f.Package).Name() in the indexer then panics.

Drop those entries once in addImportsToPkgs so callers never see them.

jupblb added 4 commits May 20, 2026 13:27
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.
@jupblb jupblb requested review from emidoots, eseliger and trly May 20, 2026 11:47
@jupblb jupblb enabled auto-merge (squash) May 20, 2026 11:48
@jupblb jupblb disabled auto-merge May 20, 2026 11:49
Copy link
Copy Markdown
Contributor

@eseliger eseliger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test to visualize this problem, thanks!

@jupblb jupblb merged commit 8d0ab8f into main May 20, 2026
14 checks passed
@jupblb jupblb deleted the michal/bug branch May 20, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants