diff options
author | Michael Matloob <matloob@golang.org> | 2020-02-27 16:18:56 -0500 |
---|---|---|
committer | Michael Matloob <matloob@golang.org> | 2020-02-28 17:51:13 +0000 |
commit | d464c7ce29faa90ef2d35d5072c3e7c07606c525 (patch) | |
tree | 726bbf6d485a6b7968834860aa6deb14cd5cedd1 /src/cmd/go/internal/modload/import.go | |
parent | 719b1ba27822e01cbef0d418d26a321a25948313 (diff) | |
download | go-d464c7ce29faa90ef2d35d5072c3e7c07606c525.tar.gz go-d464c7ce29faa90ef2d35d5072c3e7c07606c525.zip |
cmd/go/internal/modload: make AmbiguousImportError an ImportPathError
AmbiguousImportErrors will now be formatted like other ImportPathErrors:
this means that now the ambiguously imported package won't be printed
twice. Whereas the error message looked like the following:
can't load package: package example.com/m/importy: ambiguous import: found package example.com/m/importy in multiple directories:
$WORK/importy
$WORK/vendor/example.com/m/importy
It now looks like this:
can't load package: ambiguous import: found package example.com/m/importy in multiple directories:
$WORK/importy
$WORK/vendor/example.com/m/importy
Change-Id: I52a2074a6b3f5eb7d78d331d0852b7ea6b3735e6
Reviewed-on: https://go-review.googlesource.com/c/go/+/221457
Run-TryBot: Michael Matloob <matloob@golang.org>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Bryan C. Mills <bcmills@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.go | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/cmd/go/internal/modload/import.go b/src/cmd/go/internal/modload/import.go index 309d654987..3db3a266d5 100644 --- a/src/cmd/go/internal/modload/import.go +++ b/src/cmd/go/internal/modload/import.go @@ -62,11 +62,15 @@ func (e *ImportMissingError) ImportPath() string { // modules in the build list, or found in both the main module and its vendor // directory. type AmbiguousImportError struct { - ImportPath string + importPath string Dirs []string Modules []module.Version // Either empty or 1:1 with Dirs. } +func (e *AmbiguousImportError) ImportPath() string { + return e.importPath +} + func (e *AmbiguousImportError) Error() string { locType := "modules" if len(e.Modules) == 0 { @@ -74,7 +78,7 @@ func (e *AmbiguousImportError) Error() string { } var buf strings.Builder - fmt.Fprintf(&buf, "ambiguous import: found package %s in multiple %s:", e.ImportPath, locType) + fmt.Fprintf(&buf, "ambiguous import: found package %s in multiple %s:", e.importPath, locType) for i, dir := range e.Dirs { buf.WriteString("\n\t") @@ -93,6 +97,8 @@ func (e *AmbiguousImportError) Error() string { return buf.String() } +var _ load.ImportPathError = &AmbiguousImportError{} + // Import finds the module and directory in the build list // containing the package with the given import path. // The answer must be unique: Import returns an error @@ -136,7 +142,7 @@ func Import(path string) (m module.Version, dir string, err error) { mainDir, mainOK := dirInModule(path, targetPrefix, ModRoot(), true) vendorDir, vendorOK := dirInModule(path, "", filepath.Join(ModRoot(), "vendor"), false) if mainOK && vendorOK { - return module.Version{}, "", &AmbiguousImportError{ImportPath: path, Dirs: []string{mainDir, vendorDir}} + return module.Version{}, "", &AmbiguousImportError{importPath: path, Dirs: []string{mainDir, vendorDir}} } // Prefer to return main directory if there is one, // Note that we're not checking that the package exists. @@ -176,7 +182,7 @@ func Import(path string) (m module.Version, dir string, err error) { return mods[0], dirs[0], nil } if len(mods) > 0 { - return module.Version{}, "", &AmbiguousImportError{ImportPath: path, Dirs: dirs, Modules: mods} + return module.Version{}, "", &AmbiguousImportError{importPath: path, Dirs: dirs, Modules: mods} } // Look up module containing the package, for addition to the build list. |