Go

Import Cycle Not Allowed Fix

An import cycle is a loop in the package graph: a imports b, b imports a, and neither package can be built first. Go forbids the loop outright and prints the chain that closes it. The repairs do not negotiate with the rule - they remove one edge of the loop, either by moving the shared code into a package both sides can import, or by having one side accept a function value instead of importing back.

Written November 28, 2026 · Tested with Go 1.27.3 · 12 min read

By the end of this guide
  • The rule underneath
  • The first failure: two packages importing each other
  • The second failure: a package importing itself
  • First fix: move the shared code to a third package

Written November 28, 2026

Tested with Go 1.27.3

What this guide is

The error is import cycle not allowed. It fires at build time, before any code runs, when the imports between packages form a loop: one package imports a second, and the second - directly, or through a chain of others - imports the first one back. The compiler prints the full chain that closes the loop, ending with the words import cycle not allowed, so the message itself is the map of what to break.

This guide reproduces the cycle in its two common shapes - two packages importing each other under a main package, and a package importing itself - and then gives the two repairs. The first repair moves the code both packages need into a third package that imports neither of them, so both import the third package instead of each other. The second repair breaks one direction of the loop: the package that used to import back instead accepts a function value from its caller, and the import in that direction disappears. A short final section separates the error that sounds similar but is different: an import path no module provides.

Each state of the module below is complete: a go.mod file plus the source files shown, each in its own directory, with the transcripts run in the module root. Where a later state reuses a file unchanged, the text says so; every other file is printed in full for the state it belongs to.

The rule underneath

The Go specification's import declarations section states the rule in one sentence: "It is illegal for a package to import itself, directly or indirectly". An import declaration declares a dependency between the importing and the imported package, and dependencies have to be buildable in some order - the imported package first. A loop has no first package, so there is no order in which the loop's packages can be built, and the program is rejected as a whole. The specification's program initialization section makes the same point from the runtime side: the importing of packages, by construction, guarantees that there can be no cyclic initialization dependencies.

Two consequences follow. The cycle is a property of the package graph, not of any one line: no single import in the loop is wrong by itself, and the error names the whole chain rather than pointing at a faulty declaration. And the only repairs are graph repairs - remove an edge, or move the shared code so the edge is no longer needed. Renaming, reordering, or splitting files inside the same packages changes nothing, because the loop is between packages, not files.

The first failure: two packages importing each other

A main package imports package a. Package a imports package b for a helper. Package b imports package a back, for a helper of its own. Each import is reasonable on its own; together they close a loop.

The file go.mod:

Output
module cycletest

go 1.27.3

The file main.go:

Go
package main

import (
    "fmt"

    "cycletest/a"
)

func main() {
    fmt.Println(a.Greeting())
}

The file a/a.go:

Go
package a

import "cycletest/b"

func Greeting() string {
    return "a says " + b.Name()
}

func Name() string {
    return "a"
}

The file b/b.go:

Go
package b

import "cycletest/a"

func Name() string {
    return "b via " + a.Name()
}
Terminal
$ go build -trimpath ./...
package cycletest
    imports cycletest/a from main.go
    imports cycletest/b from a.go
    imports cycletest/a from b.go: import cycle not allowed

Read the chain from the top. The first line names the package being built, and each indented line is one import hop, with the file that declares it: main.go imports cycletest/a, a.go imports cycletest/b, and b.go imports cycletest/a - which is already in the chain, so the chain closes and the last line carries the verdict. The chain is the diagnosis: whichever import completes the loop is the one a repair has to remove or reroute. Note that main is part of the printed chain but not part of the loop - the loop is a to b to a, and it would fail the same way if some other package imported a.

The second failure: a package importing itself

The direct form of the same rule is a package that imports its own path. Replace the module above with this smaller one - the same go.mod, a main package that imports package c, and a package c that imports itself:

The file main.go:

Go
package main

import (
    "fmt"

    "cycletest/c"
)

func main() {
    fmt.Println(c.Name())
}

The file c/c.go:

Go
package c

import "cycletest/c"

func Name() string {
    return "c " + c.Name()
}
Terminal
$ go build -trimpath ./...
package cycletest
    imports cycletest/c from main.go
    imports cycletest/c from c.go: import cycle not allowed

The wording is identical, and the chain is one hop shorter: the second line already names the package the chain started from. A self-import usually arrives by accident - a file moved between packages keeps its old import, or an editor's automatic import adds the package's own path - rather than by design, since a package already has direct access to everything it declares and never needs to import itself to reach it. Deleting the self-import is the whole fix for this shape; the two repairs below are for the two-package loop, where both imports exist because each side genuinely uses something from the other.

First fix: move the shared code to a third package

Most two-package cycles exist because both packages use one small piece of shared code, and each reaches for it in the other. The repair gives that piece its own package. A module can consist of multiple packages, each in its own directory, as the module layout guide describes - so a new shared package costs one directory, and because it imports neither a nor b, both of them can import it without closing any loop.

Here the shared piece is a Label helper both packages use to format their names. In this state a and b no longer import each other at all; each imports shared, and main imports both. The go.mod file is unchanged from the first failure.

The file main.go:

Go
package main

