Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: prevent import modules that may cause import cycles #44669

Closed
changkun opened this issue Feb 27, 2021 · 3 comments
Closed

cmd/go: prevent import modules that may cause import cycles #44669

changkun opened this issue Feb 27, 2021 · 3 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@changkun
Copy link
Member

Assume we have two modules: A (cyclemod) and B (cyclemod2). Go module does not complain about the following dependency relationship:

  • A@v1.0.0 depends on B@v2.0.0
  • B@v2.0.0 depends on A@v0.2.0
  • A@v0.2.0 depends on B@v0.1.0
  • B@v0.1.0 depends on A@v0.1.0
  • A@v0.1.0 has no dependency

However, it is possible in a version of A:

import "cyclemod2/v2" // or "cyclemod2"

and in B:

import "cyclemod"

This leads to:

package cyclemod
        cyclemod2/v2
        cyclemod: import cycle not allowed

The dependency cycle might happen at go mod tidy, but the import cycle is not detected. Maybe, Go module should either:

  1. prevent importing module that involves import cycle at import time instead of compile-time
  2. allow import "import cycle" for modules with different versions
@seankhliao
Copy link
Member

Are you sure it's not just a plain import cycle? From the info you gave only A@v1.0.0 and B@v2.0.0 would be selected for build

@bcmills
Copy link
Contributor

bcmills commented Mar 1, 2021

A module may contain many packages. Cycles in the module requirement graph often do not correspond to cycles in the package import graph.

The import cycle not allowed error message that you observed reports a cycle in the package graph, not the module graph. The fix for that error is to update the import statements in the .go source files, not the require directives in the go.mod files.

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 1, 2021
@changkun
Copy link
Member Author

changkun commented Mar 2, 2021

Thought import cycle might work using different versioned modules. Thanks for the explanation, clear and close...

@changkun changkun closed this as completed Mar 2, 2021
@golang golang locked and limited conversation to collaborators Mar 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants