aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/import.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2019-12-06 14:48:26 -0500
committerBryan C. Mills <bcmills@google.com>2019-12-06 20:31:18 +0000
commit1de31310d9f29f1ccf78f37eb9c7da3fb7867494 (patch)
tree171786edd238af123b8b0008e0e49c62a506f3e5 /src/cmd/go/internal/modload/import.go
parentbf865823baecefbf2996a9b56bdb8b73a387fbc9 (diff)
downloadgo-1de31310d9f29f1ccf78f37eb9c7da3fb7867494.tar.gz
go-1de31310d9f29f1ccf78f37eb9c7da3fb7867494.zip
cmd/go: avoid generating "malformed module path" errors for standard-library paths
If the path looks like it belongs in GOROOT/src and isn't there, we should mention that in the error message — instead of the fact that the path is not a valid module path, which the user likely already knows. Fixes #34769 Fixes #35734 Change-Id: I3589336d102e420a5ad3bf246816e29f3cbe6d71 Reviewed-on: https://go-review.googlesource.com/c/go/+/210339 Run-TryBot: Bryan C. Mills <bcmills@google.com> Reviewed-by: Jay Conrod <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/cmd/go/internal/modload/import.go')
-rw-r--r--src/cmd/go/internal/modload/import.go27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/cmd/go/internal/modload/import.go b/src/cmd/go/internal/modload/import.go
index dc0fc3c4d0..1899abbd8f 100644
--- a/src/cmd/go/internal/modload/import.go
+++ b/src/cmd/go/internal/modload/import.go
@@ -20,7 +20,6 @@ import (
"cmd/go/internal/modfetch"
"cmd/go/internal/par"
"cmd/go/internal/search"
- "cmd/go/internal/str"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
@@ -40,7 +39,7 @@ var _ load.ImportPathError = (*ImportMissingError)(nil)
func (e *ImportMissingError) Error() string {
if e.Module.Path == "" {
- if str.HasPathPrefix(e.Path, "cmd") {
+ if search.IsStandardImportPath(e.Path) {
return fmt.Sprintf("package %s is not in GOROOT (%s)", e.Path, filepath.Join(cfg.GOROOT, "src", e.Path))
}
if i := load.ImportPathError(nil); errors.As(e.QueryErr, &i) {
@@ -121,8 +120,8 @@ func Import(path string) (m module.Version, dir string, err error) {
}
// Is the package in the standard library?
- if search.IsStandardImportPath(path) &&
- goroot.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, path) {
+ pathIsStd := search.IsStandardImportPath(path)
+ if pathIsStd && goroot.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, path) {
if targetInGorootSrc {
if dir, ok := dirInModule(path, targetPrefix, ModRoot(), true); ok {
return Target, dir, nil
@@ -131,9 +130,6 @@ func Import(path string) (m module.Version, dir string, err error) {
dir := filepath.Join(cfg.GOROOT, "src", path)
return module.Version{}, dir, nil
}
- if str.HasPathPrefix(path, "cmd") {
- return module.Version{}, "", &ImportMissingError{Path: path}
- }
// -mod=vendor is special.
// Everything must be in the main module or the main module's vendor directory.
@@ -187,6 +183,12 @@ func Import(path string) (m module.Version, dir string, err error) {
// Look up module containing the package, for addition to the build list.
// Goal is to determine the module, download it to dir, and return m, dir, ErrMissing.
if cfg.BuildMod == "readonly" {
+ if pathIsStd {
+ // 'import lookup disabled' would be confusing for standard-library paths,
+ // since the user probably isn't expecting us to look up a module for
+ // those anyway.
+ return module.Version{}, "", &ImportMissingError{Path: path}
+ }
return module.Version{}, "", fmt.Errorf("import lookup disabled by -mod=%s", cfg.BuildMod)
}
if modRoot == "" && !allowMissingModuleImports {
@@ -253,6 +255,17 @@ func Import(path string) (m module.Version, dir string, err error) {
}
}
+ 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
+ // shimmed in via a "replace" directive.
+ // Moreover, the import path is reserved for the standard library, so
+ // QueryPackage cannot possibly find a module containing this package.
+ //
+ // Instead of trying QueryPackage, report an ImportMissingError immediately.
+ return module.Version{}, "", &ImportMissingError{Path: path}
+ }
+
candidates, err := QueryPackage(path, "latest", Allowed)
if err != nil {
if errors.Is(err, os.ErrNotExist) {