Skip to content

Commit 5eb87c3

Browse files
committed
cmd/go: check case-insensitive path collisions for go mod vendor.
Fixes: #38571 Change-Id: Iec1cd1532ff17f7d943149f9b6a79e7fd419d179 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/570775 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent f83102c commit 5eb87c3

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/cmd/go/internal/load/pkg.go

+2
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,8 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
19371937
}
19381938

19391939
// Check for case-insensitive collisions of import paths.
1940+
// If modifying, consider changing checkPathCollisions() in
1941+
// src/cmd/go/internal/modcmd/vendor.go
19401942
fold := str.ToFold(p.ImportPath)
19411943
if other := foldPath[fold]; other == "" {
19421944
foldPath[fold] = p.ImportPath

src/cmd/go/internal/modcmd/vendor.go

+17
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
111111
}
112112
modpkgs[m] = append(modpkgs[m], pkg)
113113
}
114+
checkPathCollisions(modpkgs)
114115

115116
includeAllReplacements := false
116117
includeGoVersions := false
@@ -492,3 +493,19 @@ func copyDir(dst, src string, match func(dir string, info fs.DirEntry) bool, cop
492493
}
493494
}
494495
}
496+
497+
// checkPathCollisions will fail if case-insensitive collisions are present.
498+
// The reason why we do this check in go mod vendor is to keep consistentcy
499+
// with go build. If modifying, consider changing load() in
500+
// src/cmd/go/internal/load/pkg.go
501+
func checkPathCollisions(modpkgs map[module.Version][]string) {
502+
var foldPath = make(map[string]string, len(modpkgs))
503+
for m := range modpkgs {
504+
fold := str.ToFold(m.Path)
505+
if other := foldPath[fold]; other == "" {
506+
foldPath[fold] = m.Path
507+
} else if other != m.Path {
508+
base.Fatalf("go.mod: case-insensitive import collision: %q and %q", m.Path, other)
509+
}
510+
}
511+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
! go build
2+
stderr 'case-insensitive import collision'
3+
4+
! go mod vendor
5+
stderr 'case-insensitive import collision'
6+
7+
-- foo.go --
8+
package main
9+
10+
import (
11+
_ "example.com/Foo"
12+
_ "example.com/foo"
13+
)
14+
15+
func main() {}
16+
-- go.mod --
17+
module play.ground
18+
19+
go 1.14
20+
21+
require (
22+
example.com/foo v0.1.0
23+
example.com/Foo v0.1.0
24+
)
25+
26+
replace (
27+
example.com/foo => ./foo
28+
example.com/Foo => ./foo_alt
29+
)
30+
-- foo/go.mod --
31+
module example.com/foo
32+
-- foo/foo.go --
33+
package foo
34+
35+
-- foo_alt/go.mod --
36+
module example.com/Foo
37+
-- foo_alt/foo.go --
38+
package Foo

0 commit comments

Comments
 (0)