{"record":{"id":"21cf134765ce5ae6","repo":"golang/go","slug":"import-q-import-of-unknown-directory","errorCode":null,"errorMessage":"import %q: import of unknown directory","messagePattern":"import %q: import of unknown directory","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modindex/read.go","lineNumber":401,"sourceCode":"\tdefer unprotect(protect(), &err)\n\n\tctxt := (*Context)(&bctxt)\n\n\tp = &build.Package{}\n\n\tp.ImportPath = \".\"\n\tp.Dir = filepath.Join(rp.modroot, rp.dir)\n\n\tvar pkgerr error\n\tswitch ctxt.Compiler {\n\tcase \"gccgo\", \"gc\":\n\tdefault:\n\t\t// Save error for end of function.\n\t\tpkgerr = fmt.Errorf(\"import %q: unknown compiler %q\", p.Dir, ctxt.Compiler)\n\t}\n\n\tif p.Dir == \"\" {\n\t\treturn p, fmt.Errorf(\"import %q: import of unknown directory\", p.Dir)\n\t}\n\n\t// goroot and gopath\n\tinTestdata := func(sub string) bool {\n\t\tsub = filepath.ToSlash(sub)\n\t\treturn strings.Contains(sub, \"/testdata/\") || strings.HasSuffix(sub, \"/testdata\") || str.HasPathPrefix(sub, \"testdata\")\n\t}\n\tvar pkga string\n\tif !inTestdata(rp.dir) {\n\t\t// In build.go, p.Root should only be set in the non-local-import case, or in\n\t\t// GOROOT or GOPATH. Since module mode only calls Import with path set to \".\"\n\t\t// and the module index doesn't apply outside modules, the GOROOT case is\n\t\t// the only case where p.Root needs to be set.\n\t\tif ctxt.GOROOT != \"\" && str.HasFilePathPrefix(p.Dir, cfg.GOROOTsrc) && p.Dir != cfg.GOROOTsrc {\n\t\t\tp.Root = ctxt.GOROOT\n\t\t\tp.Goroot = true\n\t\t\tmodprefix := str.TrimFilePathPrefix(rp.modroot, cfg.GOROOTsrc)\n\t\t\tp.ImportPath = rp.dir","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modindex/read.go#L383-L419","documentation":"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.","triggerScenarios":"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.","commonSituations":"Tooling that constructs IndexPackage literals without going through Module.Package; memory corruption of the index structures; tests that forget to populate dir fields.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Ensure IndexPackage came from a populated Module.Package call.\nfunc validIndexPackage(rp *modindex.IndexPackage) bool {\n    // rp.Dir() returns the joined path; if empty, the struct is unusable.\n    return rp.Dir() != \"\"\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Acquire IndexPackage values only from (*Module).Package.","Do not construct IndexPackage literals in application code."],"tags":["module-index","internal","build-context"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}