Skip to content

Commit e9eed78

Browse files
author
Bryan C. Mills
committed
cmd/go: resolve std-vendored dependencies as std packages except in 'go get' and 'go mod'
In CL 251159, I removed a hard-coded special case changing the rewriting behavior for std dependencies in GOROOT/src/vendor and GOROOT/src/cmd/vendor. Unfortunately, that caused packages in 'std' to be reported as stale when run within GOROOT/src. This change restores the special-case behavior, but plumbs it through the PackageOpts explicitly instead of comparing strings stored in global variables. Fixes #44725 Change-Id: If084fe74972ce1704715ca79b0b7e092dd90c88b Reviewed-on: https://go-review.googlesource.com/c/go/+/297869 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 09f4ef4 commit e9eed78

File tree

8 files changed

+72
-29
lines changed

8 files changed

+72
-29
lines changed

src/cmd/go/internal/modcmd/tidy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
6262
modload.RootMode = modload.NeedRoot
6363

6464
modload.LoadPackages(ctx, modload.PackageOpts{
65-
Tags: imports.AnyTags(),
66-
ResolveMissingImports: true,
67-
LoadTests: true,
68-
AllowErrors: tidyE,
65+
Tags: imports.AnyTags(),
66+
VendorModulesInGOROOTSrc: true,
67+
ResolveMissingImports: true,
68+
LoadTests: true,
69+
AllowErrors: tidyE,
6970
}, "all")
7071

7172
modload.TidyBuildList()

src/cmd/go/internal/modcmd/vendor.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
6464
modload.RootMode = modload.NeedRoot
6565

6666
loadOpts := modload.PackageOpts{
67-
Tags: imports.AnyTags(),
68-
ResolveMissingImports: true,
69-
UseVendorAll: true,
70-
AllowErrors: vendorE,
67+
Tags: imports.AnyTags(),
68+
VendorModulesInGOROOTSrc: true,
69+
ResolveMissingImports: true,
70+
UseVendorAll: true,
71+
AllowErrors: vendorE,
7172
}
7273
_, pkgs := modload.LoadPackages(ctx, loadOpts, "all")
7374

src/cmd/go/internal/modcmd/why.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
6868
modload.RootMode = modload.NeedRoot
6969

7070
loadOpts := modload.PackageOpts{
71-
Tags: imports.AnyTags(),
72-
LoadTests: !*whyVendor,
73-
SilenceErrors: true,
74-
UseVendorAll: *whyVendor,
71+
Tags: imports.AnyTags(),
72+
VendorModulesInGOROOTSrc: true,
73+
LoadTests: !*whyVendor,
74+
SilenceErrors: true,
75+
UseVendorAll: *whyVendor,
7576
}
7677

