Skip to content

Commit d199cef

Browse files
author
Bryan C. Mills
committed
cmd/go: in workspace mode, resolve replacements relative to their go.mod files
Fixes #51204 Change-Id: I41106b7d04120be5ba68573bd25fd33e985688de Reviewed-on: https://go-review.googlesource.com/c/go/+/385994 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Michael Matloob <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 08ed488 commit d199cef

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,11 +1033,25 @@ func makeMainModules(ms []module.Version, rootDirs []string, modFiles []*modfile
10331033
for _, r := range modFiles[i].Replace {
10341034
if replacedByWorkFile[r.Old.Path] {
10351035
continue
1036-
} else if prev, ok := replacements[r.Old]; ok && !curModuleReplaces[r.Old] && prev != r.New {
1037-
base.Fatalf("go: conflicting replacements for %v:\n\t%v\n\t%v\nuse \"go work edit -replace %v=[override]\" to resolve", r.Old, prev, r.New, r.Old)
1036+
}
1037+
var newV module.Version = r.New
1038+
if WorkFilePath() != "" && newV.Version == "" && !filepath.IsAbs(newV.Path) {
1039+
// Since we are in a workspace, we may be loading replacements from
1040+
// multiple go.mod files. Relative paths in those replacement are
1041+
// relative to the go.mod file, not the workspace, so the same string
1042+
// may refer to two different paths and different strings may refer to
1043+
// the same path. Convert them all to be absolute instead.
1044+
//
1045+
// (We could do this outside of a workspace too, but it would mean that
1046+
// replacement paths in error strings needlessly differ from what's in
1047+
// the go.mod file.)
1048+
newV.Path = filepath.Join(rootDirs[i], newV.Path)
1049+
}
1050+
if prev, ok := replacements[r.Old]; ok && !curModuleReplaces[r.Old] && prev != newV {
1051+
base.Fatalf("go: conflicting replacements for %v:\n\t%v\n\t%v\nuse \"go work edit -replace %v=[override]\" to resolve", r.Old, prev, newV, r.Old)
10381052
}
10391053
curModuleReplaces[r.Old] = true
1040-
replacements[r.Old] = r.New
1054+
replacements[r.Old] = newV
10411055

10421056
v, ok := mainModules.highestReplaced[r.Old.Path]
10431057
if !ok || semver.Compare(r.Old.Version, v) > 0 {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
go work sync
2+
3+
go list -f '{{.Dir}}' example.com/test
4+
stdout '^'$PWD${/}test'$'
5+
6+
-- go.work --
7+
go 1.18
8+
9+
use (
10+
./test2
11+
./test2/sub
12+
)
13+
-- test/go.mod --
14+
module example.com/test
15+
16+
go 1.18
17+
-- test/file.go --
18+
package test
19+
20+
func DoSomething() {
21+
}
22+
-- test2/go.mod --
23+
module example.com/test2
24+
25+
go 1.18
26+
27+
replace example.com/test => ../test
28+
29+
require example.com/test v0.0.0-00010101000000-000000000000
30+
-- test2/file.go --
31+
package test2
32+
33+
import (
34+
"example.com/test"
35+
)
36+
37+
func DoSomething() {
38+
test.DoSomething()
39+
}
40+
-- test2/sub/go.mod --
41+
module example.com/test2/sub
42+
43+
go 1.18
44+
45+
replace example.com/test => ../../test
46+
47+
require example.com/test v0.0.0
48+
-- test2/sub/file.go --
49+
package test2
50+
51+
import (
52+
"example.com/test"
53+
)
54+
55+
func DoSomething() {
56+
test.DoSomething()
57+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# overriding it in the go.work file.
33

44
! go list -m example.com/dep
5-
stderr 'go: conflicting replacements for example.com/[email protected]:\n\t./dep1\n\t./dep2\nuse "go work edit -replace example.com/[email protected]=\[override\]" to resolve'
5+
stderr 'go: conflicting replacements for example.com/[email protected]:\n\t'$PWD${/}'dep1\n\t'$PWD${/}'dep2\nuse "go work edit -replace example.com/[email protected]=\[override\]" to resolve'
66
go work edit -replace example.com/[email protected]=./dep1
77
go list -m example.com/dep
88
stdout 'example.com/dep v1.0.0 => ./dep1'
@@ -15,7 +15,7 @@ use n
1515
module example.com/m
1616

1717
require example.com/dep v1.0.0
18-
replace example.com/dep v1.0.0 => ./dep1
18+
replace example.com/dep v1.0.0 => ../dep1
1919
-- m/m.go --
2020
package m
2121

@@ -28,7 +28,7 @@ func F() {
2828
module example.com/n
2929

3030
require example.com/dep v1.0.0
31-
replace example.com/dep v1.0.0 => ./dep2
31+
replace example.com/dep v1.0.0 => ../dep2
3232
-- n/n.go --
3333
package n
3434

0 commit comments

Comments
 (0)