And disallow replaces of any main modules in the go.work file itself.
Change-Id: Ifa9ba9eaed047e6a75fcde230d96c7c450c1a1ad
Reviewed-on: https://go-review.googlesource.com/c/go/+/379534
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
workFileReplaceMap: toReplaceMap(workFileReplaces),
highestReplaced: map[string]string{},
}
+ mainModulePaths := make(map[string]bool)
+ for _, m := range ms {
+ mainModulePaths[m.Path] = true
+ }
replacedByWorkFile := make(map[string]bool)
replacements := make(map[module.Version]module.Version)
for _, r := range workFileReplaces {
+ if mainModulePaths[r.Old.Path] && r.Old.Version == "" {
+ base.Errorf("go: workspace module %v is replaced at all versions in the go.work file. To fix, remove the replacement from the go.work file or specify the version at which to replace the module.", r.Old.Path)
+ }
replacedByWorkFile[r.Old.Path] = true
v, ok := mainModules.highestReplaced[r.Old.Path]
if !ok || semver.Compare(r.Old.Version, v) > 0 {
foundFrom, found, foundModRoot := "", module.Version{}, ""
if MainModules == nil {
return module.Version{}
+ } else if MainModules.Contains(mod.Path) && mod.Version == "" {
+ // Don't replace the workspace version of the main module.
+ return module.Version{}
}
if _, r, ok := replacement(mod, MainModules.WorkFileReplaceMap()); ok {
return r
--- /dev/null
+# Ensure that replaces of the main module in workspace modules
+# are ignored, and replaces in the go.work file are disallowed.
+# This tests against an issue where requirements of the
+# main module were being ignored because the main module
+# was replaced in a transitive dependency with another
+# version.
+
+go list example.com/dep
+
+cp replace_main_module.go.work go.work
+! go list example.com/dep
+stderr 'go: workspace module example.com/mainmoda is replaced at all versions in the go.work file. To fix, remove the replacement from the go.work file or specify the version at which to replace the module.'
+
+-- replace_main_module.go.work --
+go 1.18
+use (
+ ./mainmoda
+ ./mainmodb
+)
+replace example.com/mainmoda => ../mainmodareplacement
+-- go.work --
+go 1.18
+use (
+ ./mainmoda
+ ./mainmodb
+)
+-- mainmoda/go.mod --
+module example.com/mainmoda
+
+go 1.18
+
+require example.com/dep v1.0.0
+replace example.com/dep => ../dep
+
+-- dep/go.mod --
+module example.com/dep
+-- dep/dep.go --
+package dep
+-- mainmodb/go.mod --
+module example.com/mainmodb
+go 1.18
+replace example.com/mainmoda => ../mainmodareplacement
+-- mainmodareplacement/go.mod --
+module example.com/mainmoda
+go 1.18
\ No newline at end of file