From a9ba734e4df7d4006b18dfc6f8f4272da828c6ef Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Thu, 25 Feb 2021 18:38:43 -0500 Subject: [PATCH] [release-branch.go1.16] cmd/go/internal/modload: don't query when fixing canonical versions If a canonical version is passed to fixVersion when loading the main go.mod and that version don't match the module path's major version suffix, don't call Query. Query doesn't return a useful error in this case when the path is malformed, for example, when it doens't have a dot in the first path element. It's better to report the major version mismatch error. Fixes #44496 Change-Id: I97b1f64aee894fa0db6fb637aa03a51357ee782c Reviewed-on: https://go-review.googlesource.com/c/go/+/296590 Trust: Jay Conrod Run-TryBot: Jay Conrod TryBot-Result: Go Bot Reviewed-by: Bryan C. Mills (cherry picked from commit 5fafc0bbd4819578e58e5b9163981b0074ab0b01) Reviewed-on: https://go-review.googlesource.com/c/go/+/297989 Reviewed-by: Dmitri Shuralyov --- src/cmd/go/internal/modload/init.go | 5 ++-- .../script/mod_retract_fix_version.txt | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index bc8d17e0a5fb5..4de5ac93032e2 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -539,9 +539,10 @@ func fixVersion(ctx context.Context, fixed *bool) modfile.VersionFixer { } } if vers != "" && module.CanonicalVersion(vers) == vers { - if err := module.CheckPathMajor(vers, pathMajor); err == nil { - return vers, nil + if err := module.CheckPathMajor(vers, pathMajor); err != nil { + return "", module.VersionError(module.Version{Path: path, Version: vers}, err) } + return vers, nil } info, err := Query(ctx, path, vers, "", nil) diff --git a/src/cmd/go/testdata/script/mod_retract_fix_version.txt b/src/cmd/go/testdata/script/mod_retract_fix_version.txt index f8099ec93e38e..e45758b627016 100644 --- a/src/cmd/go/testdata/script/mod_retract_fix_version.txt +++ b/src/cmd/go/testdata/script/mod_retract_fix_version.txt @@ -12,6 +12,18 @@ go mod tidy go list -m all cmp go.mod go.mod.want +# If a retracted version doesn't match the module's major version suffx, +# an error should be reported. +! go mod edit -retract=v3.0.1 +stderr '^go mod: -retract=v3.0.1: version "v3.0.1" invalid: should be v2, not v3$' +cp go.mod.mismatch-v2 go.mod +! go list -m all +stderr 'go.mod:3: retract rsc.io/quote/v2: version "v3.0.1" invalid: should be v2, not v3$' + +cp go.mod.mismatch-v1 go.mod +! go list -m all +stderr 'go.mod:3: retract rsc.io/quote: version "v3.0.1" invalid: should be v0 or v1, not v3$' + -- go.mod -- go 1.16 @@ -22,3 +34,15 @@ go 1.16 retract v2.0.1 module rsc.io/quote/v2 +-- go.mod.mismatch-v2 -- +go 1.16 + +retract v3.0.1 + +module rsc.io/quote/v2 +-- go.mod.mismatch-v1 -- +go 1.16 + +retract v3.0.1 + +module rsc.io/quote