diff options
author | Bryan C. Mills <bcmills@google.com> | 2020-09-29 17:45:02 -0400 |
---|---|---|
committer | Bryan C. Mills <bcmills@google.com> | 2020-10-13 20:13:25 +0000 |
commit | 3a65abfbdac7ab29f693d69bd1eb12b2148a11ae (patch) | |
tree | 62ce700449d9e498e7dc6af9ef9e19a1ced9652f /src/cmd/go/internal/modload/import.go | |
parent | 076a45acd5b8d2ce08a2dbe898dc9228554db92d (diff) | |
download | go-3a65abfbdac7ab29f693d69bd1eb12b2148a11ae.tar.gz go-3a65abfbdac7ab29f693d69bd1eb12b2148a11ae.zip |
cmd/go: adjust ImportMissingError when module lookup is disabled
Previously, ImportMissingError said
"cannot find module providing package …"
even when we didn't even attempt to find such a module.
Now, we write "no module requirement provides package …"
when we did not attempt to identify a suitable module,
and suggest either 'go mod tidy' or 'go get -d' as appropriate.
Fixes #41576
Change-Id: I979bb999da4066828c54d99a310ea66bb31032ec
Reviewed-on: https://go-review.googlesource.com/c/go/+/258298
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Jay Conrod <jayconrod@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/import.go')
-rw-r--r-- | src/cmd/go/internal/modload/import.go | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/src/cmd/go/internal/modload/import.go b/src/cmd/go/internal/modload/import.go index 3642de851a..76fe6745d9 100644 --- a/src/cmd/go/internal/modload/import.go +++ b/src/cmd/go/internal/modload/import.go @@ -26,13 +26,15 @@ import ( "golang.org/x/mod/semver" ) -var errImportMissing = errors.New("import missing") - type ImportMissingError struct { Path string Module module.Version QueryErr error + // inAll indicates whether Path is in the "all" package pattern, + // and thus would be added by 'go mod tidy'. + inAll bool + // newMissingVersion is set to a newer version of Module if one is present // in the build list. When set, we can't automatically upgrade. newMissingVersion string @@ -46,7 +48,19 @@ func (e *ImportMissingError) Error() string { if e.QueryErr != nil { return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr) } - return "cannot find module providing package " + e.Path + if cfg.BuildMod == "mod" { + return "cannot find module providing package " + e.Path + } + + suggestion := "" + if !HasModRoot() { + suggestion = ": working directory is not part of a module" + } else if e.inAll { + suggestion = "; try 'go mod tidy' to add it" + } else { + suggestion = fmt.Sprintf("; try 'go get -d %s' to add it", e.Path) + } + return fmt.Sprintf("no required module provides package %s%s", e.Path, suggestion) } if e.newMissingVersion != "" { @@ -132,7 +146,7 @@ func (e *invalidImportError) Unwrap() error { // like "C" and "unsafe". // // If the package cannot be found in the current build list, -// importFromBuildList returns errImportMissing as the error. +// importFromBuildList returns an *ImportMissingError. func importFromBuildList(ctx context.Context, path string) (m module.Version, dir string, err error) { if strings.Contains(path, "@") { return module.Version{}, "", fmt.Errorf("import path should not have @version") @@ -144,6 +158,10 @@ func importFromBuildList(ctx context.Context, path string) (m module.Version, di // There's no directory for import "C" or import "unsafe". return module.Version{}, "", nil } + // Before any further lookup, check that the path is valid. + if err := module.CheckImportPath(path); err != nil { + return module.Version{}, "", &invalidImportError{importPath: path, err: err} + } // Is the package in the standard library? pathIsStd := search.IsStandardImportPath(path) @@ -212,7 +230,7 @@ func importFromBuildList(ctx context.Context, path string) (m module.Version, di return module.Version{}, "", &AmbiguousImportError{importPath: path, Dirs: dirs, Modules: mods} } - return module.Version{}, "", errImportMissing + return module.Version{}, "", &ImportMissingError{Path: path} } // queryImport attempts to locate a module that can be added to the current @@ -220,13 +238,6 @@ func importFromBuildList(ctx context.Context, path string) (m module.Version, di func queryImport(ctx context.Context, path string) (module.Version, error) { pathIsStd := search.IsStandardImportPath(path) - if modRoot == "" && !allowMissingModuleImports { - return module.Version{}, &ImportMissingError{ - Path: path, - QueryErr: errors.New("working directory is not part of a module"), - } - } - // Not on build list. // To avoid spurious remote fetches, next try the latest replacement for each // module (golang.org/issue/26241). This should give a useful message @@ -291,11 +302,6 @@ func queryImport(ctx context.Context, path string) (module.Version, error) { } } - // Before any further lookup, check that the path is valid. - if err := module.CheckImportPath(path); err != nil { - return module.Version{}, &invalidImportError{importPath: path, err: err} - } - if pathIsStd { // This package isn't in the standard library, isn't in any module already // in the build list, and isn't in any other module that the user has |