Skip to content

Commit

Permalink
gopls/internal/cache: id command-line-arguments packages using GoFiles
Browse files Browse the repository at this point in the history
Previously, we were using the first CompiledGoFiles to disambiguate the
ID of command-line-arguments packages, but in the presence of cgo
preprocessing there can actually be multiple CompiledGoFiles, leading
to the bug report of golang/go#64557. Fix this by using GoFiles instead.

Fixes golang/go#64557

Change-Id: I3eff976d07da32db1f26ced69228af41a388d9a1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/627776
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Findley <rfindley@google.com>
  • Loading branch information
findleyr authored and gopherbot committed Nov 14, 2024
1 parent 84e9c33 commit 221e94d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
20 changes: 11 additions & 9 deletions gopls/internal/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,21 +376,23 @@ func buildMetadata(updates map[PackageID]*metadata.Package, pkg *packages.Packag

if metadata.IsCommandLineArguments(id) {
var f string // file to use as disambiguating suffix
if len(pkg.CompiledGoFiles) > 0 {
f = pkg.CompiledGoFiles[0]

// If there are multiple files,
// we can't use only the first.
// (Can this happen? #64557)
if len(pkg.CompiledGoFiles) > 1 {
bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.CompiledGoFiles)
if len(pkg.GoFiles) > 0 {
f = pkg.GoFiles[0]

// If there are multiple files, we can't use only the first. Note that we
// consider GoFiles, rather than CompiledGoFiles, as there can be
// multiple CompiledGoFiles in the presence of cgo processing, whereas a
// command-line-arguments package should always have exactly one nominal
// Go source file. (See golang/go#64557.)
if len(pkg.GoFiles) > 1 {
bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.GoFiles)
return nil
}
} else if len(pkg.IgnoredFiles) > 0 {
// A file=empty.go query results in IgnoredFiles=[empty.go].
f = pkg.IgnoredFiles[0]
} else {
bug.Reportf("command-line-arguments package has neither CompiledGoFiles nor IgnoredFiles")
bug.Reportf("command-line-arguments package has neither GoFiles nor IgnoredFiles")
return nil
}
id = PackageID(pkg.ID + f)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
This test checks that we can load standalone files that use cgo.

-- go.mod --
module example.com

-- main.go --
//go:build ignore

package main

import (
"C"

"example.com/a"
)

func F() {} //@loc(F, "F")

func main() {
F() //@def("F", F)
println(a.A) //@def("A", A)
}

-- a/a.go --
package a

const A = 0 //@loc(A, "A")

0 comments on commit 221e94d

Please sign in to comment.