]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: don't report missing std import errors for tidy and vendor
authorJay Conrod <jayconrod@google.com>
Thu, 4 Mar 2021 16:50:31 +0000 (11:50 -0500)
committerJay Conrod <jayconrod@google.com>
Thu, 4 Mar 2021 22:53:05 +0000 (22:53 +0000)
'go mod tidy' and 'go mod vendor' normally report errors when a
package can't be imported, even if the import appears in a file that
wouldn't be compiled by the current version of Go. These errors are
common for packages introduced in higher versions of Go, like "embed"
in 1.16.

This change causes 'go mod tidy' and 'go mod vendor' to ignore
missing package errors if the import path appears to come from the
standard library because it lacks a dot in the first path element.

Fixes #44557
Updates #27063

Change-Id: I61d6443e77ab95fd8c0d1514f57ef4c8885a77cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/298749
Trust: Jay Conrod <jayconrod@google.com>
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/go/internal/modcmd/tidy.go
src/cmd/go/internal/modcmd/vendor.go
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/mod_tidy_error.txt

index e7e63e6533e562d57cd0b73bdeed3e0dcc5e713d..34ff86ff18548d8a0ed35ab6816a67f39e8f6406 100644 (file)
@@ -67,6 +67,7 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
                ResolveMissingImports:    true,
                LoadTests:                true,
                AllowErrors:              tidyE,
+               SilenceMissingStdImports: true,
        }, "all")
 
        modload.TidyBuildList()
index 2cd683b75c96be30d1967a30ba8a2764acc4a7a0..6ebc18dcd84b4ab05b36f8311f45dfce6d663686 100644 (file)
@@ -69,6 +69,7 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
                ResolveMissingImports:    true,
                UseVendorAll:             true,
                AllowErrors:              vendorE,
+               SilenceMissingStdImports: true,
        }
        _, pkgs := modload.LoadPackages(ctx, loadOpts, "all")
 
index 0dba49e40ec8d1e92289902edf761d6e5dff45d5..2e62a7659fd02db635f54856e8ade801d43667a2 100644 (file)
@@ -175,6 +175,12 @@ type PackageOpts struct {
        // 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
@@ -292,8 +298,13 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
                                        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 {
index b6c24ceaf75f245436a0d457fe3aea702050e8e1..395537b1a7177917d95805970163b3c7ff12edd3 100644 (file)
@@ -4,12 +4,12 @@ env GO111MODULE=on
 # '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'