]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: make AmbiguousImportError an ImportPathError
authorMichael Matloob <matloob@golang.org>
Thu, 27 Feb 2020 21:18:56 +0000 (16:18 -0500)
committerMichael Matloob <matloob@golang.org>
Fri, 28 Feb 2020 17:51:13 +0000 (17:51 +0000)
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>

src/cmd/go/internal/modload/import.go
src/cmd/go/testdata/script/mod_ambiguous_import.txt

index 309d654987e433081d73ef2fa8fa3fa8ef8f4465..3db3a266d5308af16271cf194dd3d923c7c80b66 100644 (file)
@@ -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.
index 61e632a29ccf545cb615a592ad92b7363371d76a..4281faf799f0100caacf85689088694200463a0e 100644 (file)
@@ -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