]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: ignore replaces of main modules in workspace modules
authorMichael Matloob <matloob@golang.org>
Wed, 19 Jan 2022 18:38:05 +0000 (13:38 -0500)
committerMichael Matloob <matloob@golang.org>
Thu, 20 Jan 2022 19:10:29 +0000 (19:10 +0000)
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>

src/cmd/go/internal/modload/init.go
src/cmd/go/internal/modload/modfile.go
src/cmd/go/testdata/script/work_replace_main_module.txt [new file with mode: 0644]

index fe7d0ef3e6abc8d06eeffadb675158ada22067e6..cdcfbeb8dedfb47f23a507abf73ac438e2e31260 100644 (file)
@@ -983,9 +983,16 @@ func makeMainModules(ms []module.Version, rootDirs []string, modFiles []*modfile
                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 {
index ec3f57ae3e1da513ae6a3832489beb8f49d0460c..627cf1dbc0642ee6056bdfac85a18940093a54c3 100644 (file)
@@ -340,6 +340,9 @@ func Replacement(mod module.Version) module.Version {
        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
diff --git a/src/cmd/go/testdata/script/work_replace_main_module.txt b/src/cmd/go/testdata/script/work_replace_main_module.txt
new file mode 100644 (file)
index 0000000..b213764
--- /dev/null
@@ -0,0 +1,45 @@
+# 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