Skip to content

Commit 5ba15db

Browse files
author
Bryan C. Mills
committed
cmd/go: suppress errors with '@upgrade' when the latest version is replaced
Fixes #33154 Change-Id: I5a249a77843a8bd438006af0fa1d8b4429ee25f4 Reviewed-on: https://go-review.googlesource.com/c/go/+/186617 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
1 parent 34778e5 commit 5ba15db

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

src/cmd/go/internal/modfetch/coderepo.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,13 @@ func (r *codeRepo) Stat(rev string) (*RevInfo, error) {
192192
codeRev := r.revToRev(rev)
193193
info, err := r.code.Stat(codeRev)
194194
if err != nil {
195-
return nil, err
195+
return nil, &module.ModuleError{
196+
Path: r.modPath,
197+
Err: &module.InvalidVersionError{
198+
Version: rev,
199+
Err: err,
200+
},
201+
}
196202
}
197203
return r.convert(info, rev)
198204
}

src/cmd/go/internal/modget/get.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,16 @@ func getQuery(path, vers string, prevM module.Version, forceModulePath bool) (mo
765765

766766
// If the query fails, and the path must be a real module, report the query error.
767767
if forceModulePath {
768+
// If the query was "upgrade" or "patch" and the current version has been
769+
// replaced, check to see whether the error was for that same version:
770+
// if so, the version was probably replaced because it is invalid,
771+
// and we should keep that replacement without complaining.
772+
if vers == "upgrade" || vers == "patch" {
773+
var vErr *module.InvalidVersionError
774+
if errors.As(err, &vErr) && vErr.Version == prevM.Version && modload.Replacement(prevM).Path != "" {
775+
return prevM, nil
776+
}
777+
}
768778
return module.Version{}, err
769779
}
770780
}
@@ -911,6 +921,15 @@ func (u *upgrader) Upgrade(m module.Version) (module.Version, error) {
911921
if err != nil {
912922
// Report error but return m, to let version selection continue.
913923
// (Reporting the error will fail the command at the next base.ExitIfErrors.)
924+
925+
// Special case: if the error is for m.Version itself and m.Version has a
926+
// replacement, then keep it and don't report the error: the fact that the
927+
// version is invalid is likely the reason it was replaced to begin with.
928+
var vErr *module.InvalidVersionError
929+
if errors.As(err, &vErr) && vErr.Version == m.Version && modload.Replacement(m).Path != "" {
930+
return m, nil
931+
}
932+
914933
// Special case: if the error is "no matching versions" then don't
915934
// even report the error. Because Query does not consider pseudo-versions,
916935
// it may happen that we have a pseudo-version but during -u=patch

src/cmd/go/internal/modload/query_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ var queryTests = []struct {
100100
{path: queryRepo, query: ">=v0.0.0", vers: "v0.0.0"},
101101
{path: queryRepo, query: "v0.0.1", vers: "v0.0.1"},
102102
{path: queryRepo, query: "v0.0.1+foo", vers: "v0.0.1"},
103-
{path: queryRepo, query: "v0.0.99", err: `unknown revision v0.0.99`},
103+
{path: queryRepo, query: "v0.0.99", err: `vcs-test.golang.org/git/querytest.git@v0.0.99: invalid version: unknown revision v0.0.99`},
104104
{path: queryRepo, query: "v0", vers: "v0.3.0"},
105105
{path: queryRepo, query: "v0.1", vers: "v0.1.2"},
106106
{path: queryRepo, query: "v0.2", err: `no matching versions for query "v0.2"`},
@@ -112,8 +112,8 @@ var queryTests = []struct {
112112
// unconditionally).
113113
{path: queryRepo, query: "42abcb6df8ee", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"},
114114

115-
{path: queryRepo, query: "v1.9.10-pre2+wrongmetadata", err: `unknown revision v1.9.10-pre2+wrongmetadata`},
116-
{path: queryRepo, query: "v1.9.10-pre2", err: `unknown revision v1.9.10-pre2`},
115+
{path: queryRepo, query: "v1.9.10-pre2+wrongmetadata", err: `vcs-test.golang.org/git/querytest.git@v1.9.10-pre2+wrongmetadata: invalid version: unknown revision v1.9.10-pre2+wrongmetadata`},
116+
{path: queryRepo, query: "v1.9.10-pre2", err: `vcs-test.golang.org/git/querytest.git@v1.9.10-pre2: invalid version: unknown revision v1.9.10-pre2`},
117117
{path: queryRepo, query: "latest", vers: "v1.9.9"},
118118
{path: queryRepo, query: "latest", current: "v1.9.10-pre1", vers: "v1.9.9"},
119119
{path: queryRepo, query: "upgrade", vers: "v1.9.9"},
@@ -122,7 +122,7 @@ var queryTests = []struct {
122122
{path: queryRepo, query: "upgrade", current: "v0.0.0-20190513201126-42abcb6df8ee", vers: "v0.0.0-20190513201126-42abcb6df8ee"},
123123
{path: queryRepo, query: "upgrade", allow: "NOMATCH", err: `no matching versions for query "upgrade"`},
124124
{path: queryRepo, query: "upgrade", current: "v1.9.9", allow: "NOMATCH", err: `no matching versions for query "upgrade" (current version is v1.9.9)`},
125-
{path: queryRepo, query: "upgrade", current: "v1.99.99", err: `unknown revision v1.99.99`},
125+
{path: queryRepo, query: "upgrade", current: "v1.99.99", err: `vcs-test.golang.org/git/querytest.git@v1.99.99: invalid version: unknown revision v1.99.99`},
126126
{path: queryRepo, query: "patch", current: "", vers: "v1.9.9"},
127127
{path: queryRepo, query: "patch", current: "v0.1.0", vers: "v0.1.2"},
128128
{path: queryRepo, query: "patch", current: "v1.9.0", vers: "v1.9.9"},

src/cmd/go/testdata/script/mod_invalid_version.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ cd ..
134134
go list -m golang.org/x/text
135135
stdout 'golang.org/x/text v0.0.0-0.20170915032832-14c0d48ead0c => golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c'
136136

137+
# A 'replace' directive can replace an invalid 'latest' version, and
138+
# should suppress errors for that version in 'go get -u'
139+
cp go.mod.orig go.mod
140+
go mod edit -require golang.org/x/text@v1.999999.0
141+
go mod edit -replace golang.org/x/text@v1.999999.0=golang.org/x/text@v0.0.0-20170915032832-14c0d48ead0c
142+
cd outside
143+
! go get -d golang.org/x/text@upgrade
144+
stderr 'go get golang.org/x/text@upgrade: golang.org/x/text@v1.999999.0: invalid version: unknown revision v1.999999.0'
145+
cd ..
146+
go get -d golang.org/x/text@upgrade
147+
go list -m golang.org/x/text
148+
stdout 'golang.org/x/text v1.999999.0 => golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c'
149+
137150
# A pseudo-version derived from a non-ancestor tag is invalid.
138151
cp go.mod.orig go.mod
139152
go mod edit -require golang.org/x/text@v0.2.1-0.20170915032832-14c0d48ead0c

0 commit comments

Comments
 (0)