Skip to content

Commit 73c38f2

Browse files
matloobgopherbot
authored andcommitted
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 <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent 0857633 commit 73c38f2

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

doc/go1.20.html

+11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ <h3 id="go-command">Go command</h3>
4747
TODO: <a href="https://go.dev/issue/45454">https://go.dev/issue/45454</a>: provide build tags for architecture environment variables
4848
</p>
4949

50+
<p><!-- https://go.dev/issue/37015 -->
51+
When the main module is located within <code>GOPATH/src</code>,
52+
<code>go</code> <code>install</code> no longer installs libraries for
53+
non-<code>main</code> packages to <code>GOPATH/pkg</code>,
54+
and <code>go</code> <code>list</code> no longer reports a <code>Target</code>
55+
field for such packages. (In module mode, compiled packages are stored in the
56+
<a href="https://pkg.go.dev/cmd/go#hdr-Build_and_test_caching">build cache</a>
57+
only, but <a href="https://go.dev/issue/37015">a bug</a> had caused
58+
the <code>GOPATH</code> install targets to unexpectedly remain in effect.)
59+
</p>
60+
5061
<h3 id="vet">Vet</h3>
5162

5263
<p><!-- https://go.dev/issue/48801, CL 354010 -->

src/cmd/go/internal/load/pkg.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,11 @@ func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoo
873873
var data packageData
874874
if r.dir != "" {
875875
var buildMode build.ImportMode
876+
buildContext := cfg.BuildContext
876877
if !cfg.ModulesEnabled {
877878
buildMode = build.ImportComment
879+
} else {
880+
buildContext.GOPATH = "" // Clear GOPATH so packages are imported as pure module packages
878881
}
879882
modroot := modload.PackageModRoot(ctx, r.path)
880883
if modroot == "" && str.HasPathPrefix(r.dir, cfg.GOROOTsrc) {
@@ -891,7 +894,7 @@ func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoo
891894
base.Fatalf("go: %v", err)
892895
}
893896
}
894-
data.p, data.err = cfg.BuildContext.ImportDir(r.dir, buildMode)
897+
data.p, data.err = buildContext.ImportDir(r.dir, buildMode)
895898
Happy:
896899
if cfg.ModulesEnabled {
897900
// Override data.p.Root, since ImportDir sets it to $GOPATH, if

src/cmd/go/internal/modindex/read.go

+26-45
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,7 @@ func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *b
399399
// In build.go, p.Root should only be set in the non-local-import case, or in
400400
// GOROOT or GOPATH. Since module mode only calls Import with path set to "."
401401
// and the module index doesn't apply outside modules, the GOROOT case is
402-
// the only case where GOROOT needs to be set.
403-
// But: p.Root is actually set in the local-import case outside GOROOT, if
404-
// the directory is contained in GOPATH/src
405-
// TODO(#37015): fix that behavior in go/build and remove the gopath case
406-
// below.
402+
// the only case where p.Root needs to be set.
407403
if ctxt.GOROOT != "" && str.HasFilePathPrefix(p.Dir, cfg.GOROOTsrc) && p.Dir != cfg.GOROOTsrc {
408404
p.Root = ctxt.GOROOT
409405
p.Goroot = true
@@ -412,47 +408,32 @@ func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *b
412408
if modprefix != "" {
413409
p.ImportPath = filepath.Join(modprefix, p.ImportPath)
414410
}
415-
}
416-
for _, root := range ctxt.gopath() {
417-
// TODO(matloob): do we need to reimplement the conflictdir logic?
418-
419-
// TODO(matloob): ctxt.hasSubdir evaluates symlinks, so it
420-
// can be slower than we'd like. Find out if we can drop this
421-
// logic before the release.
422-
if sub, ok := ctxt.hasSubdir(filepath.Join(root, "src"), p.Dir); ok {
423-
p.ImportPath = sub
424-
p.Root = root
411+
412+
// Set GOROOT-specific fields (sometimes for modules in a GOPATH directory).
413+
// The fields set below (SrcRoot, PkgRoot, BinDir, PkgTargetRoot, and PkgObj)
414+
// are only set in build.Import if p.Root != "".
415+
var pkgtargetroot string
416+
var pkga string
417+
suffix := ""
418+
if ctxt.InstallSuffix != "" {
419+
suffix = "_" + ctxt.InstallSuffix
420+
}
421+
switch ctxt.Compiler {
422+
case "gccgo":
423+
pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
424+
dir, elem := path.Split(p.ImportPath)
425+
pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a"
426+
case "gc":
427+
pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
428+
pkga = pkgtargetroot + "/" + p.ImportPath + ".a"
429+
}
430+
p.SrcRoot = ctxt.joinPath(p.Root, "src")
431+
p.PkgRoot = ctxt.joinPath(p.Root, "pkg")
432+
p.BinDir = ctxt.joinPath(p.Root, "bin")
433+
if pkga != "" {
434+
p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot)
435+
p.PkgObj = ctxt.joinPath(p.Root, pkga)
425436
}
426-
}
427-
}
428-
if p.Root != "" {
429-
// Set GOROOT-specific fields (sometimes for modules in a GOPATH directory).
430-
// The fields set below (SrcRoot, PkgRoot, BinDir, PkgTargetRoot, and PkgObj)
431-
// are only set in build.Import if p.Root != "". As noted in the comment
432-
// on setting p.Root above, p.Root should only be set in the GOROOT case for the
433-
// set of packages we care about, but is also set for modules in a GOPATH src
434-
// directory.
435-
var pkgtargetroot string
436-
var pkga string
437-
suffix := ""
438-
if ctxt.InstallSuffix != "" {
439-
suffix = "_" + ctxt.InstallSuffix
440-
}
441-
switch ctxt.Compiler {
442-
case "gccgo":
443-
pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
444-
dir, elem := path.Split(p.ImportPath)
445-
pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a"
446-
case "gc":
447-
pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
448-
pkga = pkgtargetroot + "/" + p.ImportPath + ".a"
449-
}
450-
p.SrcRoot = ctxt.joinPath(p.Root, "src")
451-
p.PkgRoot = ctxt.joinPath(p.Root, "pkg")
452-
p.BinDir = ctxt.joinPath(p.Root, "bin")
453-
if pkga != "" {
454-
p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot)
455-
p.PkgObj = ctxt.joinPath(p.Root, pkga)
456437
}
457438
}
458439

src/cmd/go/testdata/script/mod_go_version_missing.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cmp go.mod go.mod.orig
2727

2828
! go list -mod=vendor all
2929
! stderr '^go: inconsistent vendoring'
30-
stderr 'cannot find package "vendor/example.com/badedit" in:\n\t.*[/\\]vendor[/\\]example.com[/\\]badedit$'
30+
stderr 'cannot find package "." in:\n\t.*[/\\]vendor[/\\]example.com[/\\]badedit$'
3131

3232
# When we set -mod=mod, the go version should be updated immediately,
3333
# to the current version, converting the requirements from eager to lazy.

0 commit comments

Comments
 (0)