Skip to content

Commit 9c43803

Browse files
committed
gopls/internal/cache: allow command-line-package >1 CompileGoFiles
The previous assertion required exactly one, but this condition is violated by a file=emptyfile.go query, which produces no CompiledGoFiles and IgnoredFiles=[emptyfile.go]. So we relax the assertion, and use the first ignored file as the suffix. Fixes golang/go#64557 Change-Id: I097badd1b102bdc73af2ffa8a4871da1e359b173 Reviewed-on: https://go-review.googlesource.com/c/tools/+/560465 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent efce0f5 commit 9c43803

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

gopls/internal/cache/load.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
"time"
1717

1818
"golang.org/x/tools/go/packages"
19-
"golang.org/x/tools/gopls/internal/file"
2019
"golang.org/x/tools/gopls/internal/cache/metadata"
20+
"golang.org/x/tools/gopls/internal/file"
2121
"golang.org/x/tools/gopls/internal/protocol"
2222
"golang.org/x/tools/gopls/internal/util/bug"
2323
"golang.org/x/tools/gopls/internal/util/immutable"
@@ -341,13 +341,26 @@ func buildMetadata(updates map[PackageID]*metadata.Package, pkg *packages.Packag
341341
id := PackageID(pkg.ID)
342342

343343
if metadata.IsCommandLineArguments(id) {
344-
if len(pkg.CompiledGoFiles) != 1 {
345-
bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.CompiledGoFiles)
344+
var f string // file to use as disambiguating suffix
345+
if len(pkg.CompiledGoFiles) > 0 {
346+
f = pkg.CompiledGoFiles[0]
347+
348+
// If there are multiple files,
349+
// we can't use only the first.
350+
// (Can this happen? #64557)
351+
if len(pkg.CompiledGoFiles) > 1 {
352+
bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.CompiledGoFiles)
353+
return
354+
}
355+
} else if len(pkg.IgnoredFiles) > 0 {
356+
// A file=empty.go query results in IgnoredFiles=[empty.go].
357+
f = pkg.IgnoredFiles[0]
358+
} else {
359+
bug.Reportf("command-line-arguments package has neither CompiledGoFiles nor IgnoredFiles: %#v", "") //*pkg.Metadata)
346360
return
347361
}
348-
suffix := pkg.CompiledGoFiles[0]
349-
id = PackageID(pkg.ID + suffix)
350-
pkgPath = PackagePath(pkg.PkgPath + suffix)
362+
id = PackageID(pkg.ID + f)
363+
pkgPath = PackagePath(pkg.PkgPath + f)
351364
}
352365

353366
// Duplicate?

0 commit comments

Comments
 (0)