{"record":{"id":"e5dbe364d63fadcd","repo":"golang/go","slug":"cannot-import-main","errorCode":null,"errorMessage":"cannot import \"main\"","messagePattern":"cannot import \"main\"","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/import.go","lineNumber":133,"sourceCode":"\t\tif file, err := os.Open(fmt.Sprintf(\"%s/pkg/%s_%s%s/%s.a\", buildcfg.GOROOT, buildcfg.GOOS, buildcfg.GOARCH, suffix, path)); err == nil {\n\t\t\treturn file, nil\n\t\t}\n\t\tif file, err := os.Open(fmt.Sprintf(\"%s/pkg/%s_%s%s/%s.o\", buildcfg.GOROOT, buildcfg.GOOS, buildcfg.GOARCH, suffix, path)); err == nil {\n\t\t\treturn file, nil\n\t\t}\n\t}\n\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","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/import.go#L115-L151","documentation":"Thrown by resolveImportPath when the import path is literally \"main\". The path \"main\" is reserved to identify the program's main package, just as \"math\" identifies the standard math package; it cannot be imported as a dependency.","triggerScenarios":"A source file contains `import \"main\"` (or an import map resolves a path to \"main\"). resolveImportPath checks path==\"main\" before any further resolution.","commonSituations":"A typo or auto-generated import referencing a package whose import path was set to \"main\"; tooling/codegen producing `import \"main\"`; confusion between package name `main` (allowed as a name) and import path \"main\" (reserved).","solutions":["Remove or rename the import; you cannot import another package whose import path is \"main\".","If you intended to share code, move that code into a package with a non-reserved import path.","Audit code generators or import maps that may rewrite a path to \"main\"."],"exampleFix":"// before\nimport \"main\"\n// after (move shared code into a real package)\nimport \"example.com/myproject/appmain\"","handlingStrategy":"validation","validationCode":"// Reject attempts to import the reserved path \"main\".\nfunc isReservedImport(p string) bool { return p == \"main\" }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never name a reusable library's import path \"main\"; main is reserved for the program entry.","Distinguish package NAME main (a package named main) from import PATH \"main\" (reserved).","Audit generated imports for the literal \"main\" path."],"tags":["compiler","imports","reserved","main-package"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}