golang/go · error
file not found
Error message
file not found
What it means
Thrown by openPackage when a local import path's archive could not be found: PackageFile map is nil (no -packagefile overlay), and neither `<path>.a` nor `<path>.o` exists on disk. The compiler tries .a before .o so that a complete library archive is preferred over a lone object file.
Source
Thrown at src/cmd/compile/internal/noder/import.go:80
if islocalname(path) {
if base.Flag.NoLocalImports {
return nil, errors.New("local imports disallowed")
}
if base.Flag.Cfg.PackageFile != nil {
return os.Open(base.Flag.Cfg.PackageFile[path])
}
// try .a before .o. important for building libraries:
// if there is an array.o in the array.a library,
// want to find all of array.a, not just array.o.
if file, err := os.Open(fmt.Sprintf("%s.a", path)); err == nil {
return file, nil
}
if file, err := os.Open(fmt.Sprintf("%s.o", path)); err == nil {
return file, nil
}
return nil, errors.New("file not found")
}
// local imports should be canonicalized already.
// don't want to see "encoding/../encoding/base64"
// as different from "encoding/base64".
if q := pathpkg.Clean(path); q != path {
return nil, fmt.Errorf("non-canonical import path %q (should be %q)", path, q)
}
if base.Flag.Cfg.PackageFile != nil {
return os.Open(base.Flag.Cfg.PackageFile[path])
}
for _, dir := range base.Flag.Cfg.ImportDirs {
if file, err := os.Open(fmt.Sprintf("%s/%s.a", dir, path)); err == nil {
return file, nil
}
if file, err := os.Open(fmt.Sprintf("%s/%s.o", dir, path)); err == nil {View on GitHub (pinned to b6b368adc5)
Solutions
- Build the imported local package first so its .a/.o exists at the path the compiler searches.
- Run the compiler from the directory it expects (the source root), since local paths resolve relative to the compiler CWD.
- Use the -packagefile overlay or -I import search flags if coordinating a custom build pipeline.
- Switch to canonical module import paths to avoid CWD-relative resolution.
Example fix
// before: relative local import, no artifact present import "./foo" // after: build foo first, then depend on the produced archive go build ./foo && go build .
Defensive patterns
Strategy: validation
Validate before calling
// Confirm a local import target's archive exists before compiling.
import "os"
func localArchiveExists(path string) bool {
for _, suf := range []string{".a", ".o"} {
if _, err := os.Stat(path+suf); err == nil { return true }
}
return false
} Prevention
- Build dependencies in topological order so archives exist before dependents.
- Run the compiler from the directory it expects for relative imports.
- Prefer `go build` over raw `go tool compile` to get correct search coordination.
When it happens
Trigger: A source file imports a local path (./foo, ../bar) but the compiled archive/object file does not exist at the expected location relative to the compiler's working directory; PackageFile overlay is not configured.
Common situations: The imported local package was never compiled; the build was run from the wrong working directory; stale build artifacts were cleaned; the import path's case does not match the file system; missing -I/-o build coordination.
Related errors
- local imports disallowed
- import path cannot be absolute path
- 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/37e136cf20aa0061.
Report an issue: GitHub.