[go] cmd/dist: detect import cycles instead of deadlocking

7 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Dec 16, 2025, 8:27:01 PM12/16/25
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

cmd/dist: detect import cycles instead of deadlocking

## Summary

Previously, if there was an import cycle in the packages being built (e.g., package A imports B and B imports A), the build would deadlock with "all goroutines are asleep - deadlock!" because each package's goroutine would block waiting for the other to complete.

Now we detect import cycles before blocking on dependencies by tracking which packages each goroutine is waiting on and checking for cycles using depth-first search. When a cycle is detected, we report it with a clear message:

- Self-import: `import cycle: go/ast -> go/ast`
- Indirect cycle: `import cycle: go/ast -> go/scanner -> go/token -> go/ast`

## Test plan

- [x] Tested self-import cycle detection (go/ast imports go/ast)
- [x] Tested indirect import cycle detection (go/token imports go/ast, go/ast imports go/token)
- [x] Verified normal builds complete successfully
- [x] `all.bash` passes: ALL TESTS PASSED

Fixes #76439
Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
GitHub-Last-Rev: a4e4a2a3d1174db6ace8c735b6268c777c88518e
GitHub-Pull-Request: golang/go#76862

Change diff

diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index e4250e1..910338b 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -678,10 +678,46 @@
var installed = make(map[string]chan struct{})
var installedMu sync.Mutex

+// pkgDeps tracks the packages each package is waiting on.
+// It's used to detect import cycles that would cause deadlocks.
+var pkgDeps = make(map[string][]string)
+
func install(dir string) {
<-startInstall(dir)
}

+// checkInstallCycle checks if waiting for dep from pkg would create a cycle.
+// It returns the cycle path if a cycle is found, or nil otherwise.
+// installedMu must be held when calling this function.
+func checkInstallCycle(pkg, dep string) []string {
+ // DFS to find if dep transitively depends on pkg
+ visited := make(map[string]bool)
+ var path []string
+ var dfs func(p string) bool
+ dfs = func(p string) bool {
+ if p == pkg {
+ return true // Found cycle back to pkg
+ }
+ if visited[p] {
+ return false
+ }
+ visited[p] = true
+ path = append(path, p)
+ for _, d := range pkgDeps[p] {
+ if dfs(d) {
+ return true
+ }
+ }
+ path = path[:len(path)-1]
+ return false
+ }
+ if dfs(dep) {
+ // Return the cycle: pkg -> dep -> ... -> pkg
+ return append([]string{pkg}, path...)
+ }
+ return nil
+}
+
func startInstall(dir string) chan struct{} {
installedMu.Lock()
ch := installed[dir]
@@ -702,6 +738,13 @@
}

defer close(ch)
+ defer func() {
+ // Clean up pkgDeps when done to avoid holding references
+ // and to keep the cycle detection data structure clean.
+ installedMu.Lock()
+ delete(pkgDeps, pkg)
+ installedMu.Unlock()
+ }()

if pkg == "unsafe" {
return
@@ -883,13 +926,31 @@
}
sort.Strings(sortedImports)

