From: Michael Matloob Date: Wed, 15 Jun 2022 18:24:12 +0000 (-0400) Subject: cmd/go: clear GOPATH from build context when importing from module X-Git-Tag: go1.20rc1~999 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=73c38f226e15e218411d87a05c878f087ad89455;p=gostls13.git cmd/go: clear GOPATH from build context when importing from module In module mode, we shouldn't handle packages under GOPATH any differently from other packages. Clear GOPATH from the build context before Importing to ensure that. Fixes #37015. Change-Id: I0203e25013716bca346fd4a67d80f1d05bbaea77 Reviewed-on: https://go-review.googlesource.com/c/go/+/412476 Reviewed-by: Bryan Mills Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Michael Matloob Run-TryBot: Bryan Mills --- diff --git a/doc/go1.20.html b/doc/go1.20.html index 71850129c7..abdc684d54 100644 --- a/doc/go1.20.html +++ b/doc/go1.20.html @@ -47,6 +47,17 @@ Do not send CLs removing the interior tags from such phrases. TODO: https://go.dev/issue/45454: provide build tags for architecture environment variables

+

+ When the main module is located within GOPATH/src, + go install no longer installs libraries for + non-main packages to GOPATH/pkg, + and go list no longer reports a Target + field for such packages. (In module mode, compiled packages are stored in the + build cache + only, but a bug had caused + the GOPATH install targets to unexpectedly remain in effect.) +

+

Vet

diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index a72b6202c8..b1e4549d71 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -873,8 +873,11 @@ func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoo var data packageData if r.dir != "" { var buildMode build.ImportMode + buildContext := cfg.BuildContext if !cfg.ModulesEnabled { buildMode = build.ImportComment + } else { + buildContext.GOPATH = "" // Clear GOPATH so packages are imported as pure module packages } modroot := modload.PackageModRoot(ctx, r.path) if modroot == "" && str.HasPathPrefix(r.dir, cfg.GOROOTsrc) { @@ -891,7 +894,7 @@ func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoo base.Fatalf("go: %v", err) } } - data.p, data.err = cfg.BuildContext.ImportDir(r.dir, buildMode) + data.p, data.err = buildContext.ImportDir(r.dir, buildMode) Happy: if cfg.ModulesEnabled { // Override data.p.Root, since ImportDir sets it to $GOPATH, if diff --git a/src/cmd/go/internal/modindex/read.go b/src/cmd/go/internal/modindex/read.go index 83a54c3ef8..da525ae446 100644 --- a/src/cmd/go/internal/modindex/read.go +++ b/src/cmd/go/internal/modindex/read.go @@ -399,11 +399,7 @@ func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *b // In build.go, p.Root should only be set in the non-local-import case, or in // GOROOT or GOPATH. Since module mode only calls Import with path set to "." // and the module index doesn't apply outside modules, the GOROOT case is - // the only case where GOROOT needs to be set. - // But: p.Root is actually set in the local-import case outside GOROOT, if - // the directory is contained in GOPATH/src - // TODO(#37015): fix that behavior in go/build and remove the gopath case - // below. + // the only case where p.Root needs to be set. if ctxt.GOROOT != "" && str.HasFilePathPrefix(p.Dir, cfg.GOROOTsrc) && p.Dir != cfg.GOROOTsrc { p.Root = ctxt.GOROOT p.Goroot = true @@ -412,47 +408,32 @@ func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *b if modprefix != "" { p.ImportPath = filepath.Join(modprefix, p.ImportPath) } - } - for _, root := range ctxt.gopath() { - // TODO(matloob): do we need to reimplement the conflictdir logic? - - // TODO(matloob): ctxt.hasSubdir evaluates symlinks, so it - // can be slower than we'd like. Find out if we can drop this - // logic before the release. - if sub, ok := ctxt.hasSubdir(filepath.Join(root, "src"), p.Dir); ok { - p.ImportPath = sub - p.Root = root + + // Set GOROOT-specific fields (sometimes for modules in a GOPATH directory). + // The fields set below (SrcRoot, PkgRoot, BinDir, PkgTargetRoot, and PkgObj) + // are only set in build.Import if p.Root != "". + var pkgtargetroot string + var pkga string + suffix := "" + if ctxt.InstallSuffix != "" { + suffix = "_" + ctxt.InstallSuffix + } + switch ctxt.Compiler { + case "gccgo": + pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix + dir, elem := path.Split(p.ImportPath) + pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a" + case "gc": + pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix + pkga = pkgtargetroot + "/" + p.ImportPath + ".a" + } + p.SrcRoot = ctxt.joinPath(p.Root, "src") + p.PkgRoot = ctxt.joinPath(p.Root, "pkg") + p.BinDir = ctxt.joinPath(p.Root, "bin") + if pkga != "" { + p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot) + p.PkgObj = ctxt.joinPath(p.Root, pkga) } - } - } - if p.Root != "" { - // Set GOROOT-specific fields (sometimes for modules in a GOPATH directory). - // The fields set below (SrcRoot, PkgRoot, BinDir, PkgTargetRoot, and PkgObj) - // are only set in build.Import if p.Root != "". As noted in the comment - // on setting p.Root above, p.Root should only be set in the GOROOT case for the - // set of packages we care about, but is also set for modules in a GOPATH src - // directory. - var pkgtargetroot string - var pkga string - suffix := "" - if ctxt.InstallSuffix != "" { - suffix = "_" + ctxt.InstallSuffix - } - switch ctxt.Compiler { - case "gccgo": - pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix - dir, elem := path.Split(p.ImportPath) - pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a" - case "gc": - pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix - pkga = pkgtargetroot + "/" + p.ImportPath + ".a" - } - p.SrcRoot = ctxt.joinPath(p.Root, "src") - p.PkgRoot = ctxt.joinPath(p.Root, "pkg") - p.BinDir = ctxt.joinPath(p.Root, "bin") - if pkga != "" { - p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot) - p.PkgObj = ctxt.joinPath(p.Root, pkga) } } diff --git a/src/cmd/go/testdata/script/mod_go_version_missing.txt b/src/cmd/go/testdata/script/mod_go_version_missing.txt index 2159a1e4c0..f4e0a96f3e 100644 --- a/src/cmd/go/testdata/script/mod_go_version_missing.txt +++ b/src/cmd/go/testdata/script/mod_go_version_missing.txt @@ -27,7 +27,7 @@ cmp go.mod go.mod.orig ! go list -mod=vendor all ! stderr '^go: inconsistent vendoring' -stderr 'cannot find package "vendor/example.com/badedit" in:\n\t.*[/\\]vendor[/\\]example.com[/\\]badedit$' +stderr 'cannot find package "." in:\n\t.*[/\\]vendor[/\\]example.com[/\\]badedit$' # When we set -mod=mod, the go version should be updated immediately, # to the current version, converting the requirements from eager to lazy.