golang/go · error

import %q: unknown compiler %q

Error message

import %q: unknown compiler %q

What it means

Returned (deferred to end of Import) by IndexPackage.Import when ctxt.Compiler is neither 'gc' nor 'gccgo'. The build.Context.Compiler field selects which compiler's conventions the index should follow; an unknown value means the index cannot decide import paths, archive names, or install roots, so the import is rejected.

Source

Thrown at src/cmd/go/internal/modindex/read.go:397

var installgorootAll = godebug.New("installgoroot").Value() == "all"

// Import is the equivalent of build.Import given the information in Module.
func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *build.Package, err error) {
	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 {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Set context.Compiler to 'gc' (standard) or 'gccgo' before calling Import.
  2. For non-standard compilers, do not use the module index path; fall back to go/build.Import directly.
  3. Check for typo'd compiler names when constructing the build context.

Example fix

// before
ctx := build.Default
ctx.Compiler = "gcc"
p, err := idxPkg.Import(ctx, 0)
// after
ctx.Compiler = "gccgo"
Defensive patterns

Strategy: validation

Validate before calling

// Validate Compiler before Import.
func supportedCompiler(c string) bool {
    return c == "gc" || c == "gccgo"
}

// usage
ctx := build.Default
if !supportedCompiler(ctx.Compiler) {
    return fmt.Errorf("unsupported compiler %q; use gc or gccgo", ctx.Compiler)
}
pkg, err := idxPkg.Import(ctx, 0)

Prevention

When it happens

Trigger: Constructing a custom build.Context with an experimental or typo'd Compiler string (e.g. 'gcc', 'tinygo', 'llgo') and passing it to IndexPackage.Import. The error is stored in pkgerr and surfaced after Import finishes its other work.

Common situations: Third-party tooling sets context.Compiler to a non-standard value; user typos 'gcc' for 'gccgo'; integrating the module index with an unsupported Go compiler fork.

Related errors


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