Skip to content

Commit 11bb292

Browse files
committed
cmd/go: fix reporting of test cycles to have proper order
and begin and end with the same package to demonstrate the cyclical nature of the stack. Also fix the list_test_cycle script test which was testing for the wrong behavior. Fixes #59970 Change-Id: I3b3ee6762ee121fec19688ff1823cdfddae94f53 Reviewed-on: https://go-review.googlesource.com/c/go/+/498115 Reviewed-by: Michael Matloob <[email protected]> Run-TryBot: Michael Matloob <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 789701e commit 11bb292

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/cmd/go/internal/load/test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import (
1616
"go/token"
1717
"internal/lazytemplate"
1818
"path/filepath"
19+
"slices"
1920
"sort"
2021
"strings"
2122
"unicode"
2223
"unicode/utf8"
2324

2425
"cmd/go/internal/cfg"
2526
"cmd/go/internal/fsys"
26-
"cmd/go/internal/slices"
2727
"cmd/go/internal/str"
2828
"cmd/go/internal/trace"
2929
)
@@ -520,11 +520,22 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) *PackageError {
520520
p := q[0]
521521
q = q[1:]
522522
if p == ptest {
523+
// The stack is supposed to be in the order x imports y imports z.
524+
// We collect in the reverse order: z is imported by y is imported
525+
// by x, and then we reverse it.
523526
var stk []string
524527
for p != nil {
525528
stk = append(stk, p.ImportPath)
526529
p = importerOf[p]
527530
}
531+
// complete the cycle: we set importer[p] = nil to break the cycle
532+
// in importerOf, it's an implicit importerOf[p] == pTest. Add it
533+
// back here since we reached nil in the loop above to demonstrate
534+
// the cycle as (for example) package p imports package q imports package r
535+
// imports package p.
536+
stk = append(stk, ptest.ImportPath)
537+
slices.Reverse(stk)
538+
528539
return &PackageError{
529540
ImportStack: stk,
530541
Err: errors.New("import cycle not allowed in test"),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ cmp stderr wanterr.txt
1515

1616
-- wanterr.txt --
1717
go: can't load test package: package example/p
18+
imports example/q
1819
imports example/r
19-
imports example/q: import cycle not allowed in test
20+
imports example/p: import cycle not allowed in test
2021
-- go.mod --
2122
module example
2223
go 1.20

0 commit comments

Comments
 (0)