Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/index/scip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
13 changes: 13 additions & 0 deletions internal/testdata/snapshots/input/pr256/README.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions internal/testdata/snapshots/input/pr256/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module sg/pr256

go 1.23
5 changes: 5 additions & 0 deletions internal/testdata/snapshots/input/pr256/pr256_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pr256_test

import "testing"

func TestComment(t *testing.T) {}
26 changes: 26 additions & 0 deletions internal/testdata/snapshots/output/pr256/pr256_test.go
Original file line number Diff line number Diff line change
@@ -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().

Loading