Skip to content

Commit 26d27f9

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: remove (*loader).forceStdVendor
forceStdVendor was a special-case mechanism to allow Go contributors to use vendored dependencies by default when working in GOROOT/src. As of Go 1.14,¹ the 'go' command uses vendored dependencies by default within all modules, so the 'std' and 'cmd' modules no longer need to be special cases, and we can remove this special-case code. ¹ https://golang.org/doc/go1.14#vendor Updates #33848 Updates #30241 Change-Id: Ib2fb5841c253113b17fa86a086ce85a22ac3d121 Reviewed-on: https://go-review.googlesource.com/c/go/+/251159 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent b96d32b commit 26d27f9

File tree

3 files changed

+57
-38
lines changed

3 files changed

+57
-38
lines changed

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

+12-13
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,6 @@ func Lookup(parentPath string, parentIsStd bool, path string) (dir, realPath str
709709
type loader struct {
710710
loaderParams
711711

712-
forceStdVendor bool // if true, load standard-library dependencies from the vendor subtree
713-
714712
work *par.Queue
715713

716714
// reset on each iteration
@@ -850,13 +848,6 @@ func loadFromRoots(params loaderParams) *loader {
850848
work: par.NewQueue(runtime.GOMAXPROCS(0)),
851849
}
852850

853-
// Inside the "std" and "cmd" modules, we prefer to use the vendor directory
854-
// unless the command explicitly changes the module graph.
855-
// TODO(bcmills): Is this still needed now that we have automatic vendoring?
856-
if !targetInGorootSrc || (cfg.CmdName != "get" && !strings.HasPrefix(cfg.CmdName, "mod ")) {
857-
ld.forceStdVendor = true
858-
}
859-
860851
var err error
861852
reqs := Reqs()
862853
buildList, err = mvs.BuildList(Target, reqs)
@@ -1120,8 +1111,8 @@ func (ld *loader) load(pkg *loadPkg) {
11201111
}
11211112
for _, path := range imports {
11221113
if pkg.inStd {
1123-
// Imports from packages in "std" should resolve using GOROOT/src/vendor
1124-
// even when "std" is not the main module.
1114+
// Imports from packages in "std" and "cmd" should resolve using
1115+
// GOROOT/src/vendor even when "std" is not the main module.
11251116
path = ld.stdVendor(pkg.path, path)
11261117
}
11271118
pkg.imports = append(pkg.imports, ld.pkg(path, importFlags))
@@ -1185,13 +1176,21 @@ func (ld *loader) stdVendor(parentPath, path string) string {
11851176
}
11861177

11871178
if str.HasPathPrefix(parentPath, "cmd") {
1188-
if ld.forceStdVendor || Target.Path != "cmd" {
1179+
if Target.Path != "cmd" {
11891180
vendorPath := pathpkg.Join("cmd", "vendor", path)
11901181
if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
11911182
return vendorPath
11921183
}
11931184
}
1194-
} else if ld.forceStdVendor || Target.Path != "std" {
1185+
} else if Target.Path != "std" || str.HasPathPrefix(parentPath, "vendor") {
1186+
// If we are outside of the 'std' module, resolve imports from within 'std'
1187+
// to the vendor directory.
1188+
//
1189+
// Do the same for importers beginning with the prefix 'vendor/' even if we
1190+
// are *inside* of the 'std' module: the 'vendor/' packages that resolve
1191+
// globally from GOROOT/src/vendor (and are listed as part of 'go list std')
1192+
// are distinct from the real module dependencies, and cannot import internal
1193+
// packages from the real module.
11951194
vendorPath := pathpkg.Join("vendor", path)
11961195
if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
11971196
return vendorPath

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

+43-21
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ env GOPROXY=off
66

77
# Outside of GOROOT, our vendored packages should be reported as part of the standard library.
88
go list -f '{{if .Standard}}{{.ImportPath}}{{end}}' std cmd
9-
stdout ^vendor/golang.org/x/net/http2/hpack
9+
stdout ^vendor/golang\.org/x/net/http2/hpack
1010
stdout ^cmd/vendor/golang\.org/x/arch/x86/x86asm
11+
! stdout ^golang\.org/x/
12+
13+
# The dependencies of those packages should also be vendored.
14+
go list -deps vendor/golang.org/x/crypto/chacha20
15+
stdout ^vendor/golang\.org/x/crypto/internal/subtle
1116

1217
# cmd/... should match the same packages it used to match in GOPATH mode.
1318
go list cmd/...
@@ -23,40 +28,57 @@ stdout ^bytes$
2328
! stdout ^builtin$
2429
! stdout ^cmd/
2530
! stdout ^vendor/
31+
! stdout ^golang\.org/x/
32+
2633

34+
# Vendored dependencies should appear with their 'vendor/' paths in std (they're
35+
# in GOROOT/src, but not in the 'std' module following the usual module-boundary
36+
# rules).
2737

28-
# Within the std module, listing ./... should omit the 'std' prefix:
29-
# the package paths should be the same via ./... or the 'std' meta-pattern.
30-
# TODO(golang.org/issue/30241): Make that work.
31-
# Today, they are listed in 'std' but not './...'.
3238
cd $GOROOT/src
33-
go list ./...
34-
! stdout ^vendor/golang.org/x # TODO: should be included, or should be omitted from 'std'.
3539

36-
cp stdout $WORK/listdot.txt
3740
go list std
38-
stdout ^vendor/golang.org/x # TODO: remove vendor/ prefix
39-
# TODO: cmp stdout $WORK/listdot.txt
41+
stdout ^vendor/golang.org/x/net/http2/hpack
42+
! stdout ^golang\.org/x
43+
44+
# The dependencies of packages with an explicit 'vendor/' prefix should
45+
# still themselves resolve to vendored packages.
46+
go list -deps vendor/golang.org/x/crypto/chacha20
47+
stdout ^vendor/golang.org/x/crypto/internal/subtle
48+
! stdout ^golang\.org/x
49+
50+
# 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).
4053

4154
go list all
42-
stdout ^vendor/golang.org/x # TODO: remove vendor/ prefix.
55+
stdout ^golang.org/x/
4356
! stdout ^std/
57+
! stdout ^cmd/
58+
! stdout ^vendor/
4459

60+
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
4563

46-
# Within the std module, the vendored dependencies of std should appear
47-
# to come from the actual modules.
48-
# TODO(golang.org/issue/30241): Make that work.
49-
# Today, they still have the vendor/ prefix.
50-
go list std
51-
stdout ^vendor/golang.org/x/net/http2/hpack # TODO
52-
! stdout ^golang.org/x/net/http2/hpack # TODO
64+
go list -f '{{.Dir}}' golang.org/x/net/http2/hpack
65+
stdout $GOROOT[/\\]src[/\\]vendor
5366

54-
go list -deps -f '{{if not .Standard}}{{.ImportPath}}{{end}}' std
55-
# ! stdout ^vendor/golang.org/x/net/http2/hpack # TODO
56-
! stdout ^golang.org/x/net/http2/hpack # TODO
67+
# Within the std module, the packages within the module should omit the 'std/'
68+
# prefix (they retain their own identities), but should respect normal module
69+
# boundaries (vendored packages are not included in the module, even though they
70+
# are included in the 'std' pattern).
71+
72+
go list ./...
73+
stdout ^bytes$
74+
! stdout ^builtin$
75+
! stdout ^cmd/
76+
! stdout ^vendor/
77+
! stdout ^golang\.org/x/
5778

5879

5980
# Within std, the vendored dependencies of cmd should still appear to be part of cmd.
81+
6082
go list -f '{{if .Standard}}{{.ImportPath}}{{end}}' cmd
6183
stdout ^cmd/vendor/golang\.org/x/arch/x86/x86asm
6284

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ stderr 'use of vendored package'
3737

3838
# When run within the 'std' module, 'go list -test' should report vendored
3939
# transitive dependencies at their original module paths.
40-
# TODO(golang.org/issue/30241): Make that work.
41-
# Today, they're standard packages as long as they exist.
4240
cd $GOROOT/src
4341
go list -test -f '{{range .Deps}}{{.}}{{"\n"}}{{end}}' net/http
44-
stdout ^vendor/golang.org/x/net/http2/hpack # TODO: remove vendor/ prefix
45-
! stdout ^golang.org/x/net/http2/hpack
42+
stdout ^golang.org/x/net/http2/hpack
43+
! stdout ^vendor/golang.org/x/net/http2/hpack
4644

4745
-- go.mod --
4846
module m

0 commit comments

Comments
 (0)