Skip to content

Commit 0ad98f7

Browse files
cmd/go: do not allow main module to replace itself
Change-Id: I6d94f00e1c607581ba32253ba00fe4110e2bb5b8
1 parent bbd25d2 commit 0ad98f7

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/cmd/go/internal/modload/init.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,17 @@ func indexModFile(data []byte, modFile *modfile.File, needsFix bool) *modFileInd
10121012
if prev, dup := i.replace[r.Old]; dup && prev != r.New {
10131013
base.Fatalf("go: conflicting replacements for %v:\n\t%v\n\t%v", r.Old, prev, r.New)
10141014
}
1015+
1016+
if r.Old.Path == modFile.Module.Mod.Path && r.Old.Version == "" {
1017+
base.Fatalf("go: %s:%d: the main module (%s) cannot be replaced",
1018+
modFile.Syntax.Name, r.Syntax.Start.Line, modFile.Module.Mod.Path)
1019+
}
1020+
1021+
if modfile.IsDirectoryPath(r.New.Path) && HasModRoot() && filepath.Join(ModRoot(), r.New.Path) == ModRoot() {
1022+
base.Fatalf("go: %s:%d: the main module root directory (%s) cannot be a replacement for another module",
1023+
modFile.Syntax.Name, r.Syntax.Start.Line, modFile.Module.Mod.Path)
1024+
}
1025+
10151026
i.replace[r.Old] = r.New
10161027
}
10171028

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
env GO111MODULE=on
2+
[short] skip
3+
4+
cd fail1
5+
! go list all
6+
stderr 'go: \$WORK\/gopath\/src\/fail1\/go\.mod:3: the main module root directory \(example\.com\/m\) cannot be a replacement for another module'
7+
8+
cd ../fail2
9+
! go list all
10+
stderr 'go: \$WORK\/gopath\/src\/fail2\/go\.mod:3: the main module \(example\.com\/m\) cannot be replaced'
11+
12+
# Different module version can be replaced
13+
cd ../success
14+
go list all
15+
stdout 'example.com/m'
16+
17+
-- fail1/go.mod --
18+
module example.com/m
19+
20+
replace example.com/m2 => ./.
21+
22+
-- fail2/go.mod --
23+
module example.com/m
24+
25+
replace example.com/m => example.com/m v1.1.1
26+
27+
-- success/go.mod --
28+
module example.com/m
29+
30+
replace example.com/m v1.1.1 => example.com/m v1.1.2
31+
32+
-- fork/go.mod --
33+
module example.com/m
34+
35+
-- success/m.go --
36+
package main
37+
38+
func main() {}

0 commit comments

Comments
 (0)