golang/go · error

can't resolve import %s

Error message

can't resolve import %s

What it means

Thrown by the custom types.Importer closure inside makeTypesImporter during type-checking. For every import in the package's source, the exporter must translate the import path to a package path via cfg.ImportMap. If the import path is not a key in ImportMap, the importer cannot resolve it and fails. ImportMap is populated by `go list -export` and must list every import the package references.

Source

Thrown at src/cmd/export/main.go:136

		return nil, err
	}
	cfg := new(config)
	if err := json.Unmarshal(data, cfg); err != nil {
		return nil, fmt.Errorf("can't decode input %s: %v", filename, err)
	}
	if len(cfg.GoFiles) == 0 {
		return nil, fmt.Errorf("no files in package %s", cfg.ImportPath)
	}
	return cfg, nil
}

func makeTypesImporter(cfg *config, fset *token.FileSet) types.Importer {
	imports := make(map[string]*types.Package)
	imports["unsafe"] = types.Unsafe
	return importerFunc(func(importPath string) (*types.Package, error) {
		pkgPath, ok := cfg.ImportMap[importPath]
		if !ok {
			return nil, fmt.Errorf("can't resolve import %s", importPath)
		}
		// Check for cache hit.
		if pkg, ok := imports[pkgPath]; ok && pkg.Complete() {
			return pkg, nil
		}
		// Miss.
		cfgPath, ok := cfg.PackageFile[pkgPath]
		if !ok {
			return nil, fmt.Errorf("can't resolve export %s", pkgPath)
		}
		f, err := os.Open(cfgPath)
		if err != nil {
			return nil, err
		}
		defer f.Close()
		return gcexportdata.Read(f, fset, imports, pkgPath)
	})
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Regenerate the export config with a fresh `go list -export` so ImportMap reflects all current imports.
  2. Never hand-edit generated .cfg files; treat them as build artifacts.
  3. Keep GOOS/GOARCH/GOFLAGS/tags identical between the `go list -export` step and any later export step.
  4. Verify ImportMap coverage: `go list -export -json <pkg>` and check ImportMap contains each imported path.
Defensive patterns

Strategy: validation

Validate before calling

// Ensure every import referenced by the package is in ImportMap.
for _, f := range cfg.GoFiles {
    // (parse f's imports, then:)
    for _, imp := range importsOf(f) {
        if _, ok := cfg.ImportMap[imp]; !ok {
            return fmt.Errorf("import %s missing from ImportMap; rerun go list -export", imp)
        }
    }
}

Prevention

When it happens

Trigger: A source file imports a path that does not appear as a key in cfg.ImportMap. This happens when the config is stale relative to the source (a new import was added after the config was generated), or when a cgo/conditional import is active during type-checking but was not captured by `go list -export`.

Common situations: Editing source to add a new import then reusing a previously generated .cfg; build tags/GOOS/GOARCH differing between the `go list` run and the export run; manually trimming ImportMap; vendoring changes that rename import paths.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/144b904cc7146141. Report an issue: GitHub.