Skip to content

Commit adfa453

Browse files
findleyrgopherbot
authored andcommitted
[gopls-release-branch.0.15] gopls/internal/golang: fix crash in package references
Fix a crash found via telemetry, which may be encountered when finding references for a package where one of the package files lacks a valid name. Fixes golang/go#66250 Change-Id: Ifca9b6b7ab2ab8181b70e97d343fa2b072e866eb Reviewed-on: https://go-review.googlesource.com/c/tools/+/570597 Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent ba252e8 commit adfa453

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

gopls/internal/golang/references.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,14 @@ func packageReferences(ctx context.Context, snapshot *cache.Snapshot, uri protoc
197197
if err != nil {
198198
return nil, err
199199
}
200-
refs = append(refs, reference{
201-
isDeclaration: true, // (one of many)
202-
location: mustLocation(f, f.File.Name),
203-
pkgPath: widest.PkgPath,
204-
})
200+
// golang/go#66250: don't crash if the package file lacks a name.
201+
if f.File.Name.Pos().IsValid() {
202+
refs = append(refs, reference{
203+
isDeclaration: true, // (one of many)
204+
location: mustLocation(f, f.File.Name),
205+
pkgPath: widest.PkgPath,
206+
})
207+
}
205208
}
206209

207210
return refs, nil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
This bug checks the fix for golang/go#66250. Package references should not
2+
crash when one package file lacks a package name.
3+
4+
TODO(rfindley): the -ignore_extra_diags flag is only necessary because of
5+
problems matching diagnostics in the broken file, likely due to poor parser
6+
recovery.
7+
8+
-- flags --
9+
-ignore_extra_diags
10+
11+
-- a.go --
12+
package x //@refs("x", "x")
13+
14+
-- b.go --
15+
16+
func _() {
17+
}

0 commit comments

Comments
 (0)