-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go/internal/modload: use a structured error for 'ambiguous import'
This consolidates the construction of 'ambiguous import' errors to a single location, ensuring consistency, and lays the groundwork for automatic resolution in the future. While we're at it, change "found" to "found package" to try to make the cause of the error clearer. Updates #32128 Updates #27899 Change-Id: I14a93593320e5c60d20b0eb686d0d5355763c30c Reviewed-on: https://go-review.googlesource.com/c/go/+/196298 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
- Loading branch information
Bryan C. Mills
committed
Sep 19, 2019
1 parent
83feeed
commit d6c2f1e
Showing
2 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
env GO111MODULE=on | ||
|
||
cd $WORK | ||
|
||
# An import provided by two different modules should be flagged as an error. | ||
! go build ./importx | ||
stderr '^importx[/\\]importx.go:2:8: ambiguous import: found package example.com/a/x in multiple modules:\n\texample.com/a v0.1.0 \('$WORK'[/\\]a[/\\]x\)\n\texample.com/a/x v0.1.0 \('$WORK'[/\\]ax\)$' | ||
|
||
# However, it should not be an error if that import is unused. | ||
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$' | ||
|
||
-- $WORK/go.mod -- | ||
module example.com/m | ||
go 1.14 | ||
require ( | ||
example.com/a v0.1.0 | ||
example.com/a/x v0.1.0 | ||
) | ||
replace ( | ||
example.com/a v0.1.0 => ./a | ||
example.com/a/x v0.1.0 => ./ax | ||
) | ||
-- $WORK/importx/importx.go -- | ||
package importx | ||
import _ "example.com/a/x" | ||
-- $WORK/importy/importy.go -- | ||
package importy | ||
import _ "example.com/a/y" | ||
-- $WORK/a/go.mod -- | ||
module example.com/a | ||
go 1.14 | ||
-- $WORK/a/x/x.go -- | ||
package x | ||
-- $WORK/a/y/y.go -- | ||
package y | ||
-- $WORK/ax/go.mod -- | ||
module example.com/a/x | ||
go 1.14 | ||
-- $WORK/ax/x.go -- | ||
package x |