Skip to content
Latchkey

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 allowed

Common 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

  1. Move the type or function both packages need into a third, lower-level package.
  2. Import that shared package from both, breaking the loop.
Go
// before: a <-> b
// after:  a -> shared <- b

Invert a dependency with an interface

  1. 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →