// 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 {
}
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")
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
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.
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.
# 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