From 9c4380303312ac3b550a4499aafccce037c6266e Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 2 Feb 2024 15:29:50 -0500 Subject: [PATCH] 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 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/cache/load.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/gopls/internal/cache/load.go b/gopls/internal/cache/load.go index aa9d9bd0d8f..4bbeb2d160a 100644 --- a/gopls/internal/cache/load.go +++ b/gopls/internal/cache/load.go @@ -16,8 +16,8 @@ import ( "time" "golang.org/x/tools/go/packages" - "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/cache/metadata" + "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/immutable" @@ -341,13 +341,26 @@ func buildMetadata(updates map[PackageID]*metadata.Package, pkg *packages.Packag id := PackageID(pkg.ID) if metadata.IsCommandLineArguments(id) { - if len(pkg.CompiledGoFiles) != 1 { - bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.CompiledGoFiles) + 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) + return + } + } 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: %#v", "") //*pkg.Metadata) return } - suffix := pkg.CompiledGoFiles[0] - id = PackageID(pkg.ID + suffix) - pkgPath = PackagePath(pkg.PkgPath + suffix) + id = PackageID(pkg.ID + f) + pkgPath = PackagePath(pkg.PkgPath + f) } // Duplicate?