golang/go · error
import path cannot be absolute path
Error message
import path cannot be absolute path
What it means
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.
Source
Thrown at src/cmd/compile/internal/noder/import.go:149
// 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.Pathname
}
path = pathpkg.Join(prefix, path)
if err := checkImportPath(path, true); err != nil {
return "", err
}
}
return path, nilView on GitHub (pinned to b6b368adc5)
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.
Example fix
// before import "/home/user/proj/pkg" // after import "example.com/myproject/pkg"
Defensive patterns
Strategy: validation
Validate before calling
// Reject absolute import paths in source/tooling.
import "strings"
func isAbsoluteImport(p string) bool { return strings.HasPrefix(p, "/") } Prevention
- 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.
When it happens
Trigger: A source file contains `import "/abs/path/to/pkg"`. islocalname returns true (starts with '/'), then the `path[0] == '/'` check rejects it.
Common situations: 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.
Related errors
- local imports disallowed
- file not found
- cannot import "main"
- import path is empty
- import path contains NUL
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/60186ea0de0218c0.
Report an issue: GitHub.