Skip to content

Commit 21b8e81

Browse files
cuiweixieromaindoumenc
authored andcommitted
cmd/go: show an error when a package in a module conflicts with one in std
Fixes golang#35270 Change-Id: I5d2a04359702be6dc04affb867540091b926bc23 Reviewed-on: https://go-review.googlesource.com/c/go/+/434095 Run-TryBot: xie cui <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent c05bf1e commit 21b8e81

File tree

5 files changed

+90
-19
lines changed

5 files changed

+90
-19
lines changed

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ func importFromModules(ctx context.Context, path string, rs *Requirements, mg *M
286286
return module.Version{}, "", "", nil, &invalidImportError{importPath: path, err: err}
287287
}
288288

289+
// Check each module on the build list.
290+
var dirs, roots []string
291+
var mods []module.Version
292+
289293
// Is the package in the standard library?
290294
pathIsStd := search.IsStandardImportPath(path)
291295
if pathIsStd && modindex.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, path) {
@@ -303,35 +307,43 @@ func importFromModules(ctx context.Context, path string, rs *Requirements, mg *M
303307
if str.HasPathPrefix(path, "cmd") {
304308
modroot = filepath.Join(cfg.GOROOTsrc, "cmd")
305309
}
306-
return module.Version{}, modroot, dir, nil, nil
310+
dirs = append(dirs, dir)
311+
roots = append(roots, modroot)
312+
mods = append(mods, module.Version{})
307313
}
308-
309314
// -mod=vendor is special.
310315
// Everything must be in the main module or the main module's vendor directory.
311316
if cfg.BuildMod == "vendor" {
312317
mainModule := MainModules.mustGetSingleMainModule()
313318
modRoot := MainModules.ModRoot(mainModule)
314319
mainDir, mainOK, mainErr := dirInModule(path, MainModules.PathPrefix(mainModule), modRoot, true)
320+
if mainOK {
321+
mods = append(mods, mainModule)
322+
dirs = append(dirs, mainDir)
323+
roots = append(roots, modRoot)
324+
}
315325
vendorDir, vendorOK, _ := dirInModule(path, "", filepath.Join(modRoot, "vendor"), false)
316-
if mainOK && vendorOK {
317-
return module.Version{}, modRoot, "", nil, &AmbiguousImportError{importPath: path, Dirs: []string{mainDir, vendorDir}}
326+
if vendorOK {
327+
readVendorList(mainModule)
328+
mods = append(mods, vendorPkgModule[path])
329+
dirs = append(dirs, vendorDir)
330+
roots = append(roots, modRoot)
318331
}
319-
// Prefer to return main directory if there is one,
320-
// Note that we're not checking that the package exists.
321-
// We'll leave that for load.
322-
if !vendorOK && mainDir != "" {
323-
return mainModule, modRoot, mainDir, nil, nil
332+
333+
if len(dirs) > 1 {
334+
return module.Version{}, modRoot, "", nil, &AmbiguousImportError{importPath: path, Dirs: dirs}
324335
}
336+
325337
if mainErr != nil {
326338
return module.Version{}, "", "", nil, mainErr
327339
}
328-
readVendorList(mainModule)
329-
return vendorPkgModule[path], modRoot, vendorDir, nil, nil
330-
}
331340

332-
// Check each module on the build list.
333-
var dirs, roots []string
334-
var mods []module.Version
341+
if len(dirs) == 0 {
342+
return module.Version{}, modRoot, "", nil, &ImportMissingError{Path: path}
343+
}
344+
345+
return mods[0], roots[0], dirs[0], nil, nil
346+
}
335347

336348
// Iterate over possible modules for the path, not all selected modules.
337349
// Iterating over selected modules would make the overall loading time

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ cmp go.mod go.mod.orig
2727

2828
! go list -mod=vendor all
2929
! stderr '^go: inconsistent vendoring'
30-
stderr 'cannot find package "." in:\n\t.*[/\\]vendor[/\\]example.com[/\\]badedit$'
30+
stderr 'go: finding module for package example.com/badedit'
31+
stderr 'cannot query module due to -mod=vendor'
3132

3233
# When we set -mod=mod, the go version should be updated immediately,
3334
# to the current version, converting the requirements from eager to lazy.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
cd a
3+
! go build
4+
stderr '^ambiguous import: found package image in multiple modules:\s+image\s+.+\s.+image.+\s$'
5+
6+
7+
cd ../b
8+
! go build -mod=vendor
9+
stderr '^main.go:4:5: ambiguous import: found package image in multiple directories:\s+.+image\s+.+image\s+$'
10+
11+
cd ../c
12+
! go build -mod=vendor
13+
stderr 'main.go:4:5: package p is not in GOROOT'
14+
15+
-- a/go.mod --
16+
module image
17+
18+
-- a/main.go --
19+
package main
20+
21+
func main() {
22+
println("hello world!")
23+
}
24+
25+
-- b/go.mod --
26+
module test
27+
28+
-- b/vendor/image/b.go --
29+
package image
30+
func Add(a, b int) int {
31+
return a + b
32+
}
33+
34+
-- b/main.go --
35+
package main
36+
37+
import (
38+
"image"
39+
)
40+
41+
func main() {
42+
println(image.Add(1,1))
43+
}
44+
45+
-- c/go.mod --
46+
module test
47+
48+
-- c/main.go --
49+
package main
50+
51+
import (
52+
"p"
53+
)
54+
55+
func main() {
56+
println(p.Add(1,1))
57+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ cd broken
2222
! go build -mod=readonly
2323
stderr 'disabled by -mod=readonly'
2424
! go build -mod=vendor
25-
stderr 'cannot find package'
26-
stderr 'hpack'
25+
stderr 'go: finding module for package golang.org/x/net/http2/hpack'
26+
stderr 'http.go:5:2: cannot query module due to -mod=vendor'
27+
2728

2829
# ...even if they explicitly use the "cmd/vendor/" or "vendor/" prefix.
2930
cd ../importcmd

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ stderr 'go: module diamondright: can''t resolve module using the vendor director
5555
go list -mod=mod -f {{.Dir}} w
5656
stdout 'src[\\/]w'
5757
! go list -mod=vendor -f {{.Dir}} w
58-
stderr 'src[\\/]vendor[\\/]w'
58+
stderr 'package w is not in GOROOT'
5959

6060
go list -mod=mod -f {{.Dir}} diamondright
6161
stdout 'src[\\/]diamondright'

0 commit comments

Comments
 (0)