From 176baafd5b6d968fc0df25b344ffe826e47e6879 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Wed, 28 Jul 2021 12:32:08 -0400 Subject: [PATCH] [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 Run-TryBot: Michael Matloob Reviewed-by: Jay Conrod --- src/cmd/go/internal/modload/buildlist.go | 3 +++ src/cmd/go/testdata/script/work.txt | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/cmd/go/internal/modload/buildlist.go b/src/cmd/go/internal/modload/buildlist.go index d2957a34c4..4fbe563cb8 100644 --- a/src/cmd/go/internal/modload/buildlist.go +++ b/src/cmd/go/internal/modload/buildlist.go @@ -1083,6 +1083,9 @@ func updateEagerRoots(ctx context.Context, direct map[string]bool, rs *Requireme } roots = append(roots, min...) } + if MainModules.Len() > 1 { + module.Sort(roots) + } if rs.depth == eager && reflect.DeepEqual(roots, rs.rootModules) && reflect.DeepEqual(direct, rs.direct) { // The root set is unchanged and rs was already eager, so keep rs to // preserve its cached ModuleGraph (if any). diff --git a/src/cmd/go/testdata/script/work.txt b/src/cmd/go/testdata/script/work.txt index 0d820fffc2..eeaf92eaec 100644 --- a/src/cmd/go/testdata/script/work.txt +++ b/src/cmd/go/testdata/script/work.txt @@ -36,6 +36,11 @@ cp go.work.backup go.work cp go.work.d go.work go run example.com/d +# Test that we don't run into "newRequirements called with unsorted roots" +# panic with unsorted main modules. +cp go.work.backwards go.work +go run example.com/d + -- go.work.dup -- go 1.17 @@ -109,3 +114,12 @@ import "example.com/b/lib" func main() { lib.Hello() } + +-- go.work.backwards -- +go 1.18 + +directory ( + d + b + a +) \ No newline at end of file -- 2.48.1