1
0
mirror of https://github.com/golang/go synced 2024-11-17 08:54:41 -07:00

cmd/go: ignore replaces of main modules in workspace modules

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>
This commit is contained in:
Michael Matloob 2022-01-19 13:38:05 -05:00
parent 59122f85bd
commit 65535bfe6d
3 changed files with 55 additions and 0 deletions

View 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 {

View 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

View File

@ -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