Skip to content

Commit 221e94d

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/cache: id command-line-arguments packages using GoFiles
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent 84e9c33 commit 221e94d

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

gopls/internal/cache/load.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -376,21 +376,23 @@ func buildMetadata(updates map[PackageID]*metadata.Package, pkg *packages.Packag
376376

377377
if metadata.IsCommandLineArguments(id) {
378378
var f string // file to use as disambiguating suffix
379-
if len(pkg.CompiledGoFiles) > 0 {
380-
f = pkg.CompiledGoFiles[0]
381-
382-
// If there are multiple files,
383-
// we can't use only the first.
384-
// (Can this happen? #64557)
385-
if len(pkg.CompiledGoFiles) > 1 {
386-
bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.CompiledGoFiles)
379+
if len(pkg.GoFiles) > 0 {
380+
f = pkg.GoFiles[0]
381+
382+
// If there are multiple files, we can't use only the first. Note that we
383+
// consider GoFiles, rather than CompiledGoFiles, as there can be
384+
// multiple CompiledGoFiles in the presence of cgo processing, whereas a
385+
// command-line-arguments package should always have exactly one nominal
386+
// Go source file. (See golang/go#64557.)
387+
if len(pkg.GoFiles) > 1 {
388+
bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.GoFiles)
387389
return nil
388390
}
389391
} else if len(pkg.IgnoredFiles) > 0 {
390392
// A file=empty.go query results in IgnoredFiles=[empty.go].
391393
f = pkg.IgnoredFiles[0]
392394
} else {
393-
bug.Reportf("command-line-arguments package has neither CompiledGoFiles nor IgnoredFiles")
395+
bug.Reportf("command-line-arguments package has neither GoFiles nor IgnoredFiles")
394396
return nil
395397
}
396398
id = PackageID(pkg.ID + f)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
This test checks that we can load standalone files that use cgo.
2+
3+
-- go.mod --
4+
module example.com
5+
6+
-- main.go --
7+
//go:build ignore
8+
9+
package main
10+
11+
import (
12+
"C"
13+
14+
"example.com/a"
15+
)
16+
17+
func F() {} //@loc(F, "F")
18+
19+
func main() {
20+
F() //@def("F", F)
21+
println(a.A) //@def("A", A)
22+
}
23+
24+
-- a/a.go --
25+
package a
26+
27+
const A = 0 //@loc(A, "A")

0 commit comments

Comments
 (0)