]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.16] cmd/go/internal/load: always set IsImportCycle when in a...
authorRoland Shoemaker <roland@golang.org>
Fri, 12 Mar 2021 17:51:50 +0000 (09:51 -0800)
committerCarlos Amedee <carlos@golang.org>
Mon, 2 Aug 2021 22:23:30 +0000 (22:23 +0000)
When hitting an import cycle in reusePackage, and there is already
an error set, make sure IsImportCycle is set so that we don't
end up stuck in a loop.

Updates #25830
Fixes #47348

Change-Id: Iba966aea4a637dfc34ee22782a477209ac48c9bd
Reviewed-on: https://go-review.googlesource.com/c/go/+/301289
Trust: Roland Shoemaker <roland@golang.org>
Run-TryBot: Roland Shoemaker <roland@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
(cherry picked from commit cdd08e615a9b92742b21a94443720b6d70452510)
Reviewed-on: https://go-review.googlesource.com/c/go/+/336649
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>

src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_err_cycle.txt [new file with mode: 0644]

index 8b12faf4cd2e51d8665a838c68275dce87cba018..61fde895f85b24166afc338d391b90992476f68b 100644 (file)
@@ -1323,6 +1323,11 @@ func reusePackage(p *Package, stk *ImportStack) *Package {
                                Err:           errors.New("import cycle not allowed"),
                                IsImportCycle: true,
                        }
+               } else if !p.Error.IsImportCycle {
+                       // If the error is already set, but it does not indicate that
+                       // we are in an import cycle, set IsImportCycle so that we don't
+                       // end up stuck in a loop down the road.
+                       p.Error.IsImportCycle = true
                }
                p.Incomplete = true
        }
diff --git a/src/cmd/go/testdata/script/list_err_cycle.txt b/src/cmd/go/testdata/script/list_err_cycle.txt
new file mode 100644 (file)
index 0000000..44b82a6
--- /dev/null
@@ -0,0 +1,15 @@
+# Check that we don't get infinite recursion when loading a package with
+# an import cycle and another error. Verifies #25830.
+! go list
+stderr 'found packages a \(a.go\) and b \(b.go\)'
+
+-- go.mod --
+module errcycle
+
+go 1.16
+-- a.go --
+package a
+
+import _ "errcycle"
+-- b.go --
+package b
\ No newline at end of file