Skip to content

Commit 5a9fc94

Browse files
author
Bryan C. Mills
committed
cmd/go: avoid +incompatible major versions if a go.mod file exists in a subdirectory for that version
Previous versions of the 'go' command would reject a pseudo-version passed to 'go get' if that pseudo-version had a mismatched major version and lacked a "+incompatible" suffix. However, they would erroneously accept a version *with* a "+incompatible" suffix even if the repo contained a vN/go.mod file for the same major version, and would generate a "+incompatible" pseudo-version or version if the user requested a tag, branch, or commit hash. This change uniformly rejects "vN.…" without "+incompatible", and also avoids resolving to "vN.…+incompatible", when vN/go.mod exists. To maintain compatibility with existing go.mod files, it still accepts "vN.…+incompatible" if the version is requested explicitly as such and the repo root lacks a go.mod file. Fixes #51324 Updates #36438 Change-Id: I2b16150c73fc2abe4d0a1cd34cb1600635db7139 Reviewed-on: https://go-review.googlesource.com/c/go/+/387675 Trust: Bryan Mills <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 4edefe9 commit 5a9fc94

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

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

+41-12
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,46 @@ func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, e
305305
//
306306
// (If the version is +incompatible, then the go.mod file must not exist:
307307
// +incompatible is not an ongoing opt-out from semantic import versioning.)
308-
var canUseIncompatible func() bool
309-
canUseIncompatible = func() bool {
310-
var ok bool
311-
if r.codeDir == "" && r.pathMajor == "" {
308+
incompatibleOk := map[string]bool{}
309+
canUseIncompatible := func(v string) bool {
310+
if r.codeDir != "" || r.pathMajor != "" {
311+
// A non-empty codeDir indicates a module within a subdirectory,
312+
// which necessarily has a go.mod file indicating the module boundary.
313+
// A non-empty pathMajor indicates a module path with a major-version
314+
// suffix, which must match.
315+
return false
316+
}
317+
318+
ok, seen := incompatibleOk[""]
319+
if !seen {
312320
_, errGoMod := r.code.ReadFile(info.Name, "go.mod", codehost.MaxGoMod)
313-
if errGoMod != nil {
314-
ok = true
321+
ok = (errGoMod != nil)
322+
incompatibleOk[""] = ok
323+
}
324+
if !ok {
325+
// A go.mod file exists at the repo root.
326+
return false
327+
}
328+
329+
// Per https://go.dev/issue/51324, previous versions of the 'go' command
330+
// didn't always check for go.mod files in subdirectories, so if the user
331+
// requests a +incompatible version explicitly, we should continue to allow
332+
// it. Otherwise, if vN/go.mod exists, expect that release tags for that
333+
// major version are intended for the vN module.
334+
if v != "" && !strings.HasSuffix(statVers, "+incompatible") {
335+
major := semver.Major(v)
336+
ok, seen = incompatibleOk[major]
337+
if !seen {
338+
_, errGoModSub := r.code.ReadFile(info.Name, path.Join(major, "go.mod"), codehost.MaxGoMod)
339+
ok = (errGoModSub != nil)
340+
incompatibleOk[major] = ok
341+
}
342+
if !ok {
343+
return false
315344
}
316345
}
317-
canUseIncompatible = func() bool { return ok }
318-
return ok
346+
347+
return true
319348
}
320349

321350
// checkCanonical verifies that the canonical version v is compatible with the
@@ -367,7 +396,7 @@ func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, e
367396
base := strings.TrimSuffix(v, "+incompatible")
368397
var errIncompatible error
369398
if !module.MatchPathMajor(base, r.pathMajor) {
370-
if canUseIncompatible() {
399+
if canUseIncompatible(base) {
371400
v = base + "+incompatible"
372401
} else {
373402
if r.pathMajor != "" {
@@ -495,7 +524,7 @@ func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, e
495524
// Save the highest non-retracted canonical tag for the revision.
496525
// If we don't find a better match, we'll use it as the canonical version.
497526
if tagIsCanonical && semver.Compare(highestCanonical, v) < 0 && !isRetracted(v) {
498-
if module.MatchPathMajor(v, r.pathMajor) || canUseIncompatible() {
527+
if module.MatchPathMajor(v, r.pathMajor) || canUseIncompatible(v) {
499528
highestCanonical = v
500529
}
501530
}
@@ -513,12 +542,12 @@ func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, e
513542
// retracted versions.
514543
allowedMajor := func(major string) func(v string) bool {
515544
return func(v string) bool {
516-
return (major == "" || semver.Major(v) == major) && !isRetracted(v)
545+
return ((major == "" && canUseIncompatible(v)) || semver.Major(v) == major) && !isRetracted(v)
517546
}
518547
}
519548
if pseudoBase == "" {
520549
var tag string
521-
if r.pseudoMajor != "" || canUseIncompatible() {
550+
if r.pseudoMajor != "" || canUseIncompatible("") {
522551
tag, _ = r.code.RecentTag(info.Name, tagPrefix, allowedMajor(r.pseudoMajor))
523552
} else {
524553
// Allow either v1 or v0, but not incompatible higher versions.

src/cmd/go/internal/modfetch/coderepo_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,54 @@ var codeRepoTests = []codeRepoTest{
458458
rev: "v3.0.0-devel",
459459
err: `resolves to version v0.1.1-0.20220203155313-d59622f6e4d7 (v3.0.0-devel is not a tag)`,
460460
},
461+
462+
// If v2/go.mod exists, then we should prefer to match the "v2"
463+
// pseudo-versions to the nested module, and resolve the module in the parent
464+
// directory to only compatible versions.
465+
//
466+
// However (https://go.dev/issue/51324), previous versions of the 'go' command
467+
// didn't always do so, so if the user explicitly requests a +incompatible
468+
// version (as would be present in an existing go.mod file), we should
469+
// continue to allow it.
470+
{
471+
vcs: "git",
472+
path: "vcs-test.golang.org/git/v2sub.git",
473+
rev: "80beb17a1603",
474+
version: "v0.0.0-20220222205507-80beb17a1603",
475+
name: "80beb17a16036f17a5aedd1bb5bd6d407b3c6dc5",
476+
short: "80beb17a1603",
477+
time: time.Date(2022, 2, 22, 20, 55, 7, 0, time.UTC),
478+
},
479+
{
480+
vcs: "git",
481+
path: "vcs-test.golang.org/git/v2sub.git",
482+
rev: "v2.0.0",
483+
err: `module contains a go.mod file, so module path must match major version ("vcs-test.golang.org/git/v2sub.git/v2")`,
484+
},
485+
{
486+
vcs: "git",
487+
path: "vcs-test.golang.org/git/v2sub.git",
488+
rev: "v2.0.1-0.20220222205507-80beb17a1603",
489+
err: `module contains a go.mod file, so module path must match major version ("vcs-test.golang.org/git/v2sub.git/v2")`,
490+
},
491+
{
492+
vcs: "git",
493+
path: "vcs-test.golang.org/git/v2sub.git",
494+
rev: "v2.0.0+incompatible",
495+
version: "v2.0.0+incompatible",
496+
name: "5fcd3eaeeb391d399f562fd45a50dac9fc34ae8b",
497+
short: "5fcd3eaeeb39",
498+
time: time.Date(2022, 2, 22, 20, 53, 33, 0, time.UTC),
499+
},
500+
{
501+
vcs: "git",
502+
path: "vcs-test.golang.org/git/v2sub.git",
503+
rev: "v2.0.1-0.20220222205507-80beb17a1603+incompatible",
504+
version: "v2.0.1-0.20220222205507-80beb17a1603+incompatible",
505+
name: "80beb17a16036f17a5aedd1bb5bd6d407b3c6dc5",
506+
short: "80beb17a1603",
507+
time: time.Date(2022, 2, 22, 20, 55, 7, 0, time.UTC),
508+
},
461509
}
462510

463511
func TestCodeRepo(t *testing.T) {

0 commit comments

Comments
 (0)