1
0
mirror of https://github.com/golang/go synced 2024-09-30 16:28:32 -06:00

go/packages: handle ad-hoc overlays with sources outside modules

If a user runs a file= query for a source that doesn't exist,
and they are outside of a module, fill in the GoSources for the
empty package that go list returns, so the overlay can be applied.

Also add a hack for the case where go list can't determine the compiler
(gc or gccgo) a user is using because
    go list -f "{{context.Compiler}}" -- unsafe
doesn't work. If go list complains that a user is outside a module,
we'll give ourselves flexibility to guess things because there's
no right answer. So we'll guess that the compiler is gc.

Fixes golang/go#33482

Change-Id: I6a8aa0c617c4d803558389fb4272854245f59c5a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/189322
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Michael Matloob 2019-08-07 15:31:54 -04:00
parent 128824a23e
commit a804cf87ae
5 changed files with 40 additions and 12 deletions

View File

@ -82,15 +82,28 @@ func GetSizesGolist(ctx context.Context, buildFlags, env []string, dir string, u
args = append(args, buildFlags...)
args = append(args, "--", "unsafe")
stdout, err := InvokeGo(ctx, env, dir, usesExportData, args...)
var goarch, compiler string
if err != nil {
return nil, err
if strings.Contains(err.Error(), "cannot find main module") {
// User's running outside of a module. All bets are off. Get GOARCH and guess compiler is gc.
// TODO(matloob): Is this a problem in practice?
envout, enverr := InvokeGo(ctx, env, dir, usesExportData, "env", "GOARCH")
if enverr != nil {
return nil, err
}
goarch = strings.TrimSpace(envout.String())
compiler = "gc"
} else {
return nil, err
}
} else {
fields := strings.Fields(stdout.String())
if len(fields) < 2 {
return nil, fmt.Errorf("could not determine GOARCH and Go compiler")
}
goarch = fields[0]
compiler = fields[1]
}
fields := strings.Fields(stdout.String())
if len(fields) < 2 {
return nil, fmt.Errorf("could not determine GOARCH and Go compiler")
}
goarch := fields[0]
compiler := fields[1]
return types.SizesFor(compiler, goarch), nil
}

View File

@ -258,6 +258,21 @@ func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, q
// Return the original error if the attempt to fall back failed.
return err
}
// Special case to handle issue #33482:
// If this is a file= query for ad-hoc packages where the file only exists on an overlay,
// and exists outside of a module, add the file in for the package.
if len(dirResponse.Packages) == 1 && len(dirResponse.Packages) == 1 &&
dirResponse.Packages[0].ID == "command-line-arguments" && len(dirResponse.Packages[0].GoFiles) == 0 {
filename := filepath.Join(pattern, filepath.Base(query)) // avoid recomputing abspath
// TODO(matloob): check if the file is outside of a root dir?
for path := range cfg.Overlay {
if path == filename {
dirResponse.Packages[0].Errors = nil
dirResponse.Packages[0].GoFiles = []string{path}
dirResponse.Packages[0].CompiledGoFiles = []string{path}
}
}
}
}
isRoot := make(map[string]bool, len(dirResponse.Roots))
for _, root := range dirResponse.Roots {

View File

@ -50,7 +50,7 @@ func processGolistOverlay(cfg *Config, response *responseDeduper, rootDirs func(
}
nextPackage:
for _, p := range response.dr.Packages {
if pkgName != p.Name {
if pkgName != p.Name && p.ID != "command-line-arguments" {
continue
}
for _, f := range p.GoFiles {

View File

@ -1011,9 +1011,6 @@ func testNewPackagesInOverlay(t *testing.T, exporter packagestest.Exporter) {
}
func TestAdHocOverlays(t *testing.T) {
// Enable this test when https://golang.org/issue/33482 is resolved.
t.Skip()
// This test doesn't use packagestest because we are testing ad-hoc packages,
// which are outside of $GOPATH and outside of a module.
tmp, err := ioutil.TempDir("", "a")
@ -1039,6 +1036,9 @@ const A = 1
}
// Check value of a.A.
a := initial[0]
if a.Errors != nil {
t.Fatalf("a: got errors %+v, want no error", err)
}
aA := constant(a, "A")
if aA == nil {
t.Errorf("a.A: got nil")
@ -1048,7 +1048,6 @@ const A = 1
if want := "1"; got != want {
t.Errorf("a.A: got %s, want %s", got, want)
}
}
func TestLoadAllSyntaxImportErrors(t *testing.T) {

View File

@ -6,3 +6,4 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190723021737-8bb11ff117ca h1:SqwJrz6xPBlCUltcEHz2/p01HRPR+VGD+aYLikk8uas=
golang.org/x/tools v0.0.0-20190723021737-8bb11ff117ca/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=