]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.15] cmd/go: don't report missing std import errors for tidy and...
authorJay Conrod <jayconrod@google.com>
Thu, 4 Mar 2021 16:50:31 +0000 (11:50 -0500)
committerAlexander Rakoczy <alex@golang.org>
Wed, 10 Mar 2021 21:25:28 +0000 (21:25 +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.

NOTE: This change is not a clean cherry-pick of CL 298749 because
parts of modload were substantially rewritten after 1.15.

Fixes #44792
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>
(cherry picked from commit 56d52e661114be60fb1893b034ac0c5976b622af)
Reviewed-on: https://go-review.googlesource.com/c/go/+/298950

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

index af2b04c0c20381ec72b0903df3d50b53c695f800..5ff847485cbedfa24621ad9265b6fa733f8c58ae 100644 (file)
@@ -42,6 +42,7 @@ func runTidy(cmd *base.Command, args []string) {
                base.Fatalf("go mod tidy: no arguments allowed")
        }
 
+       modload.SilenceMissingStdImports = true
        modload.LoadALL()
        modload.TidyBuildList()
        modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
index 8509ceb7a8b948fab8a83bc1a00415e4ae955c5b..5a5bb943a5837a72b614509e19b60dbfa298a0d3 100644 (file)
@@ -47,6 +47,7 @@ func runVendor(cmd *base.Command, args []string) {
        if len(args) != 0 {
                base.Fatalf("go mod vendor: vendor takes no arguments")
        }
+       modload.SilenceMissingStdImports = true
        pkgs := modload.LoadVendor()
 
        vdir := filepath.Join(modload.ModRoot(), "vendor")
index 664a2a15943abcae1cb6b389393fb58e087ac29d..2360feef045f8e89ef29830ced60d6665832d549 100644 (file)
@@ -56,6 +56,12 @@ var (
        CmdModModule string // module argument for 'go mod init'
 
        allowMissingModuleImports bool
+
+       // SilenceMissingStdImports indicates that LoadALL should not print an error
+       // 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
+       // Go version).
+       SilenceMissingStdImports bool
 )
 
 // ModFile returns the parsed go.mod file.
index 30992e0cc233efac2bbd8e7936fc6eaff3eb6dff..e5ea1a6c238b302bd4681967783d2336e563e48c 100644 (file)
@@ -433,6 +433,11 @@ func loadAll(testAll bool) []string {
        var paths []string
        for _, pkg := range loaded.pkgs {
                if pkg.err != nil {
+                       if impErr := (*ImportMissingError)(nil); SilenceMissingStdImports &&
+                               errors.As(pkg.err, &impErr) &&
+                               search.IsStandardImportPath(impErr.Path) {
+                               continue
+                       }
                        base.Errorf("%s: %v", pkg.stackText(), pkg.err)
                        continue
                }
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'