Skip to content

Commit 56d52e6

Browse files
author
Jay Conrod
committed
cmd/go: don't report missing std import errors for tidy and vendor
'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 <[email protected]> Run-TryBot: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent b87e9b9 commit 56d52e6

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

src/cmd/go/internal/modcmd/tidy.go

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
6767
ResolveMissingImports: true,
6868
LoadTests: true,
6969
AllowErrors: tidyE,
70+
SilenceMissingStdImports: true,
7071
}, "all")
7172

7273
modload.TidyBuildList()

src/cmd/go/internal/modcmd/vendor.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
6969
ResolveMissingImports: true,
7070
UseVendorAll: true,
7171
AllowErrors: vendorE,
72+
SilenceMissingStdImports: true,
7273
}
7374
_, pkgs := modload.LoadPackages(ctx, loadOpts, "all")
7475

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ type PackageOpts struct {
175175
// that occur while loading packages. SilenceErrors implies AllowErrors.
176176
SilenceErrors bool
177177

178+
// SilenceMissingStdImports indicates that LoadPackages should not print
179+
// errors or terminate the process if an imported package is missing, and the
180+
// import path looks like it might be in the standard library (perhaps in a
181+
// future version).
182+
SilenceMissingStdImports bool
183+
178184
// SilenceUnmatchedWarnings suppresses the warnings normally emitted for
179185
// patterns that did not match any packages.
180186
SilenceUnmatchedWarnings bool
@@ -292,8 +298,13 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
292298
sumErr.importerIsTest = importer.testOf != nil
293299
}
294300
}
301+
silence := opts.SilenceErrors
302+
if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) &&
303+
stdErr.isStd && opts.SilenceMissingStdImports {
304+
silence = true
305+
}
295306

296-
if !opts.SilenceErrors {
307+
if !silence {
297308
if opts.AllowErrors {
298309
fmt.Fprintf(os.Stderr, "%s: %v\n", pkg.stackText(), pkg.err)
299310
} else {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ env GO111MODULE=on
44
# 'go mod tidy' and 'go mod vendor' should not hide loading errors.
55

66
! go mod tidy
7-
stderr '^issue27063 imports\n\tnonexist: package nonexist is not in GOROOT \(.*\)'
7+
! stderr 'package nonexist is not in GOROOT'
88
stderr '^issue27063 imports\n\tnonexist.example.com: cannot find module providing package nonexist.example.com'
99
stderr '^issue27063 imports\n\tissue27063/other imports\n\tother.example.com/nonexist: cannot find module providing package other.example.com/nonexist'
1010

1111
! go mod vendor
12-
stderr '^issue27063 imports\n\tnonexist: package nonexist is not in GOROOT \(.*\)'
12+
! stderr 'package nonexist is not in GOROOT'
1313
stderr '^issue27063 imports\n\tnonexist.example.com: cannot find module providing package nonexist.example.com'
1414
stderr '^issue27063 imports\n\tissue27063/other imports\n\tother.example.com/nonexist: cannot find module providing package other.example.com/nonexist'
1515

0 commit comments

Comments
 (0)