]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: resolve std-vendored dependencies as std packages except in 'go get' and...
authorBryan C. Mills <bcmills@google.com>
Tue, 2 Mar 2021 14:52:55 +0000 (09:52 -0500)
committerBryan C. Mills <bcmills@google.com>
Tue, 2 Mar 2021 19:15:52 +0000 (19:15 +0000)
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 <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/modcmd/tidy.go
src/cmd/go/internal/modcmd/vendor.go
src/cmd/go/internal/modcmd/why.go
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/list_std_stale.txt [new file with mode: 0644]
src/cmd/go/testdata/script/mod_list_std.txt
src/cmd/go/testdata/script/mod_std_vendor.txt

index 3b83d87a8eb6fecf5693a49891c33fdf16bb672f..e7e63e6533e562d57cd0b73bdeed3e0dcc5e713d 100644 (file)
@@ -62,10 +62,11 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
        modload.RootMode = modload.NeedRoot
 
        modload.LoadPackages(ctx, modload.PackageOpts{
-               Tags:                  imports.AnyTags(),
-               ResolveMissingImports: true,
-               LoadTests:             true,
-               AllowErrors:           tidyE,
+               Tags:                     imports.AnyTags(),
+               VendorModulesInGOROOTSrc: true,
+               ResolveMissingImports:    true,
+               LoadTests:                true,
+               AllowErrors:              tidyE,
        }, "all")
 
        modload.TidyBuildList()
index d3ed9e00e229a2021c4d7a4c3ea8a544d0ad654f..2cd683b75c96be30d1967a30ba8a2764acc4a7a0 100644 (file)
@@ -64,10 +64,11 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
        modload.RootMode = modload.NeedRoot
 
        loadOpts := modload.PackageOpts{
-               Tags:                  imports.AnyTags(),
-               ResolveMissingImports: true,
-               UseVendorAll:          true,
-               AllowErrors:           vendorE,
+               Tags:                     imports.AnyTags(),
+               VendorModulesInGOROOTSrc: true,
+               ResolveMissingImports:    true,
+               UseVendorAll:             true,
+               AllowErrors:              vendorE,
        }
        _, pkgs := modload.LoadPackages(ctx, loadOpts, "all")
 
