Skip to content

Commit d615e2d

Browse files
authored
Fix panic on directories with only _test.go files (#256)
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.
1 parent fff195c commit d615e2d

5 files changed

Lines changed: 56 additions & 0 deletions

File tree

internal/index/scip.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ func indexVisitPackages(
206206
slog.Debug("Visiting package", "path", pkg.PkgPath)
207207
visitors.VisitPackageSyntax(opts.ModuleRoot, pkg, pathToDocuments, globalSymbols)
208208

209+
// A package may have no parsed source files (e.g. a directory
210+
// that only contains *_test.go files belonging to an external
211+
// "*_test" package). There is nothing to attach package symbol
212+
// information or occurrences to, so skip it.
213+
if len(pkg.Syntax) == 0 {
214+
atomic.AddUint64(&count, 1)
215+
continue
216+
}
217+
209218
pkgSymbol, _ := globalSymbols.GetPkgSymbol(pkg)
210219

211220
symInfo := &scip.SymbolInformation{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Directories with only `_test.go` files
2+
3+
Reproduction for issue
4+
[#255](https://github.com/scip-code/scip-go/issues/255).
5+
6+
A directory whose only Go file is a `*_test.go` file belonging to an
7+
external `*_test` package would cause `scip-go` to panic with
8+
`index out of range [0] with length 0` because the synthetic regular
9+
package returned by `packages.Load` had an empty `Syntax` slice.
10+
11+
The fix skips package-symbol attachment for packages with no parsed
12+
source files; the external `*_test` package still has its own non-empty
13+
entry and is indexed normally.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module sg/pr256
2+
3+
go 1.23
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package pr256_test
2+
3+
import "testing"
4+
5+
func TestComment(t *testing.T) {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package pr256_test
2+
// ^^^^^^^^^^ definition 0.1.test `sg/pr256_test`/
3+
// kind Package
4+
// display_name pr256_test
5+
// signature_documentation
6+
// > package pr256_test
7+
8+
import "testing"
9+
// ^^^^^^^ reference github.com/golang/go/src go1.22 testing/
10+
11+
//⌄ enclosing_range_start 0.1.test `sg/pr256_test`/TestComment().
12+
func TestComment(t *testing.T) {}
13+
// ^^^^^^^^^^^ definition 0.1.test `sg/pr256_test`/TestComment().
14+
// kind Function
15+
// display_name TestComment
16+
// signature_documentation
17+
// > func TestComment(t *testing.T)
18+
// ^ definition local 0
19+
// kind Variable
20+
// display_name t
21+
// signature_documentation
22+
// > var t *T
23+
// ^^^^^^^ reference github.com/golang/go/src go1.22 testing/
24+
// ^ reference github.com/golang/go/src go1.22 testing/T#
25+
// ⌃ enclosing_range_end 0.1.test `sg/pr256_test`/TestComment().
26+

0 commit comments

Comments
 (0)