aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/load.go
diff options
context:
space:
mode:
authorJeremy Brewer <jeremybrewer@google.com>2022-09-19 20:09:56 -0400
committerGopher Robot <gobot@golang.org>2022-10-07 20:20:51 +0000
commit40c7e94cc5740496a0a47d6ef9863b2bbca44b0e (patch)
tree652d2ded779da039d75e1a7a072a3717b9259c5f /src/cmd/go/internal/modload/load.go
parent9be09916d9209a02308d553cfbbb7ff1dc6593dd (diff)
downloadgo-40c7e94cc5740496a0a47d6ef9863b2bbca44b0e.tar.gz
go-40c7e94cc5740496a0a47d6ef9863b2bbca44b0e.zip
modload: provide a clearer error for standard library packages from newer releases
An older version of go compiling a main module that references a standard library package from a newer release (e.g. net/netip added in go 1.18) currently produces a confusing error message. This changes adds a new error message including go version diagnostics. Fixes #48966 Change-Id: I1e8319dafcf1f67d1b1ca869fe84190c3b3f3c3e Reviewed-on: https://go-review.googlesource.com/c/go/+/432075 Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/cmd/go/internal/modload/load.go')
-rw-r--r--src/cmd/go/internal/modload/load.go26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go
index afd6c80370..09572bf1b1 100644
--- a/src/cmd/go/internal/modload/load.go
+++ b/src/cmd/go/internal/modload/load.go
@@ -984,7 +984,7 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
if ld.GoVersion == "" {
ld.GoVersion = MainModules.GoVersion()
- if ld.Tidy && semver.Compare("v"+ld.GoVersion, "v"+LatestGoVersion()) > 0 {
+ if ld.Tidy && versionLess(LatestGoVersion(), ld.GoVersion) {
ld.errorf("go: go.mod file indicates go %s, but maximum version supported by tidy is %s\n", ld.GoVersion, LatestGoVersion())
base.ExitIfErrors()
}
@@ -993,7 +993,7 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
if ld.Tidy {
if ld.TidyCompatibleVersion == "" {
ld.TidyCompatibleVersion = priorGoVersion(ld.GoVersion)
- } else if semver.Compare("v"+ld.TidyCompatibleVersion, "v"+ld.GoVersion) > 0 {
+ } else if versionLess(ld.GoVersion, ld.TidyCompatibleVersion) {
// Each version of the Go toolchain knows how to interpret go.mod and
// go.sum files produced by all previous versions, so a compatibility
// version higher than the go.mod version adds nothing.
@@ -1184,11 +1184,19 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
}
}
- if ld.SilencePackageErrors {
- continue
+ if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) && stdErr.isStd {
+ // Add importer go version information to import errors of standard
+ // library packages arising from newer releases.
+ if importer := pkg.stack; importer != nil {
+ if v, ok := rawGoVersion.Load(importer.mod); ok && versionLess(LatestGoVersion(), v.(string)) {
+ stdErr.importerGoVersion = v.(string)
+ }
+ }
+ if ld.SilenceMissingStdImports {
+ continue
+ }
}
- if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) &&
- stdErr.isStd && ld.SilenceMissingStdImports {
+ if ld.SilencePackageErrors {
continue
}
if ld.SilenceNoGoErrors && errors.Is(pkg.err, imports.ErrNoGo) {
@@ -1202,6 +1210,12 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
return ld
}
+// versionLess returns whether a < b according to semantic version precedence.
+// Both strings are interpreted as go version strings, e.g. "1.19".
+func versionLess(a, b string) bool {
+ return semver.Compare("v"+a, "v"+b) < 0
+}
+
// updateRequirements ensures that ld.requirements is consistent with the
// information gained from ld.pkgs.
//