7778
if *whyM {

src/cmd/go/internal/modget/get.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,9 +1120,10 @@ func (r *resolver) findAndUpgradeImports(ctx context.Context, queries []*query)
11201120
// build list.
11211121
func (r *resolver) loadPackages(ctx context.Context, patterns []string, findPackage func(ctx context.Context, path string, m module.Version) (versionOk bool)) {
11221122
opts := modload.PackageOpts{
1123-
Tags: imports.AnyTags(),
1124-
LoadTests: *getT,
1125-
SilenceErrors: true, // May be fixed by subsequent upgrades or downgrades.
1123+
Tags: imports.AnyTags(),
1124+
VendorModulesInGOROOTSrc: true,
1125+
LoadTests: *getT,
1126+
SilenceErrors: true, // May be fixed by subsequent upgrades or downgrades.
11261127
}
11271128

11281129
opts.AllowPackage = func(ctx context.Context, path string, m module.Version) error {
@@ -1459,9 +1460,10 @@ func (r *resolver) checkPackagesAndRetractions(ctx context.Context, pkgPatterns
14591460
// LoadPackages will print errors (since it has more context) but will not
14601461
// exit, since we need to load retractions later.
14611462
pkgOpts := modload.PackageOpts{
1462-
LoadTests: *getT,
1463-
ResolveMissingImports: false,
1464-
AllowErrors: true,
1463+
VendorModulesInGOROOTSrc: true,
1464+
LoadTests: *getT,
1465+
ResolveMissingImports: false,
1466+
AllowErrors: true,
14651467
}
14661468
matches, pkgs := modload.LoadPackages(ctx, pkgOpts, pkgPatterns...)
14671469
for _, m := range matches {

src/cmd/go/internal/modload/load.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ type PackageOpts struct {
134134
// If nil, treated as equivalent to imports.Tags().
135135
Tags map[string]bool
136136

137+
// VendorModulesInGOROOTSrc indicates that if we are within a module in
138+
// GOROOT/src, packages in the module's vendor directory should be resolved as
139+
// actual module dependencies (instead of standard-library packages).
140+
VendorModulesInGOROOTSrc bool
141+
137142
// ResolveMissingImports indicates that we should attempt to add module
138143
// dependencies as needed to resolve imports of packages that are not found.
139144
//
@@ -1170,13 +1175,13 @@ func (ld *loader) stdVendor(parentPath, path string) string {
11701175
}
11711176

11721177
if str.HasPathPrefix(parentPath, "cmd") {
1173-
if Target.Path != "cmd" {
1178+
if !ld.VendorModulesInGOROOTSrc || Target.Path != "cmd" {
11741179
vendorPath := pathpkg.Join("cmd", "vendor", path)
11751180
if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
11761181
return vendorPath
11771182
}
11781183
}
1179-
} else if Target.Path != "std" || str.HasPathPrefix(parentPath, "vendor") {
1184+
} else if !ld.VendorModulesInGOROOTSrc || Target.Path != "std" || str.HasPathPrefix(parentPath, "vendor") {
11801185
// If we are outside of the 'std' module, resolve imports from within 'std'
11811186
// to the vendor directory.
11821187
//
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://golang.org/issue/44725: packages in std should not be reported as stale,
2+
# regardless of whether they are listed from within or outside GOROOT/src.
3+
4+
# Control case: net should not be stale at the start of the test,
5+
# and should depend on vendor/golang.org/… instead of golang.org/….
6+
7+
! stale net
8+
9+
go list -deps net
10+
stdout '^vendor/golang.org/x/net'
11+
! stdout '^golang.org/x/net'
12+
13+
# Net should also not be stale when viewed from within GOROOT/src,
14+
# and should still report the same package dependencies.
15+
16+
cd $GOROOT/src
17+
! stale net
18+
19+
go list -deps net
20+
stdout '^vendor/golang.org/x/net'
21+
! stdout '^golang.org/x/net'
22+
23+
24+
# However, 'go mod' and 'go get' subcommands should report the original module
25+
# dependencies, not the vendored packages.
26+
27+
[!net] stop
28+
29+
env GOPROXY=
30+
go mod why -m golang.org/x/net
31+
stdout '^# golang.org/x/net\nnet\ngolang.org/x/net'

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,20 @@ stdout ^vendor/golang.org/x/crypto/internal/subtle
4848
! stdout ^golang\.org/x
4949

5050
# Within the std module, the dependencies of the non-vendored packages within
51-
# std should appear to come from modules, but they should be loaded from the
52-
# vendor directory (just like ordinary vendored module dependencies).
51+
# std should appear to be packages beginning with 'vendor/', not 'golang.org/…'
52+
# module dependencies.
5353

5454
go list all
55-
stdout ^golang.org/x/
55+
! stdout ^golang.org/x/
5656
! stdout ^std/
5757
! stdout ^cmd/
58-
! stdout ^vendor/
58+
stdout ^vendor/
5959

6060
go list -deps -f '{{if not .Standard}}{{.ImportPath}}{{end}}' std
61-
! stdout ^vendor/golang.org/x/net/http2/hpack
62-
stdout ^golang.org/x/net/http2/hpack
61+
! stdout .
62+
63+
# However, the 'golang.org/…' module dependencies should resolve to those same
64+
# directories.
6365

6466
go list -f '{{.Dir}}' golang.org/x/net/http2/hpack
6567
stdout $GOROOT[/\\]src[/\\]vendor

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ stderr 'use of vendored package'
3636

3737

3838
# When run within the 'std' module, 'go list -test' should report vendored
39-
# transitive dependencies at their original module paths.
39+
# transitive dependencies at their vendored paths.
4040
cd $GOROOT/src
4141
go list -test -f '{{range .Deps}}{{.}}{{"\n"}}{{end}}' net/http
42-
stdout ^golang.org/x/net/http2/hpack
43-
! stdout ^vendor/golang.org/x/net/http2/hpack
42+
! stdout ^golang.org/x/net/http2/hpack
43+
stdout ^vendor/golang.org/x/net/http2/hpack
4444

4545
-- go.mod --
4646
module m

0 commit comments

Comments
 (0)