]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix reporting of test cycles to have proper order
authorMichael Matloob <matloob@google.com>
Wed, 24 May 2023 18:59:01 +0000 (14:59 -0400)
committerMichael Matloob <matloob@golang.org>
Thu, 25 May 2023 19:18:23 +0000 (19:18 +0000)
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 <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/go/internal/load/test.go
src/cmd/go/testdata/script/list_test_cycle.txt

index ff3e17c90a4064b25f2c41e24ee9dff40a58ba2d..ceedb99e2f1e3a19c40abcf8a978e2fec26c5740 100644 (file)
@@ -16,6 +16,7 @@ import (
        "go/token"
        "internal/lazytemplate"
        "path/filepath"
+       "slices"
        "sort"
        "strings"
        "unicode"
@@ -23,7 +24,6 @@ import (
 
        "cmd/go/internal/cfg"
        "cmd/go/internal/fsys"
-       "cmd/go/internal/slices"
        "cmd/go/internal/str"
        "cmd/go/internal/trace"
 )
@@ -520,11 +520,22 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) *PackageError {
                p := q[0]
                q = q[1:]
                if p == ptest {
+                       // The stack is supposed to be in the order x imports y imports z.
+                       // We collect in the reverse order: z is imported by y is imported
+                       // by x, and then we reverse it.
                        var stk []string
                        for p != nil {
                                stk = append(stk, p.ImportPath)
                                p = importerOf[p]
                        }
+                       // complete the cycle: we set importer[p] = nil to break the cycle
+                       // in importerOf, it's an implicit importerOf[p] == pTest. Add it
+                       // back here since we reached nil in the loop above to demonstrate
+                       // the cycle as (for example) package p imports package q imports package r
+                       // imports package p.
+                       stk = append(stk, ptest.ImportPath)
+                       slices.Reverse(stk)
+
                        return &PackageError{
                                ImportStack:   stk,
                                Err:           errors.New("import cycle not allowed in test"),
index ea6379200787bf01dd552fbe7c481eb17f4adc0a..67edf183374b0b6cf096f0e4a50c648822b2248f 100644 (file)
@@ -15,8 +15,9 @@ cmp stderr wanterr.txt
 
 -- wanterr.txt --
 go: can't load test package: package example/p
+       imports example/q
        imports example/r
-       imports example/q: import cycle not allowed in test
+       imports example/p: import cycle not allowed in test
 -- go.mod --
 module example
 go 1.20