+ // Start all dependency installations and collect the list of deps.
+ deps := make([]string, 0, len(importMap))
for _, dep := range importMap {
if dep == "C" {
fatalf("%s imports C", pkg)
}
+ deps = append(deps, dep)
startInstall(dep)
}
- for _, dep := range importMap {
+
+ // Check for import cycles before blocking on dependencies.
+ // We record that this package depends on deps, then check if any dep
+ // transitively depends on us, which would cause a deadlock.
+ installedMu.Lock()
+ pkgDeps[pkg] = deps
+ for _, dep := range deps {
+ if cycle := checkInstallCycle(pkg, dep); cycle != nil {
+ installedMu.Unlock()
+ fatalf("import cycle: %s", strings.Join(append(cycle, pkg), " -> "))
+ }
+ }
+ installedMu.Unlock()
+
+ // Wait for all dependencies to be installed.
+ for _, dep := range deps {
install(dep)
}

Change information

Files:
  • M src/cmd/dist/build.go
Change size: M
Delta: 1 file changed, 62 insertions(+), 1 deletion(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
Gerrit-Change-Number: 730620
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Dec 16, 2025, 8:27:03 PM12/16/25
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. You have a long 263 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
2. It looks like you are using markdown in the commit message. If so, please remove it. Be sure to double-check the plain text shown in the Gerrit commit message above for any markdown backticks, markdown links, or other markdown formatting.

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
    Gerrit-Change-Number: 730620
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 17 Dec 2025 01:26:59 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gopher Robot (Gerrit)

    unread,
    Dec 16, 2025, 8:29:42 PM12/16/25
    to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Message from Gopher Robot

    Congratulations on opening your first change. Thank you for your contribution!

    Next steps:
    A maintainer will review your change and provide feedback. See
    https://go.dev/doc/contribute#review for more info and tips to get your
    patch through code review.

    Most changes in the Go project go through a few rounds of revision. This can be
    surprising to people new to the project. The careful, iterative review process
    is our way of helping mentor contributors and ensuring that their contributions
    have a lasting impact.

    During May-July and Nov-Jan the Go project is in a code freeze, during which
    little code gets reviewed or merged. If a reviewer responds with a comment like
    R=go1.11 or adds a tag like "wait-release", it means that this CL will be
    reviewed as part of the next development cycle. See https://go.dev/s/release
    for more details.

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
    Gerrit-Change-Number: 730620
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 17 Dec 2025 01:29:38 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Michael Matloob (Gerrit)

    unread,
    Dec 19, 2025, 1:40:43 PM12/19/25
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Michael Matloob, Alan Donovan, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Michael Matloob

    Michael Matloob added 2 comments

    Patchset-level comments
    Gopher Robot . unresolved

    I spotted some possible problems with your PR:

      1. You have a long 263 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
    2. It looks like you are using markdown in the commit message. If so, please remove it. Be sure to double-check the plain text shown in the Gerrit commit message above for any markdown backticks, markdown links, or other markdown formatting.

    Please address any problems by updating the GitHub PR.

    When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

    To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

    For more details, see:

    (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

    Michael Matloob

    Could you address these issues?

    File src/cmd/dist/build.go
    Line 945, Patchset 1 (Latest): if cycle := checkInstallCycle(pkg, dep); cycle != nil {
    Michael Matloob . unresolved

    We're doing the check for each dependency in the graph, and each check visits all the nodes. The check is made harder by the concurrent aspect, but one option would be to pass in the import stack, and produce the error if the dependency is on the stack. That's essentially how the go command checks for cycles.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Michael Matloob
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedNo-Wait-Release
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
      Gerrit-Change-Number: 730620
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-CC: Alan Donovan <adon...@google.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: Michael Matloob <mat...@google.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Comment-Date: Fri, 19 Dec 2025 18:40:40 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Gopher Robot <go...@golang.org>
      unsatisfied_requirement
      open
      diffy

      Funda Secgin (Gerrit)

      unread,
      Dec 19, 2025, 5:07:32 PM12/19/25
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Michael Matloob, Michael Matloob, Alan Donovan, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Michael Matloob

      Funda Secgin voted Code-Review+1

      Code-Review+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedNo-Wait-Release
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
      Gerrit-Change-Number: 730620
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Funda Secgin <fundas...@gmail.com>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-CC: Alan Donovan <adon...@google.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: Michael Matloob <mat...@google.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Comment-Date: Fri, 19 Dec 2025 22:07:24 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      open
      diffy

      Gerrit Bot (Gerrit)

      unread,
      Dec 23, 2025, 12:28:22 AM12/23/25
      to Heath Dutton, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Funda Secgin and Michael Matloob

      Gerrit Bot uploaded new patchset

      Gerrit Bot uploaded patch set #2 to this change.
      Following approvals got outdated and were removed:
      • Code-Review: +1 by Funda Secgin
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Funda Secgin
      • Michael Matloob
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedNo-Wait-Release
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
      Gerrit-Change-Number: 730620
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Funda Secgin <fundas...@gmail.com>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-CC: Alan Donovan <adon...@google.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: Heath Dutton <heath...@gmail.com>
      Gerrit-CC: Michael Matloob <mat...@google.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Attention: Funda Secgin <fundas...@gmail.com>
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      Mar 16, 2026, 2:45:18 PM (2 days ago) Mar 16
      to Heath Dutton, Gerrit Bot, goph...@pubsubhelper.golang.org, Alan Donovan, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Funda Secgin

      Michael Matloob added 1 comment

      Patchset-level comments
      File-level comment, Patchset 2 (Latest):
      Michael Matloob . resolved

      Please add me back when this is ready for another round of review.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Funda Secgin
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
        Gerrit-Change-Number: 730620
        Gerrit-PatchSet: 2
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Funda Secgin <fundas...@gmail.com>
        Gerrit-CC: Alan Donovan <adon...@google.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Heath Dutton <heath...@gmail.com>
        Gerrit-Attention: Funda Secgin <fundas...@gmail.com>
        Gerrit-Comment-Date: Mon, 16 Mar 2026 18:45:15 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        open
        diffy

        Gerrit Bot (Gerrit)

        unread,
        12:22 AM (14 hours ago) 12:22 AM
        to Heath Dutton, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
        Attention needed from Funda Secgin

        Gerrit Bot uploaded new patchset

        Gerrit Bot uploaded patch set #3 to this change.
        Open in Gerrit

        Related details

        Attention is currently required from:
        • Funda Secgin
        Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: newpatchset
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ief6d195bb932f0271214c45c56adaac56260d724
        Gerrit-Change-Number: 730620
        Gerrit-PatchSet: 3
        unsatisfied_requirement
        open
        diffy
        Reply all
        Reply to author
        Forward
        0 new messages