{"record":{"id":"60186ea0de0218c0","repo":"golang/go","slug":"import-path-cannot-be-absolute-path","errorCode":null,"errorMessage":"import path cannot be absolute path","messagePattern":"import path cannot be absolute path","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/import.go","lineNumber":149,"sourceCode":"\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\n\t\t}\n\t\tpath = pathpkg.Join(prefix, path)\n\n\t\tif err := checkImportPath(path, true); err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\t}\n\n\treturn path, nil","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/import.go#L131-L167","documentation":"Thrown by resolveImportPath when the import path is local (per islocalname) AND begins with '/'. Absolute filesystem paths are not permitted as import paths because they break portability and caching; the compiler refuses to canonicalize an absolute local path.","triggerScenarios":"A source file contains `import \"/abs/path/to/pkg\"`. islocalname returns true (starts with '/'), then the `path[0] == '/'` check rejects it.","commonSituations":"Generated code or hand edits that insert an absolute path import; porting GOPATH-style code that used absolute paths; an import map misconfiguration that produces an absolute path.","solutions":["Replace the absolute import path with the canonical module import path.","If resolving relative to a -D directory, use a relative path (./ or ../) instead of an absolute one.","Ensure import-map rewriting does not produce leading-slash paths."],"exampleFix":"// before\nimport \"/home/user/proj/pkg\"\n// after\nimport \"example.com/myproject/pkg\"","handlingStrategy":"validation","validationCode":"// Reject absolute import paths in source/tooling.\nimport \"strings\"\nfunc isAbsoluteImport(p string) bool { return strings.HasPrefix(p, \"/\") }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use module-relative canonical paths, never filesystem-absolute ones.","Lint imports to forbid leading-slash paths.","Check import-map tooling does not rewrite paths to absolute form."],"tags":["compiler","imports","absolute-path","go-build"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}