From d464c7ce29faa90ef2d35d5072c3e7c07606c525 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Thu, 27 Feb 2020 16:18:56 -0500 Subject: [PATCH] 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 Run-TryBot: Bryan C. Mills Reviewed-by: Bryan C. Mills TryBot-Result: Gobot Gobot --- src/cmd/go/internal/modload/import.go | 14 ++++++++++---- .../go/testdata/script/mod_ambiguous_import.txt | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/cmd/go/internal/modload/import.go b/src/cmd/go/internal/modload/import.go index 309d654987e433..3db3a266d5308a 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. diff --git a/src/cmd/go/testdata/script/mod_ambiguous_import.txt b/src/cmd/go/testdata/script/mod_ambiguous_import.txt index 61e632a29ccf54..4281faf799f010 100644 --- a/src/cmd/go/testdata/script/mod_ambiguous_import.txt +++ b/src/cmd/go/testdata/script/mod_ambiguous_import.txt @@ -11,12 +11,12 @@ go build ./importy # An import provided by both the main module and the vendor directory # should be flagged as an error only when -mod=vendor is set. -# TODO: This error message is a bit redundant. mkdir vendor/example.com/m/importy cp $WORK/importy/importy.go vendor/example.com/m/importy/importy.go go build example.com/m/importy ! go build -mod=vendor example.com/m/importy -stderr '^can.t load package: package example.com/m/importy: ambiguous import: found package example.com/m/importy in multiple directories:\n\t'$WORK'[/\\]importy\n\t'$WORK'[/\\]vendor[/\\]example.com[/\\]m[/\\]importy$' +stderr '^can.t load package: ambiguous import: found package example.com/m/importy in multiple directories:\n\t'$WORK'[/\\]importy\n\t'$WORK'[/\\]vendor[/\\]example.com[/\\]m[/\\]importy$' + -- $WORK/go.mod -- module example.com/m