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.