From 4480df5f16277fac3c49dbd103521626b10718a5 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Thu, 2 Apr 2020 16:59:29 -0400 Subject: go/packages: only list current module's directory in determineRootDirs When determining the root directory of a module, the go list overlay code would do a go list -m all to get the module information for all modules. Stop doing that and only list the current module. This fixes an issue when users had overlays and were using automatic vendoring because go list -m all doesn't work with automatic vendoring. It isn't clear if overlays outside of the main module were ever working and overlays in the module cache don't make sense. There's a chance that this might break users with overlays in replaced directories, but it's not clear that that previously worked, and it wasn't tested. If this change causes breakages, we can use bcmills' suggestion in golang.org/issue/37629#issuecomment-594179751 point 3 to fix it. Fixes golang/go#37629 Change-Id: I826a86bbe54311cac72cbcafb2de4863dc805c2d Reviewed-on: https://go-review.googlesource.com/c/tools/+/227117 Run-TryBot: Michael Matloob TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- go/packages/packages_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'go/packages/packages_test.go') diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 95ce27124..259441bdf 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -2593,6 +2593,47 @@ func testForTestField(t *testing.T, exporter packagestest.Exporter) { } } +func TestIssue37529(t *testing.T) { + packagestest.TestAll(t, testIssue37529) +} +func testIssue37529(t *testing.T, exporter packagestest.Exporter) { + // Tests #37529. When automatic vendoring is triggered, and we try to determine + // the module root dir for a new overlay package, we previously would do a go list -m all, + // which is incompatible with automatic vendoring. + + exported := packagestest.Export(t, exporter, []packagestest.Module{{ + Name: "golang.org/fake", + Files: map[string]interface{}{ + "c/c2.go": `package c`, + "a/a.go": `package a; import "b.com/b"; const A = b.B`, + "vendor/b.com/b/b.go": `package b; const B = 4`, + }}}) + rootDir := filepath.Dir(filepath.Dir(exported.File("golang.org/fake", "a/a.go"))) + exported.Config.Overlay = map[string][]byte{ + filepath.Join(rootDir, "c/c.go"): []byte(`package c; import "golang.org/fake/a"; const C = a.A`), + } + exported.Config.Env = append(exported.Config.Env, "GOFLAGS=-mod=vendor") + exported.Config.Mode = packages.LoadAllSyntax + + defer exported.Cleanup() + + initial, err := packages.Load(exported.Config, "golang.org/fake/c") + if err != nil { + t.Fatal(err) + } + + // Check value of a.A. + a := initial[0] + aA := constant(a, "C") + if aA == nil { + t.Fatalf("a.A: got nil") + } + got := aA.Val().String() + if got != "4" { + t.Errorf("a.A: got %s, want %s", got, "4") + } +} + func errorMessages(errors []packages.Error) []string { var msgs []string for _, err := range errors { -- cgit v1.2.3