aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManlio Perillo <manlio.perillo@gmail.com>2021-05-12 12:09:18 +0200
committerBryan C. Mills <bcmills@google.com>2021-05-14 16:15:28 +0000
commit0eb38f2b164ec5b0094c5895cfe1b3a40c183d50 (patch)
tree89a970c00218c32faac94576977735341dc8ce0c
parenta938e529861215d9721f5e2590d5166bfbf2d271 (diff)
downloadgo-0eb38f2b164ec5b0094c5895cfe1b3a40c183d50.tar.gz
go-0eb38f2b164ec5b0094c5895cfe1b3a40c183d50.zip
cmd/go/internal/load: override Package.Root in module mode
The Context.ImportDir method in the go/build package sets Package.Root to $GOPATH, if a package is inside a GOPATH workspace. The loadPackageData function keeps this value even when modules are enabled. Override Package.Root when modules are enabled, instead of just set its value when Context.ImportDir was unable to set it. Add a regression test. Fixes #46119 Change-Id: I900a33fe13a445cb771e2952d0d830f1b4a5921f Reviewed-on: https://go-review.googlesource.com/c/go/+/319209 Reviewed-by: Bryan C. Mills <bcmills@google.com> Trust: Bryan C. Mills <bcmills@google.com> Trust: Jay Conrod <jayconrod@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org>
-rw-r--r--src/cmd/go/internal/load/pkg.go4
-rw-r--r--src/cmd/go/testdata/script/list_gomod_in_gopath.txt23
2 files changed, 26 insertions, 1 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index a3b96702ce..738904865e 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -849,7 +849,9 @@ func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoo
buildMode = build.ImportComment
}
data.p, data.err = cfg.BuildContext.ImportDir(r.dir, buildMode)
- if data.p.Root == "" && cfg.ModulesEnabled {
+ if cfg.ModulesEnabled {
+ // Override data.p.Root, since ImportDir sets it to $GOPATH, if
+ // the module is inside $GOPATH/src.
if info := modload.PackageModuleInfo(ctx, path); info != nil {
data.p.Root = info.Dir
}
diff --git a/src/cmd/go/testdata/script/list_gomod_in_gopath.txt b/src/cmd/go/testdata/script/list_gomod_in_gopath.txt
new file mode 100644
index 0000000000..064f33adc3
--- /dev/null
+++ b/src/cmd/go/testdata/script/list_gomod_in_gopath.txt
@@ -0,0 +1,23 @@
+# Issue 46119
+
+# When a module is inside a GOPATH workspace, Package.Root should be set to
+# Module.Dir instead of $GOPATH/src.
+
+env GOPATH=$WORK/tmp
+cd $WORK/tmp/src/test
+
+go list -f {{.Root}}
+stdout ^$PWD$
+
+# Were we really inside a GOPATH workspace?
+env GO111MODULE=off
+go list -f {{.Root}}
+stdout ^$WORK/tmp$
+
+-- $WORK/tmp/src/test/go.mod --
+module test
+
+-- $WORK/tmp/src/test/main.go --
+package main
+
+func main() {}