Skip to content

Commit ba3dc70

Browse files
Jay Conrodtoothrot
Jay Conrod
authored andcommitted
[release-branch.go1.15] 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. 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 <[email protected]> Run-TryBot: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> (cherry picked from commit 56d52e6) Reviewed-on: https://go-review.googlesource.com/c/go/+/298950
1 parent ff7e638 commit ba3dc70

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func runTidy(cmd *base.Command, args []string) {
4242
base.Fatalf("go mod tidy: no arguments allowed")
4343
}
4444

45+
modload.SilenceMissingStdImports = true
4546
modload.LoadALL()
4647
modload.TidyBuildList()
4748
modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func runVendor(cmd *base.Command, args []string) {
4747
if len(args) != 0 {
4848
base.Fatalf("go mod vendor: vendor takes no arguments")
4949
}
50+
modload.SilenceMissingStdImports = true
5051
pkgs := modload.LoadVendor()
5152

5253
vdir := filepath.Join(modload.ModRoot(), "vendor")

src/cmd/go/internal/modload/init.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ var (
5656
CmdModModule string // module argument for 'go mod init'
5757

5858
allowMissingModuleImports bool
59+
60+
// SilenceMissingStdImports indicates that LoadALL should not print an error
61+
// or terminate the process if an imported package is missing, and the import
62+
// path looks like it might be in the standard library (perhaps in a future
63+
// Go version).
64+
SilenceMissingStdImports bool
5965
)
6066

6167
// ModFile returns the parsed go.mod file.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@ func loadAll(testAll bool) []string {
433433
var paths []string
434434
for _, pkg := range loaded.pkgs {
435435
if pkg.err != nil {
436+
if impErr := (*ImportMissingError)(nil); SilenceMissingStdImports &&
437+
errors.As(pkg.err, &impErr) &&
438+
search.IsStandardImportPath(impErr.Path) {
439+
continue
440+
}
436441
base.Errorf("%s: %v", pkg.stackText(), pkg.err)
437442
continue
438443
}

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

Lines changed: 2 additions & 2 deletions
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)