]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/load: override Package.Root in module mode
authorManlio Perillo <manlio.perillo@gmail.com>
Wed, 12 May 2021 10:09:18 +0000 (12:09 +0200)
committerBryan C. Mills <bcmills@google.com>
Fri, 14 May 2021 16:15:28 +0000 (16:15 +0000)
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>

src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_gomod_in_gopath.txt [new file with mode: 0644]

index a3b96702ce52863c76fec052b8873f9c8b14bf2d..738904865e245d0aea2ba0805024a3d817c51ce9 100644 (file)
@@ -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 (file)
index 0000000..064f33a
--- /dev/null
@@ -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() {}