Go "import cycle not allowed" - Fix in CI
Go forbids import cycles between packages. When package A imports B and B imports A (directly or through a chain), the compiler cannot order the build and rejects it.
What this error means
A build fails with import cycle not allowed and a list of packages forming the loop. It usually appears after moving a type or function so two packages now depend on each other.
go
package example.com/app/a
imports example.com/app/b
imports example.com/app/a: import cycle not allowedCommon causes
Two packages depend on each other
A symbol was placed so that A needs B and B needs A, forming a direct cycle.
A transitive loop through a third package
The cycle runs through a chain (A -> B -> C -> A) rather than a direct pair.
How to fix it
Extract the shared piece
- Move the type or function both packages need into a third, lower-level package.
- Import that shared package from both, breaking the loop.
Go
// before: a <-> b
// after: a -> shared <- bInvert a dependency with an interface
- Define an interface in the lower package and have the higher package implement it, removing the back-reference.
Terminal
go build ./...How to prevent it
- Keep dependencies flowing in one direction (high-level to low-level).
- Place shared types in a neutral package both sides can import.
- Use interfaces to invert dependencies instead of importing back.
Related guides
Go "error loading module requirements" - Fix in CIFix Go "error loading module requirements" in CI - Go could not resolve the full module graph, often a bad re…
Go "internal error: package without types was imported" - Fix in CIFix Go "internal error: package without types was imported" in CI - usually a stale build cache or mismatched…
Go "package command-line-arguments is not a main package" - Fix in CIFix Go "package command-line-arguments is not a main package" in CI - go build/run got file paths that are no…