import (
    "fmt"

    "cycletest/a"
    "cycletest/b"
)

func main() {
    fmt.Println(a.Greeting())
    fmt.Println(b.Greeting())
}

The file shared/shared.go:

Go
package shared

func Label(name string) string {
    return "label:" + name
}

The file a/a.go:

Go
package a

import "cycletest/shared"

func Greeting() string {
    return shared.Label("a")
}

The file b/b.go:

Go
package b

import "cycletest/shared"

func Greeting() string {
    return shared.Label("b")
}
Terminal
$ go build -trimpath -o app.bin .
$ ./app.bin; echo "exit status $?"
label:a
label:b
exit status 0

The import graph is now a fan-in: main imports a and b, a and b each import shared, and shared imports nothing from this module, so a build order exists - shared first, then a and b, then main. Choose this fix when the thing the two packages share is data or a self-contained helper: a type both sides use, a formatting or validation function, a set of constants. The shared package should hold only what both sides genuinely need; moving a whole package's worth of code into it just relocates the tangle.

Second fix: pass a function value instead of importing back

Sometimes one direction of the loop needs very little from the other side - here, package b needs only a's name, one string produced by one function. In that case the import itself is more than the need: b can declare that it takes the name as a function value, supplied by whoever calls it, and never import a at all. Package a still imports b, and passes its own Name function in.

In this state go.mod and main.go are unchanged from the first failure. Only the two package files change.

The file a/a.go:

Go
package a

import "cycletest/b"

func Greeting() string {
    return "a says " + b.Describe(Name)
}

func Name() string {
    return "a"
}

The file b/b.go:

Go
package b

func Describe(nameOfA func() string) string {
    return "b via " + nameOfA()
}
Terminal
$ go build -trimpath -o app.bin .
$ ./app.bin; echo "exit status $?"
a says b via a
exit status 0

The output is the same sentence the cyclic version was written to produce - a says b via a - with one import fewer. b.Describe now takes a func() string parameter, and a passes its own Name function as that value, so the call that used to cross the loop still happens; it just travels through a parameter instead of an import. An interface with the needed method, passed the same way, is the same repair for a need that spans several functions. Choose this fix when one side's need is narrow and behavioral - a callback, a lookup, a single operation - and no shared type has to live somewhere both sides can name. When both sides need the same type, the third-package fix is the one that fits, because a type passed back and forth still has to be declared in exactly one package.

Neighboring error: an import no module provides

A mistyped import path fails differently. In a module containing only the go.mod file above and this main.go, the import names a path outside the module:

The file main.go:

Go
package main

import (
    "fmt"

    "example.com/aa"
)

func main() {
    fmt.Println(aa.Greeting())
}
Terminal
$ go build -trimpath ./...
main.go:6:2: no required module provides package example.com/aa; to add it:
    go get example.com/aa

There is no chain and no cycle verdict, because the build never gets as far as ordering packages: the named package cannot be found in the module, in the standard library, or in any module the go.mod file requires, and the message says so, with the command that would add it. Read the two errors by their shape. A chain of imports ... from ... lines ending in import cycle not allowed means every package in the chain was found and the loop between them is the problem. A single line naming one file and one import path, saying no required module provides it, means that path itself is wrong or the requirement is missing - a different repair, covered by the neighboring guide linked below.

Decision summary

  • The build prints a chain of imports ... from ... lines ending in import cycle not allowed: the packages named in the chain import each other in a loop - remove or reroute the import that closes the chain.
  • The chain is two lines long and the same package appears in both: a package imports itself - delete the self-import; a package never needs it.
  • Both packages in the loop use the same helper, type, or constants: move that shared code into a third package that imports neither of them, and have both import the third package instead of each other.
  • One side of the loop needs only a call or two from the other: that side accepts a function value (or an interface) as a parameter instead of importing, and the caller passes its own function in.
  • The message instead names one file and says no required module provides the package: the import path is mistyped or the module requirement is missing - that is not a cycle, and no graph repair applies.

Checklist

  • The import that closes the loop, as printed at the end of the chain, is the one the repair removes or reroutes - not an import earlier in the chain.
  • No package in the module imports itself, directly or through any chain of other packages.
  • Any new shared package imports neither of the packages that use it; if it imports one of them, the loop is still there.
  • A function value or interface used to break a direction is a parameter of the function that needs it, supplied by the caller - the importing package is not referenced by name anywhere in the package that used to import it.
  • The repaired module builds with go build ./... and the resulting program runs and prints what it printed before the cycle was introduced, or what it was written to print.
  • A no required module provides package message is treated as a path or requirement problem, not as a cycle.

Sources

  • The Go Programming Language Specification - the Packages and Import declarations sections: an import declaration declares a dependency relation between the importing and imported package, and "It is illegal for a package to import itself, directly or indirectly"; the Program initialization section adds that the importing of packages, by construction, guarantees that there can be no cyclic initialization dependencies - the rule both failures break and both fixes restore.
  • Go documentation, Organizing a Go module - the module layout the first fix uses: a module can consist of multiple importable packages, each package has its own directory, and sub-packages are imported by the module path plus the directory - so the shared code can live in its own package that both sides import.

Keep Exploring