Skip to content

Commit

Permalink
[gopls-release-branch.0.15] gopls/internal/golang: fix crash in packa…
Browse files Browse the repository at this point in the history
…ge 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 <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
findleyr authored and gopherbot committed Mar 11, 2024
1 parent ba252e8 commit adfa453
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
13 changes: 8 additions & 5 deletions gopls/internal/golang/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,14 @@ func packageReferences(ctx context.Context, snapshot *cache.Snapshot, uri protoc
if err != nil {
return nil, err
}
refs = append(refs, reference{
isDeclaration: true, // (one of many)
location: mustLocation(f, f.File.Name),
pkgPath: widest.PkgPath,
})
// golang/go#66250: don't crash if the package file lacks a name.
if f.File.Name.Pos().IsValid() {
refs = append(refs, reference{
isDeclaration: true, // (one of many)
location: mustLocation(f, f.File.Name),
pkgPath: widest.PkgPath,
})
}
}

return refs, nil
Expand Down
17 changes: 17 additions & 0 deletions gopls/internal/test/marker/testdata/fixedbugs/issue66250.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
This bug checks the fix for golang/go#66250. Package references should not
crash when one package file lacks a package name.

TODO(rfindley): the -ignore_extra_diags flag is only necessary because of
problems matching diagnostics in the broken file, likely due to poor parser
recovery.

-- flags --
-ignore_extra_diags

-- a.go --
package x //@refs("x", "x")

-- b.go --

func _() {
}

0 comments on commit adfa453

Please sign in to comment.