Skip to content

Commit

Permalink
[release-branch.go1.16] cmd/go/internal/modload: don't query when fix…
Browse files Browse the repository at this point in the history
…ing 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 <[email protected]>
Run-TryBot: Jay Conrod <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
(cherry picked from commit 5fafc0b)
Reviewed-on: https://go-review.googlesource.com/c/go/+/297989
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
Jay Conrod authored and dmitshur committed Mar 3, 2021
1 parent 047ca22 commit a9ba734
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions src/cmd/go/testdata/script/mod_retract_fix_version.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

0 comments on commit a9ba734

Please sign in to comment.