Skip to content

Commit

Permalink
cmd/go/internal/modload: make AmbiguousImportError an ImportPathError
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Bryan C. Mills <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
matloob committed Feb 28, 2020
1 parent 719b1ba commit d464c7c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/cmd/go/internal/modload/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,23 @@ 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 {
locType = "directories"
}

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")
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/testdata/script/mod_ambiguous_import.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d464c7c

Please sign in to comment.