golang/go · error
import %q while compiling that package (import cycle)
Error message
import %q while compiling that package (import cycle)
What it means
Returned by the noder import resolver when an import path equals the package currently being compiled (base.Ctxt.Pkgpath). Importing yourself directly is a self-referential import cycle; the compiler rejects it outright before resolution proceeds.
Source
Thrown at src/cmd/compile/internal/noder/import.go:140
return nil, errors.New("file not found")
}
// resolveImportPath resolves an import path as it appears in a Go
// source file to the package's full path.
func resolveImportPath(path string) (string, error) {
// The package name main is no longer reserved,
// but we reserve the import path "main" to identify
// the main package, just as we reserve the import
// path "math" to identify the standard math package.
if path == "main" {
return "", errors.New("cannot import \"main\"")
}
if base.Ctxt.Pkgpath == "" {
panic("missing pkgpath")
}
if path == base.Ctxt.Pkgpath {
return "", fmt.Errorf("import %q while compiling that package (import cycle)", path)
}
if mapped, ok := base.Flag.Cfg.ImportMap[path]; ok {
path = mapped
}
if islocalname(path) {
if path[0] == '/' {
return "", errors.New("import path cannot be absolute path")
}
prefix := base.Flag.D
if prefix == "" {
// Questionable, but when -D isn't specified, historically we
// resolve local import paths relative to the directory the
// compiler's current directory, not the respective source
// file's directory.
prefix = base.Ctxt.PathnameView on GitHub (pinned to b6b368adc5)
Solutions
- Remove the import statement that imports the package you are currently compiling; refer to its declarations directly instead.
- If you intended to import a subpackage, use the full subpackage path (example.com/foo/sub) rather than the parent path.
- Run goimports/gofmt to clean stray self-imports.
Example fix
// before (file lives in package example.com/foo) import ( "example.com/foo" ) // after // (remove the self-import; use local declarations)
Defensive patterns
Strategy: validation
Validate before calling
// Lint for self-imports before compiling.
for _, im := range file.Imports {
if im.Path.Value == fmt.Sprintf("%q", pkgPath) {
return fmt.Errorf("file in %s imports itself", pkgPath)
}
} Prevention
- Run goimports/goimports-reviser to drop unused or self-referential imports.
- After renaming a package, grep the module for stale imports of the old path.
When it happens
Trigger: A Go source file in package P contains an import statement whose path is exactly P (e.g. a file in package example.com/foo with import "example.com/foo"). The check path == base.Ctxt.Pkgpath fires.
Common situations: Accidental self-import while refactoring; tooling that auto-inserts imports; a package whose import path was renamed/restructured and source files still reference the old path that now resolves to itself.
Related errors
- can only use path@version syntax with 'go get' and 'go insta
- mixing of meta and non-meta packages is not allowed
- %s: %v
- import %q: %v
- marshal error %v
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/2446c1c1386647cf.
Report an issue: GitHub.