From ef160abf2dc24fbf8d8c34a7ca18b2c1d2f60e7f Mon Sep 17 00:00:00 2001 From: yongqijia Date: Thu, 12 Feb 2026 19:32:16 +0800 Subject: 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 Reviewed-by: Antonio Camacho Reviewed-by: Michael Matloob Reviewed-by: Sean Liao LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Matloob Reviewed-by: Cherry Mui --- src/cmd/go/internal/modfetch/proxy.go | 12 ++++++++---- src/cmd/go/testdata/script/get_version_error_44810.txt | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 src/cmd/go/testdata/script/get_version_error_44810.txt (limited to 'src/cmd') diff --git a/src/cmd/go/internal/modfetch/proxy.go b/src/cmd/go/internal/modfetch/proxy.go index 896f310bdf..bddeca3340 100644 --- a/src/cmd/go/internal/modfetch/proxy.go +++ b/src/cmd/go/internal/modfetch/proxy.go @@ -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, } } diff --git a/src/cmd/go/testdata/script/get_version_error_44810.txt b/src/cmd/go/testdata/script/get_version_error_44810.txt new file mode 100644 index 0000000000..37c7b8e4be --- /dev/null +++ b/src/cmd/go/testdata/script/get_version_error_44810.txt @@ -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' -- cgit v1.3-5-g9baa