diff --git a/internal/index/scip.go b/internal/index/scip.go index 5c7b27c..af59a2d 100644 --- a/internal/index/scip.go +++ b/internal/index/scip.go @@ -206,6 +206,15 @@ func indexVisitPackages( slog.Debug("Visiting package", "path", pkg.PkgPath) visitors.VisitPackageSyntax(opts.ModuleRoot, pkg, pathToDocuments, globalSymbols) + // A package may have no parsed source files (e.g. a directory + // that only contains *_test.go files belonging to an external + // "*_test" package). There is nothing to attach package symbol + // information or occurrences to, so skip it. + if len(pkg.Syntax) == 0 { + atomic.AddUint64(&count, 1) + continue + } + pkgSymbol, _ := globalSymbols.GetPkgSymbol(pkg) symInfo := &scip.SymbolInformation{ diff --git a/internal/testdata/snapshots/input/pr256/README.md b/internal/testdata/snapshots/input/pr256/README.md new file mode 100644 index 0000000..af3d537 --- /dev/null +++ b/internal/testdata/snapshots/input/pr256/README.md @@ -0,0 +1,13 @@ +# Directories with only `_test.go` files + +Reproduction for issue +[#255](https://github.com/scip-code/scip-go/issues/255). + +A directory whose only Go file is a `*_test.go` file belonging to an +external `*_test` package would cause `scip-go` to panic with +`index out of range [0] with length 0` because the synthetic regular +package returned by `packages.Load` had an empty `Syntax` slice. + +The fix skips package-symbol attachment for packages with no parsed +source files; the external `*_test` package still has its own non-empty +entry and is indexed normally. diff --git a/internal/testdata/snapshots/input/pr256/go.mod b/internal/testdata/snapshots/input/pr256/go.mod new file mode 100644 index 0000000..d77189b --- /dev/null +++ b/internal/testdata/snapshots/input/pr256/go.mod @@ -0,0 +1,3 @@ +module sg/pr256 + +go 1.23 diff --git a/internal/testdata/snapshots/input/pr256/pr256_test.go b/internal/testdata/snapshots/input/pr256/pr256_test.go new file mode 100644 index 0000000..e7403ff --- /dev/null +++ b/internal/testdata/snapshots/input/pr256/pr256_test.go @@ -0,0 +1,5 @@ +package pr256_test + +import "testing" + +func TestComment(t *testing.T) {} diff --git a/internal/testdata/snapshots/output/pr256/pr256_test.go b/internal/testdata/snapshots/output/pr256/pr256_test.go new file mode 100755 index 0000000..0ee263d --- /dev/null +++ b/internal/testdata/snapshots/output/pr256/pr256_test.go @@ -0,0 +1,26 @@ + package pr256_test +// ^^^^^^^^^^ definition 0.1.test `sg/pr256_test`/ +// kind Package +// display_name pr256_test +// signature_documentation +// > package pr256_test + + import "testing" +// ^^^^^^^ reference github.com/golang/go/src go1.22 testing/ + +//⌄ enclosing_range_start 0.1.test `sg/pr256_test`/TestComment(). + func TestComment(t *testing.T) {} +// ^^^^^^^^^^^ definition 0.1.test `sg/pr256_test`/TestComment(). +// kind Function +// display_name TestComment +// signature_documentation +// > func TestComment(t *testing.T) +// ^ definition local 0 +// kind Variable +// display_name t +// signature_documentation +// > var t *T +// ^^^^^^^ reference github.com/golang/go/src go1.22 testing/ +// ^ reference github.com/golang/go/src go1.22 testing/T# +// ⌃ enclosing_range_end 0.1.test `sg/pr256_test`/TestComment(). +