From cc54218193fd0f4023a9d3561d5860182b94163c Mon Sep 17 00:00:00 2001 From: jupblb Date: Fri, 15 May 2026 22:36:50 +0200 Subject: [PATCH 1/3] Fix panic on directories with only _test.go files When a directory contains only *_test.go files belonging to an external '*_test' package, packages.Load (with Tests: true) returns a synthetic regular package whose Syntax slice is empty. indexVisitPackages then panicked on pkg.Syntax[0] while attaching package SymbolInformation. Skip the package-symbol attachment when len(pkg.Syntax) == 0; the external *_test package has its own non-empty entry and is processed normally. Fixes #255 --- internal/index/scip.go | 9 +++++++++ 1 file changed, 9 insertions(+) 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{ From 53eba3dc78f0d07489168a0efe8bcb1f86623826 Mon Sep 17 00:00:00 2001 From: jupblb Date: Fri, 15 May 2026 22:45:57 +0200 Subject: [PATCH 2/3] Add pr256 snapshot test for #255 reproduction A directory containing only *_test.go files (belonging to an external *_test package) used to panic with 'index out of range [0] with length 0' in indexVisitPackages. This snapshot exercises that scenario and pins the resulting index for the package and its external test package. --- .../testdata/snapshots/input/pr256/README.md | 14 ++++++++++ .../testdata/snapshots/input/pr256/go.mod | 3 +++ .../testdata/snapshots/input/pr256/main.go | 3 +++ .../input/pr256/tests/comment/comment_test.go | 5 ++++ .../testdata/snapshots/output/pr256/main.go | 16 ++++++++++++ .../pr256/tests/comment/comment_test.go | 26 +++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 internal/testdata/snapshots/input/pr256/README.md create mode 100644 internal/testdata/snapshots/input/pr256/go.mod create mode 100644 internal/testdata/snapshots/input/pr256/main.go create mode 100644 internal/testdata/snapshots/input/pr256/tests/comment/comment_test.go create mode 100755 internal/testdata/snapshots/output/pr256/main.go create mode 100755 internal/testdata/snapshots/output/pr256/tests/comment/comment_test.go diff --git a/internal/testdata/snapshots/input/pr256/README.md b/internal/testdata/snapshots/input/pr256/README.md new file mode 100644 index 0000000..ea91120 --- /dev/null +++ b/internal/testdata/snapshots/input/pr256/README.md @@ -0,0 +1,14 @@ +# Directories with only `_test.go` files + +Reproduction for issue [#255]. + +A directory that only contains `*_test.go` files 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. + + [#255]: https://github.com/scip-code/scip-go/issues/255 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/main.go b/internal/testdata/snapshots/input/pr256/main.go new file mode 100644 index 0000000..38dd16d --- /dev/null +++ b/internal/testdata/snapshots/input/pr256/main.go @@ -0,0 +1,3 @@ +package main + +func main() {} diff --git a/internal/testdata/snapshots/input/pr256/tests/comment/comment_test.go b/internal/testdata/snapshots/input/pr256/tests/comment/comment_test.go new file mode 100644 index 0000000..e6d6617 --- /dev/null +++ b/internal/testdata/snapshots/input/pr256/tests/comment/comment_test.go @@ -0,0 +1,5 @@ +package comment_test + +import "testing" + +func TestComment(t *testing.T) {} diff --git a/internal/testdata/snapshots/output/pr256/main.go b/internal/testdata/snapshots/output/pr256/main.go new file mode 100755 index 0000000..e63b8b5 --- /dev/null +++ b/internal/testdata/snapshots/output/pr256/main.go @@ -0,0 +1,16 @@ + package main +// ^^^^ definition 0.1.test `sg/pr256`/ +// kind Package +// display_name main +// signature_documentation +// > package main + +//⌄ enclosing_range_start 0.1.test `sg/pr256`/main(). + func main() {} +// ^^^^ definition 0.1.test `sg/pr256`/main(). +// kind Function +// display_name main +// signature_documentation +// > func main() +// ⌃ enclosing_range_end 0.1.test `sg/pr256`/main(). + diff --git a/internal/testdata/snapshots/output/pr256/tests/comment/comment_test.go b/internal/testdata/snapshots/output/pr256/tests/comment/comment_test.go new file mode 100755 index 0000000..c46a9dc --- /dev/null +++ b/internal/testdata/snapshots/output/pr256/tests/comment/comment_test.go @@ -0,0 +1,26 @@ + package comment_test +// ^^^^^^^^^^^^ definition 0.1.test `sg/pr256/tests/comment_test`/ +// kind Package +// display_name comment_test +// signature_documentation +// > package comment_test + + import "testing" +// ^^^^^^^ reference github.com/golang/go/src go1.22 testing/ + +//⌄ enclosing_range_start 0.1.test `sg/pr256/tests/comment_test`/TestComment(). + func TestComment(t *testing.T) {} +// ^^^^^^^^^^^ definition 0.1.test `sg/pr256/tests/comment_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/tests/comment_test`/TestComment(). + From 0db62a87ecc27ef3086ee8879afb236d14d74af7 Mon Sep 17 00:00:00 2001 From: jupblb Date: Fri, 15 May 2026 22:48:56 +0200 Subject: [PATCH 3/3] Simplify pr256 snapshot to a single test file at the module root The bug only requires a directory whose Go files all belong to an external '*_test' package, leaving the synthetic regular package with empty Syntax. A single pr256_test.go at the module root is enough; the extra main.go and nested tests/comment/ subdirectory aren't needed. --- .../testdata/snapshots/input/pr256/README.md | 19 +++++++++---------- .../testdata/snapshots/input/pr256/main.go | 3 --- .../comment/comment_test.go => pr256_test.go} | 2 +- .../testdata/snapshots/output/pr256/main.go | 16 ---------------- .../comment/comment_test.go => pr256_test.go} | 18 +++++++++--------- 5 files changed, 19 insertions(+), 39 deletions(-) delete mode 100644 internal/testdata/snapshots/input/pr256/main.go rename internal/testdata/snapshots/input/pr256/{tests/comment/comment_test.go => pr256_test.go} (71%) delete mode 100755 internal/testdata/snapshots/output/pr256/main.go rename internal/testdata/snapshots/output/pr256/{tests/comment/comment_test.go => pr256_test.go} (59%) diff --git a/internal/testdata/snapshots/input/pr256/README.md b/internal/testdata/snapshots/input/pr256/README.md index ea91120..af3d537 100644 --- a/internal/testdata/snapshots/input/pr256/README.md +++ b/internal/testdata/snapshots/input/pr256/README.md @@ -1,14 +1,13 @@ # Directories with only `_test.go` files -Reproduction for issue [#255]. +Reproduction for issue +[#255](https://github.com/scip-code/scip-go/issues/255). -A directory that only contains `*_test.go` files 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. +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. - - [#255]: https://github.com/scip-code/scip-go/issues/255 +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/main.go b/internal/testdata/snapshots/input/pr256/main.go deleted file mode 100644 index 38dd16d..0000000 --- a/internal/testdata/snapshots/input/pr256/main.go +++ /dev/null @@ -1,3 +0,0 @@ -package main - -func main() {} diff --git a/internal/testdata/snapshots/input/pr256/tests/comment/comment_test.go b/internal/testdata/snapshots/input/pr256/pr256_test.go similarity index 71% rename from internal/testdata/snapshots/input/pr256/tests/comment/comment_test.go rename to internal/testdata/snapshots/input/pr256/pr256_test.go index e6d6617..e7403ff 100644 --- a/internal/testdata/snapshots/input/pr256/tests/comment/comment_test.go +++ b/internal/testdata/snapshots/input/pr256/pr256_test.go @@ -1,4 +1,4 @@ -package comment_test +package pr256_test import "testing" diff --git a/internal/testdata/snapshots/output/pr256/main.go b/internal/testdata/snapshots/output/pr256/main.go deleted file mode 100755 index e63b8b5..0000000 --- a/internal/testdata/snapshots/output/pr256/main.go +++ /dev/null @@ -1,16 +0,0 @@ - package main -// ^^^^ definition 0.1.test `sg/pr256`/ -// kind Package -// display_name main -// signature_documentation -// > package main - -//⌄ enclosing_range_start 0.1.test `sg/pr256`/main(). - func main() {} -// ^^^^ definition 0.1.test `sg/pr256`/main(). -// kind Function -// display_name main -// signature_documentation -// > func main() -// ⌃ enclosing_range_end 0.1.test `sg/pr256`/main(). - diff --git a/internal/testdata/snapshots/output/pr256/tests/comment/comment_test.go b/internal/testdata/snapshots/output/pr256/pr256_test.go similarity index 59% rename from internal/testdata/snapshots/output/pr256/tests/comment/comment_test.go rename to internal/testdata/snapshots/output/pr256/pr256_test.go index c46a9dc..0ee263d 100755 --- a/internal/testdata/snapshots/output/pr256/tests/comment/comment_test.go +++ b/internal/testdata/snapshots/output/pr256/pr256_test.go @@ -1,16 +1,16 @@ - package comment_test -// ^^^^^^^^^^^^ definition 0.1.test `sg/pr256/tests/comment_test`/ -// kind Package -// display_name comment_test -// signature_documentation -// > package comment_test + 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/tests/comment_test`/TestComment(). +//⌄ enclosing_range_start 0.1.test `sg/pr256_test`/TestComment(). func TestComment(t *testing.T) {} -// ^^^^^^^^^^^ definition 0.1.test `sg/pr256/tests/comment_test`/TestComment(). +// ^^^^^^^^^^^ definition 0.1.test `sg/pr256_test`/TestComment(). // kind Function // display_name TestComment // signature_documentation @@ -22,5 +22,5 @@ // > 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/tests/comment_test`/TestComment(). +// ⌃ enclosing_range_end 0.1.test `sg/pr256_test`/TestComment().