mirror of
https://github.com/golang/go
synced 2024-11-17 02:54:45 -07:00
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>
This commit is contained in:
parent
a938e52986
commit
0eb38f2b16
@ -849,7 +849,9 @@ func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoo
|
|||||||
buildMode = build.ImportComment
|
buildMode = build.ImportComment
|
||||||
}
|
}
|
||||||
data.p, data.err = cfg.BuildContext.ImportDir(r.dir, buildMode)
|
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 {
|
if info := modload.PackageModuleInfo(ctx, path); info != nil {
|
||||||
data.p.Root = info.Dir
|
data.p.Root = info.Dir
|
||||||
}
|
}
|
||||||
|
23
src/cmd/go/testdata/script/list_gomod_in_gopath.txt
vendored
Normal file
23
src/cmd/go/testdata/script/list_gomod_in_gopath.txt
vendored
Normal file
@ -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() {}
|
Loading…
Reference in New Issue
Block a user