golang/go · error
Config.Importer not installed
Error message
Config.Importer not installed
What it means
The types2 Config.Importer field (exposed as go/types.Config.Importer) provides the import resolution mechanism for the type checker. When the checker encounters an import statement and Config.Importer is nil, this error is returned. Without an importer, the type checker cannot resolve imported packages.
Source
Thrown at src/cmd/compile/internal/types2/resolver.go:149
key := importKey{path, dir}
imp := check.impMap[key]
if imp != nil {
return imp
}
// no package yet => import it
if path == "C" && (check.conf.FakeImportC || check.conf.go115UsesCgo) {
if check.conf.FakeImportC && check.conf.go115UsesCgo {
check.error(pos, BadImportPath, "cannot use FakeImportC and go115UsesCgo together")
}
imp = NewPackage("C", "C")
imp.fake = true // package scope is not populated
imp.cgo = check.conf.go115UsesCgo
} else {
// ordinary import
var err error
if importer := check.conf.Importer; importer == nil {
err = fmt.Errorf("Config.Importer not installed")
} else if importerFrom, ok := importer.(ImporterFrom); ok {
imp, err = importerFrom.ImportFrom(path, dir, 0)
if imp == nil && err == nil {
err = fmt.Errorf("Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error", path, dir)
}
} else {
imp, err = importer.Import(path)
if imp == nil && err == nil {
err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
}
}
// make sure we have a valid package name
// (errors here can only happen through manipulation of packages after creation)
if err == nil && imp != nil && (imp.name == "_" || imp.name == "") {
err = fmt.Errorf("invalid package name: %q", imp.name)
imp = nil // create fake package below
}
if err != nil {View on GitHub (pinned to b6b368adc5)
Solutions
- Set conf.Importer = importer.Default() before calling Check
- For source-level imports: conf.Importer = importer.ForCompiler(fset, "source", nil)
- Use golang.org/x/tools/go/packages which handles import resolution automatically
- If checking code with no imports, this error will not fire — but set the importer anyway for safety
Example fix
// before
conf := types.Config{}
_, err := conf.Check(pkgName, fset, files, &info)
// after
import "go/importer"
conf := types.Config{Importer: importer.Default()}
_, err := conf.Check(pkgName, fset, files, &info) Defensive patterns
Strategy: validation
Validate before calling
// Ensure Config.Importer is set before type-checking
import (
"go/importer"
"go/types"
)
func newTypeChecker(fset *token.FileSet) *types.Checker {
conf := types.Config{
Importer: importer.ForCompiler(fset, "source", nil),
}
return conf.NewChecker(fset)
}
// Or validate before use:
func validateConfig(conf *types.Config) error {
if conf.Importer == nil {
return fmt.Errorf("types.Config.Importer must be set when checking code with imports")
}
return nil
} Type guard
// Type guard / assertion for Importer capability
func hasImporter(conf *types.Config) bool {
return conf != nil && conf.Importer != nil
}
// Check if the importer supports ImportFrom (newer interface)
func supportsImportFrom(conf *types.Config) bool {
_, ok := conf.Importer.(types.ImporterFrom)
return ok
} Prevention
- Always set Config.Importer = importer.Default() or importer.ForCompiler() before calling Check
- Use golang.org/x/tools/go/packages instead of manual Config setup — it handles imports automatically
- Add a nil check for Importer as part of your tool's initialization validation
- Document that the Config requires an Importer in your tool's API documentation
When it happens
Trigger: Calling checker.Check() or checker.Files() with a types.Config where Importer is nil, when the source code being checked contains import statements for non-C packages. The error is set on the import, and the type checker continues with a fake package.
Common situations: Static analysis tools, linters, or code generators that construct a types.Config without setting the Importer. Forgetting to call importer.Default() or importer.ForCompiler(). Test setups that only check self-contained code but later add imports.
Related errors
- empty string
- invalid character %#U
- Config.Importer.ImportFrom(%s, %s, 0) returned nil but no er
- total length of command line and environment variables excee
- cipher: the nonce can't have zero length
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/eae45d1bbd00cf06.
Report an issue: GitHub.