cmd/go: avoid repetitive "invalid version" error on go get

When go get encounters an invalid version string like "branch/with/slash",
the error message redundantly repeats the version information:

  go get: pkg@branch/with/slash: invalid version: version "branch/with/slash" invalid: disallowed version string

This happens because proxyRepo.versionError wraps the error from
module.EscapeVersion (which already returns an InvalidVersionError) in
another InvalidVersionError, causing ModuleError.Error to format both
layers.

Avoid the double wrapping by checking whether the error is already an
InvalidVersionError before creating a new one. The resulting message is:

  go get: pkg@branch/with/slash: invalid version: disallowed version string

Fixes #44810

Change-Id: I5c259ef6f1ea23b6673689defbe7e51c8ec813ec
Reviewed-on: https://go-review.googlesource.com/c/go/+/744920
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Antonio Camacho <antoniocho444@gmail.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
yongqijia
2026-02-12 19:32:16 +08:00
committed by Gopher Robot
parent 07e487bc5c
commit ef160abf2d
2 changed files with 18 additions and 4 deletions

View File

@@ -238,13 +238,17 @@ func (p *proxyRepo) CheckReuse(ctx context.Context, old *codehost.Origin) error
// versionError returns err wrapped in a ModuleError for p.path.
func (p *proxyRepo) versionError(version string, err error) error {
if version != "" && version != module.CanonicalVersion(version) {
return &module.ModuleError{
Path: p.path,
Err: &module.InvalidVersionError{
var iv *module.InvalidVersionError
if !errors.As(err, &iv) {
iv = &module.InvalidVersionError{
Version: version,
Pseudo: module.IsPseudoVersion(version),
Err: err,
},
}
}
return &module.ModuleError{
Path: p.path,
Err: iv,
}
}

View File

@@ -0,0 +1,10 @@
# Issue #44810: go get should not produce unnecessarily repetitive
# "invalid version" error messages.
go mod init m
# A version string containing a slash should produce a concise error
# without repeating the version information multiple times.
! go get example.com/mod@branch/with/slash
stderr '^go: example.com/mod@branch/with/slash: invalid version: disallowed version string$'
! stderr 'version "branch/with/slash" invalid'