{"record":{"id":"2446c1c1386647cf","repo":"golang/go","slug":"import-q-while-compiling-that-package-import-cyc","errorCode":null,"errorMessage":"import %q while compiling that package (import cycle)","messagePattern":"import %q while compiling that package \\(import cycle\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/import.go","lineNumber":140,"sourceCode":"\treturn nil, errors.New(\"file not found\")\n}\n\n// resolveImportPath resolves an import path as it appears in a Go\n// source file to the package's full path.\nfunc resolveImportPath(path string) (string, error) {\n\t// The package name main is no longer reserved,\n\t// but we reserve the import path \"main\" to identify\n\t// the main package, just as we reserve the import\n\t// path \"math\" to identify the standard math package.\n\tif path == \"main\" {\n\t\treturn \"\", errors.New(\"cannot import \\\"main\\\"\")\n\t}\n\n\tif base.Ctxt.Pkgpath == \"\" {\n\t\tpanic(\"missing pkgpath\")\n\t}\n\tif path == base.Ctxt.Pkgpath {\n\t\treturn \"\", fmt.Errorf(\"import %q while compiling that package (import cycle)\", path)\n\t}\n\n\tif mapped, ok := base.Flag.Cfg.ImportMap[path]; ok {\n\t\tpath = mapped\n\t}\n\n\tif islocalname(path) {\n\t\tif path[0] == '/' {\n\t\t\treturn \"\", errors.New(\"import path cannot be absolute path\")\n\t\t}\n\n\t\tprefix := base.Flag.D\n\t\tif prefix == \"\" {\n\t\t\t// Questionable, but when -D isn't specified, historically we\n\t\t\t// resolve local import paths relative to the directory the\n\t\t\t// compiler's current directory, not the respective source\n\t\t\t// file's directory.\n\t\t\tprefix = base.Ctxt.Pathname","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/import.go#L122-L158","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (file lives in package example.com/foo)\n import (\n   \"example.com/foo\"\n )\n// after\n // (remove the self-import; use local declarations)","handlingStrategy":"validation","validationCode":"// Lint for self-imports before compiling.\nfor _, im := range file.Imports {\n    if im.Path.Value == fmt.Sprintf(\"%q\", pkgPath) {\n        return fmt.Errorf(\"file in %s imports itself\", pkgPath)\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["go-compiler","import-cycle","noder","packages"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}