golang/go · error
import %q: import of unknown directory
Error message
import %q: import of unknown directory
What it means
Returned by IndexPackage.Import when p.Dir evaluates to an empty string. p.Dir is computed as filepath.Join(rp.modroot, rp.dir); an empty result implies both rp.modroot and rp.dir were empty, i.e. the IndexPackage was constructed without a usable module-root or relative-dir field. This is an internal-consistency failure rather than a user mistake.
Source
Thrown at src/cmd/go/internal/modindex/read.go:401
defer unprotect(protect(), &err)
ctxt := (*Context)(&bctxt)
p = &build.Package{}
p.ImportPath = "."
p.Dir = filepath.Join(rp.modroot, rp.dir)
var pkgerr error
switch ctxt.Compiler {
case "gccgo", "gc":
default:
// Save error for end of function.
pkgerr = fmt.Errorf("import %q: unknown compiler %q", p.Dir, ctxt.Compiler)
}
if p.Dir == "" {
return p, fmt.Errorf("import %q: import of unknown directory", p.Dir)
}
// goroot and gopath
inTestdata := func(sub string) bool {
sub = filepath.ToSlash(sub)
return strings.Contains(sub, "/testdata/") || strings.HasSuffix(sub, "/testdata") || str.HasPathPrefix(sub, "testdata")
}
var pkga string
if !inTestdata(rp.dir) {
// In build.go, p.Root should only be set in the non-local-import case, or in
// GOROOT or GOPATH. Since module mode only calls Import with path set to "."
// and the module index doesn't apply outside modules, the GOROOT case is
// the only case where p.Root needs to be set.
if ctxt.GOROOT != "" && str.HasFilePathPrefix(p.Dir, cfg.GOROOTsrc) && p.Dir != cfg.GOROOTsrc {
p.Root = ctxt.GOROOT
p.Goroot = true
modprefix := str.TrimFilePathPrefix(rp.modroot, cfg.GOROOTsrc)
p.ImportPath = rp.dirView on GitHub (pinned to b6b368adc5)
Solutions
- Obtain IndexPackage instances only through (*Module).Package(path), which sets modroot and dir from the index.
- If writing tests, populate rp.modroot and rp.dir explicitly.
- Clear and rebuild the cache (`go clean -modcache`) if the index appears malformed.
Defensive patterns
Strategy: validation
Validate before calling
// Ensure IndexPackage came from a populated Module.Package call.
func validIndexPackage(rp *modindex.IndexPackage) bool {
// rp.Dir() returns the joined path; if empty, the struct is unusable.
return rp.Dir() != ""
} Prevention
- Acquire IndexPackage values only from (*Module).Package.
- Do not construct IndexPackage literals in application code.
When it happens
Trigger: Importing an IndexPackage whose modroot/dir fields were never populated (e.g. a zero-value IndexPackage, or a synthetic test fixture). The check fires immediately after the compiler switch.
Common situations: Tooling that constructs IndexPackage literals without going through Module.Package; memory corruption of the index structures; tests that forget to populate dir fields.
Related errors
- import %q: unknown compiler %q
- invalid ASN.1 from SignASN1
- marshal error %v
- no clauses
- clause %q: expected colon
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/21cf134765ce5ae6.
Report an issue: GitHub.