Skip to content

Commit 176baaf

Browse files
committed
[dev.cmdgo] cmd/go: sort roots when joining multiple main module roots
When concatenating multiple main modules' roots we need to resort the list of main module roots. This avoids a panic from unsorted main module roots. This will get better when we have structured module roots. Change-Id: I68fed47b0f3b131ed3dadf45db3c442286a0ced0 Reviewed-on: https://go-review.googlesource.com/c/go/+/338111 Trust: Michael Matloob <[email protected]> Run-TryBot: Michael Matloob <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 288a83d commit 176baaf

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/cmd/go/internal/modload/buildlist.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,9 @@ func updateEagerRoots(ctx context.Context, direct map[string]bool, rs *Requireme
10831083
}
10841084
roots = append(roots, min...)
10851085
}
1086+
if MainModules.Len() > 1 {
1087+
module.Sort(roots)
1088+
}
10861089
if rs.depth == eager && reflect.DeepEqual(roots, rs.rootModules) && reflect.DeepEqual(direct, rs.direct) {
10871090
// The root set is unchanged and rs was already eager, so keep rs to
10881091
// preserve its cached ModuleGraph (if any).

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ cp go.work.backup go.work
3636
cp go.work.d go.work
3737
go run example.com/d
3838

39+
# Test that we don't run into "newRequirements called with unsorted roots"
40+
# panic with unsorted main modules.
41+
cp go.work.backwards go.work
42+
go run example.com/d
43+
3944
-- go.work.dup --
4045
go 1.17
4146

@@ -109,3 +114,12 @@ import "example.com/b/lib"
109114
func main() {
110115
lib.Hello()
111116
}
117+
118+
-- go.work.backwards --
119+
go 1.18
120+
121+
directory (
122+
d
123+
b
124+
a
125+
)

0 commit comments

Comments
 (0)