diff options
| author | yongqijia <YongqiJia520@gmail.com> | 2026-02-12 19:32:16 +0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-06 12:11:45 -0800 |
| commit | ef160abf2dc24fbf8d8c34a7ca18b2c1d2f60e7f (patch) | |
| tree | b514fa69d1d84c89829419b147a24f9ee907f0f8 /src/cmd | |
| parent | 07e487bc5ceb05d5fdbbec3c66f75a8a36460038 (diff) | |
| download | go-ef160abf2dc24fbf8d8c34a7ca18b2c1d2f60e7f.tar.xz | |
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>
Diffstat (limited to 'src/cmd')
| -rw-r--r-- | src/cmd/go/internal/modfetch/proxy.go | 12 | ||||
| -rw-r--r-- | src/cmd/go/testdata/script/get_version_error_44810.txt | 10 |
2 files changed, 18 insertions, 4 deletions
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' |
