Skip to content

Commit d464c7c

Browse files
committed
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 <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 719b1ba commit d464c7c

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/cmd/go/internal/modload/import.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,23 @@ func (e *ImportMissingError) ImportPath() string {
6262
// modules in the build list, or found in both the main module and its vendor
6363
// directory.
6464
type AmbiguousImportError struct {
65-
ImportPath string
65+
importPath string
6666
Dirs []string
6767
Modules []module.Version // Either empty or 1:1 with Dirs.
6868
}
6969

70+
func (e *AmbiguousImportError) ImportPath() string {
71+
return e.importPath
72+
}
73+
7074
func (e *AmbiguousImportError) Error() string {
7175
locType := "modules"
7276
if len(e.Modules) == 0 {
7377
locType = "directories"
7478
}
7579

7680
var buf strings.Builder
77-
fmt.Fprintf(&buf, "ambiguous import: found package %s in multiple %s:", e.ImportPath, locType)
81+
fmt.Fprintf(&buf, "ambiguous import: found package %s in multiple %s:", e.importPath, locType)
7882

7983
for i, dir := range e.Dirs {
8084
buf.WriteString("\n\t")
@@ -93,6 +97,8 @@ func (e *AmbiguousImportError) Error() string {
9397
return buf.String()
9498
}
9599

100+
var _ load.ImportPathError = &AmbiguousImportError{}
101+
96102
// Import finds the module and directory in the build list
97103
// containing the package with the given import path.
98104
// The answer must be unique: Import returns an error
@@ -136,7 +142,7 @@ func Import(path string) (m module.Version, dir string, err error) {
136142
mainDir, mainOK := dirInModule(path, targetPrefix, ModRoot(), true)
137143
vendorDir, vendorOK := dirInModule(path, "", filepath.Join(ModRoot(), "vendor"), false)
138144
if mainOK && vendorOK {
139-
return module.Version{}, "", &AmbiguousImportError{ImportPath: path, Dirs: []string{mainDir, vendorDir}}
145+
return module.Version{}, "", &AmbiguousImportError{importPath: path, Dirs: []string{mainDir, vendorDir}}
140146
}
141147
// Prefer to return main directory if there is one,
142148
// 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) {
176182
return mods[0], dirs[0], nil
177183
}
178184
if len(mods) > 0 {
179-
return module.Version{}, "", &AmbiguousImportError{ImportPath: path, Dirs: dirs, Modules: mods}
185+
return module.Version{}, "", &AmbiguousImportError{importPath: path, Dirs: dirs, Modules: mods}
180186
}
181187

182188
// Look up module containing the package, for addition to the build list.

src/cmd/go/testdata/script/mod_ambiguous_import.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ go build ./importy
1111

1212
# An import provided by both the main module and the vendor directory
1313
# should be flagged as an error only when -mod=vendor is set.
14-
# TODO: This error message is a bit redundant.
1514
mkdir vendor/example.com/m/importy
1615
cp $WORK/importy/importy.go vendor/example.com/m/importy/importy.go
1716
go build example.com/m/importy
1817
! go build -mod=vendor example.com/m/importy
19-
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$'
18+
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$'
19+
2020

2121
-- $WORK/go.mod --
2222
module example.com/m

0 commit comments

Comments
 (0)