index a5f3e8afcbe3a52072e7265eecb7e117f9dabc78..79d257d198a37b508b6f0fa42264d89cee5b0971 100644 (file)
@@ -68,10 +68,11 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
        modload.RootMode = modload.NeedRoot
 
        loadOpts := modload.PackageOpts{
-               Tags:          imports.AnyTags(),
-               LoadTests:     !*whyVendor,
-               SilenceErrors: true,
-               UseVendorAll:  *whyVendor,
+               Tags:                     imports.AnyTags(),
+               VendorModulesInGOROOTSrc: true,
+               LoadTests:                !*whyVendor,
+               SilenceErrors:            true,
+               UseVendorAll:             *whyVendor,
        }
 
        if *whyM {
index 971c5a8d8a86d84a132011cd6d39d0127da1d8b1..b875a46d81ade850bb9beaa7774b4802622f8f9d 100644 (file)
@@ -1120,9 +1120,10 @@ func (r *resolver) findAndUpgradeImports(ctx context.Context, queries []*query)
 // build list.
 func (r *resolver) loadPackages(ctx context.Context, patterns []string, findPackage func(ctx context.Context, path string, m module.Version) (versionOk bool)) {
        opts := modload.PackageOpts{
-               Tags:          imports.AnyTags(),
-               LoadTests:     *getT,
-               SilenceErrors: true, // May be fixed by subsequent upgrades or downgrades.
+               Tags:                     imports.AnyTags(),
+               VendorModulesInGOROOTSrc: true,
+               LoadTests:                *getT,
+               SilenceErrors:            true, // May be fixed by subsequent upgrades or downgrades.
        }
 
        opts.AllowPackage = func(ctx context.Context, path string, m module.Version) error {
@@ -1459,9 +1460,10 @@ func (r *resolver) checkPackagesAndRetractions(ctx context.Context, pkgPatterns
                // LoadPackages will print errors (since it has more context) but will not
                // exit, since we need to load retractions later.
                pkgOpts := modload.PackageOpts{
-                       LoadTests:             *getT,
-                       ResolveMissingImports: false,
-                       AllowErrors:           true,
+                       VendorModulesInGOROOTSrc: true,
+                       LoadTests:                *getT,
+                       ResolveMissingImports:    false,
+                       AllowErrors:              true,
                }
                matches, pkgs := modload.LoadPackages(ctx, pkgOpts, pkgPatterns...)
                for _, m := range matches {
index 6d87acc6d3b7c9371cdb62e08339b585dd149833..0dba49e40ec8d1e92289902edf761d6e5dff45d5 100644 (file)
@@ -134,6 +134,11 @@ type PackageOpts struct {
        // If nil, treated as equivalent to imports.Tags().
        Tags map[string]bool
 
+       // VendorModulesInGOROOTSrc indicates that if we are within a module in
+       // GOROOT/src, packages in the module's vendor directory should be resolved as
+       // actual module dependencies (instead of standard-library packages).
+       VendorModulesInGOROOTSrc bool
+
        // ResolveMissingImports indicates that we should attempt to add module
        // dependencies as needed to resolve imports of packages that are not found.
        //
@@ -1170,13 +1175,13 @@ func (ld *loader) stdVendor(parentPath, path string) string {
        }
 
        if str.HasPathPrefix(parentPath, "cmd") {
-               if Target.Path != "cmd" {
+               if !ld.VendorModulesInGOROOTSrc || Target.Path != "cmd" {
                        vendorPath := pathpkg.Join("cmd", "vendor", path)
                        if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
                                return vendorPath
                        }
                }
-       } else if Target.Path != "std" || str.HasPathPrefix(parentPath, "vendor") {
+       } else if !ld.VendorModulesInGOROOTSrc || Target.Path != "std" || str.HasPathPrefix(parentPath, "vendor") {
                // If we are outside of the 'std' module, resolve imports from within 'std'
                // to the vendor directory.
                //
diff --git a/src/cmd/go/testdata/script/list_std_stale.txt b/src/cmd/go/testdata/script/list_std_stale.txt
new file mode 100644 (file)
index 0000000..e5c1f33
--- /dev/null
@@ -0,0 +1,31 @@
+# https://golang.org/issue/44725: packages in std should not be reported as stale,
+# regardless of whether they are listed from within or outside GOROOT/src.
+
+# Control case: net should not be stale at the start of the test,
+# and should depend on vendor/golang.org/… instead of golang.org/….
+
+! stale net
+
+go list -deps net
+stdout '^vendor/golang.org/x/net'
+! stdout '^golang.org/x/net'
+
+# Net should also not be stale when viewed from within GOROOT/src,
+# and should still report the same package dependencies.
+
+cd $GOROOT/src
+! stale net
+
+go list -deps net
+stdout '^vendor/golang.org/x/net'
+! stdout '^golang.org/x/net'
+
+
+# However, 'go mod' and 'go get' subcommands should report the original module
+# dependencies, not the vendored packages.
+
+[!net] stop
+
+env GOPROXY=
+go mod why -m golang.org/x/net
+stdout '^# golang.org/x/net\nnet\ngolang.org/x/net'
index baf7908ab93d2a946c5a8b4a1cb8c00ad816fdd8..f4e0433d8a0e171f585673c3664306a20fcc49b6 100644 (file)
@@ -48,18 +48,20 @@ stdout ^vendor/golang.org/x/crypto/internal/subtle
 ! stdout ^golang\.org/x
 
 # Within the std module, the dependencies of the non-vendored packages within
-# std should appear to come from modules, but they should be loaded from the
-# vendor directory (just like ordinary vendored module dependencies).
+# std should appear to be packages beginning with 'vendor/', not 'golang.org/…'
+# module dependencies.
 
 go list all
-stdout ^golang.org/x/
+stdout ^golang.org/x/
 ! stdout ^std/
 ! stdout ^cmd/
-stdout ^vendor/
+stdout ^vendor/
 
 go list -deps -f '{{if not .Standard}}{{.ImportPath}}{{end}}' std
-! stdout ^vendor/golang.org/x/net/http2/hpack
-stdout ^golang.org/x/net/http2/hpack
+! stdout .
+
+# However, the 'golang.org/…' module dependencies should resolve to those same
+# directories.
 
 go list -f '{{.Dir}}' golang.org/x/net/http2/hpack
 stdout $GOROOT[/\\]src[/\\]vendor
index fb954d74edbe9e76aa4f0a45e1eabf20a29d0adb..c3cde52953f86b023ecd1b8c854e3ec65dbf18ca 100644 (file)
@@ -36,11 +36,11 @@ stderr 'use of vendored package'
 
 
 # When run within the 'std' module, 'go list -test' should report vendored
-# transitive dependencies at their original module paths.
+# transitive dependencies at their vendored paths.
 cd $GOROOT/src
 go list -test -f '{{range .Deps}}{{.}}{{"\n"}}{{end}}' net/http
-stdout ^golang.org/x/net/http2/hpack
-stdout ^vendor/golang.org/x/net/http2/hpack
+stdout ^golang.org/x/net/http2/hpack
+stdout ^vendor/golang.org/x/net/http2/hpack
 
 -- go.mod --
 module m