Skip to content

Commit

Permalink
gopls/internal/cache: allow command-line-package >1 CompileGoFiles
Browse files Browse the repository at this point in the history
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 <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
adonovan committed Feb 2, 2024
1 parent efce0f5 commit 9c43803
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions gopls/internal/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 9c43803

Please sign in to comment.