cmd/go: check go version when parsing go.mod fails

Fixes #70979

Change-Id: I6597fe178eed34702eea6cba4eec5174c9203458
Reviewed-on: https://go-review.googlesource.com/c/go/+/639115
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
Sean Liao 2024-12-28 11:19:23 -05:00 committed by Michael Matloob
parent de9fdc7b71
commit 6da16013ba
2 changed files with 22 additions and 0 deletions

View file

@ -44,6 +44,17 @@ func ReadModFile(gomod string, fix modfile.VersionFixer) (data []byte, f *modfil
f, err = modfile.Parse(gomod, data, fix) f, err = modfile.Parse(gomod, data, fix)
if err != nil { if err != nil {
f, laxErr := modfile.ParseLax(gomod, data, fix)
if laxErr == nil {
if f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 {
toolchain := ""
if f.Toolchain != nil {
toolchain = f.Toolchain.Name
}
return nil, nil, &gover.TooNewError{What: base.ShortPath(gomod), GoVersion: f.Go.Version, Toolchain: toolchain}
}
}
// Errors returned by modfile.Parse begin with file:line. // Errors returned by modfile.Parse begin with file:line.
return nil, nil, fmt.Errorf("errors parsing %s:\n%w", base.ShortPath(gomod), shortPathErrorList(err)) return nil, nil, fmt.Errorf("errors parsing %s:\n%w", base.ShortPath(gomod), shortPathErrorList(err))
} }

View file

@ -0,0 +1,11 @@
env GOTOOLCHAIN=local
! go list .
stderr 'go: go.mod requires go >= 1.999'
-- go.mod --
module example.com
go 1.999
anewblock foo