Skip to content

Commit

Permalink
[release-branch.go1.15] cmd/go/internal/load: always set IsImportCycl…
Browse files Browse the repository at this point in the history
…e when in a cycle

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 #47347

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 cdd08e6)
Reviewed-on: https://go-review.googlesource.com/c/go/+/336669
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
  • Loading branch information
rolandshoemaker authored and cagedmantis committed Aug 2, 2021
1 parent ba93baa commit b81d75f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cmd/go/internal/load/pkg.go
Expand Up @@ -1280,6 +1280,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
}
Expand Down
15 changes: 15 additions & 0 deletions src/cmd/go/testdata/script/list_err_cycle.txt
@@ -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

0 comments on commit b81d75f

Please sign in to comment.