src/cmd/go/internal/modcmd/tidy.go | 9 +++++---- src/cmd/go/internal/modcmd/vendor.go | 9 +++++---- src/cmd/go/internal/modload/load.go | 13 ++++++++++++- src/cmd/go/testdata/script/mod_tidy_error.txt | 4 ++-- diff --git a/src/cmd/go/internal/modcmd/tidy.go b/src/cmd/go/internal/modcmd/tidy.go index 3b83d87a8eb6fecf5693a49891c33fdf16bb672f..8bc9ed50bede7ffcc9069a4b2ff0a4d974a538b8 100644 --- a/src/cmd/go/internal/modcmd/tidy.go +++ b/src/cmd/go/internal/modcmd/tidy.go @@ -62,10 +62,11 @@ modload.ForceUseModules = true modload.RootMode = modload.NeedRoot modload.LoadPackages(ctx, modload.PackageOpts{ - Tags: imports.AnyTags(), - ResolveMissingImports: true, - LoadTests: true, - AllowErrors: tidyE, + Tags: imports.AnyTags(), + ResolveMissingImports: true, + LoadTests: true, + AllowErrors: tidyE, + SilenceMissingStdImports: true, }, "all") modload.TidyBuildList() diff --git a/src/cmd/go/internal/modcmd/vendor.go b/src/cmd/go/internal/modcmd/vendor.go index d3ed9e00e229a2021c4d7a4c3ea8a544d0ad654f..ac1fb7720aad4c86e86cfab8a9193fcad4bde4ed 100644 --- a/src/cmd/go/internal/modcmd/vendor.go +++ b/src/cmd/go/internal/modcmd/vendor.go @@ -64,10 +64,11 @@ modload.ForceUseModules = true modload.RootMode = modload.NeedRoot loadOpts := modload.PackageOpts{ - Tags: imports.AnyTags(), - ResolveMissingImports: true, - UseVendorAll: true, - AllowErrors: vendorE, + Tags: imports.AnyTags(), + ResolveMissingImports: true, + UseVendorAll: true, + AllowErrors: vendorE, + SilenceMissingStdImports: true, } _, pkgs := modload.LoadPackages(ctx, loadOpts, "all") diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index 6d87acc6d3b7c9371cdb62e08339b585dd149833..154fc3c6f0afc6bba814240e3504ef0a94d290ed 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -170,6 +170,12 @@ // SilenceErrors indicates that LoadPackages should not print errors // that occur while loading packages. SilenceErrors implies AllowErrors. SilenceErrors bool + // SilenceMissingStdImports indicates that LoadPackages should not print + // errors or terminate the process if an imported package is missing, and the + // import path looks like it might be in the standard library (perhaps in a + // future version). + SilenceMissingStdImports bool + // SilenceUnmatchedWarnings suppresses the warnings normally emitted for // patterns that did not match any packages. SilenceUnmatchedWarnings bool @@ -287,8 +293,13 @@ sumErr.importerVersion = importer.mod.Version sumErr.importerIsTest = importer.testOf != nil } } + silence := opts.SilenceErrors + if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) && + stdErr.isStd && opts.SilenceMissingStdImports { + silence = true + } - if !opts.SilenceErrors { + if !silence { if opts.AllowErrors { fmt.Fprintf(os.Stderr, "%s: %v\n", pkg.stackText(), pkg.err) } else { diff --git a/src/cmd/go/testdata/script/mod_tidy_error.txt b/src/cmd/go/testdata/script/mod_tidy_error.txt index b6c24ceaf75f245436a0d457fe3aea702050e8e1..395537b1a7177917d95805970163b3c7ff12edd3 100644 --- a/src/cmd/go/testdata/script/mod_tidy_error.txt +++ b/src/cmd/go/testdata/script/mod_tidy_error.txt @@ -4,12 +4,12 @@ # Regression test for golang.org/issue/27063: # 'go mod tidy' and 'go mod vendor' should not hide loading errors. ! go mod tidy -stderr '^issue27063 imports\n\tnonexist: package nonexist is not in GOROOT \(.*\)' +! stderr 'package nonexist is not in GOROOT' stderr '^issue27063 imports\n\tnonexist.example.com: cannot find module providing package nonexist.example.com' stderr '^issue27063 imports\n\tissue27063/other imports\n\tother.example.com/nonexist: cannot find module providing package other.example.com/nonexist' ! go mod vendor -stderr '^issue27063 imports\n\tnonexist: package nonexist is not in GOROOT \(.*\)' +! stderr 'package nonexist is not in GOROOT' stderr '^issue27063 imports\n\tnonexist.example.com: cannot find module providing package nonexist.example.com' stderr '^issue27063 imports\n\tissue27063/other imports\n\tother.example.com/nonexist: cannot find module providing package other.example.com/nonexist'