golang/go · error

import lookup disabled by -mod=%s

Error message

import lookup disabled by -mod=%s

What it means

Returned (wrapped in ImportMissingError.QueryErr) when an import path cannot be resolved to a module because -mod=readonly or -mod=vendor is in effect and module lookup is disabled. Readonly forbids modifying go.mod to add a new dependency; vendor forbids network/cache lookups entirely. The error surfaces only when cfg.BuildModExplicit is true (the user passed -mod=... explicitly).

Source

Thrown at src/cmd/go/internal/modload/import.go:623

		//
		// Instead of trying QueryPattern, report an ImportMissingError immediately.
		return module.Version{}, &ImportMissingError{
			Path:                      path,
			isStd:                     true,
			modContainingCWD:          ld.MainModules.ModContainingCWD(),
			allowMissingModuleImports: ld.allowMissingModuleImports,
		}
	}

	if (cfg.BuildMod == "readonly" || cfg.BuildMod == "vendor") && !ld.allowMissingModuleImports {
		// In readonly mode, we can't write go.mod, so we shouldn't try to look up
		// the module. If readonly mode was enabled explicitly, include that in
		// the error message.
		// In vendor mode, we cannot use the network or module cache, so we
		// shouldn't try to look up the module
		var queryErr error
		if cfg.BuildModExplicit {
			queryErr = fmt.Errorf("import lookup disabled by -mod=%s", cfg.BuildMod)
		} else if cfg.BuildModReason != "" {
			queryErr = fmt.Errorf("import lookup disabled by -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)
		}
		return module.Version{}, &ImportMissingError{
			Path:                      path,
			QueryErr:                  queryErr,
			modContainingCWD:          ld.MainModules.ModContainingCWD(),
			allowMissingModuleImports: ld.allowMissingModuleImports,
		}
	}

	// Look up module containing the package, for addition to the build list.
	// Goal is to determine the module, download it to dir,
	// and return m, dir, ImportMissingError.
	fmt.Fprintf(os.Stderr, "go: finding module for package %s\n", path)

	mg, err := rs.Graph(ld, ctx)
	if err != nil {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run 'go get <importpath>@latest' (or the desired version) to add the missing module to go.mod, then rebuild.
  2. Switch to -mod=mod for that invocation: 'go build -mod=mod' to let go.mod be updated automatically.
  3. If vendoring, run 'go mod vendor' after adding the dependency so vendor/ and modules.txt include it.
  4. Check that the import path is spelled correctly and the module is publicly fetchable.

Example fix

// before
$ go build -mod=readonly ./...
// import lookup disabled by -mod=readonly

// after
$ go get golang.org/x/example@latest
$ go build -mod=readonly ./...
Defensive patterns

Strategy: validation

Validate before calling

// Before building, ensure every import is already in go.mod when you must use -mod=readonly.
// Run: go list -mod=readonly -e -deps ./... and check for ImportMissingError markers,
// or simply resolve deps ahead of time:
cmd := exec.Command("go", "get", "./...")
cmd.Env = append(os.Environ(), "GOFLAGS=-mod=mod")
if err := cmd.Run(); err != nil { return err }
// now safe to use -mod=readonly

Try / catch

out, err := exec.Command("go", "build", "-mod=readonly", "./...").CombinedOutput()
if err != nil && bytes.Contains(out, []byte("import lookup disabled by -mod=")) {
    // missing dep under readonly: run 'go get' then retry
    if gerr := exec.Command("go", "get", "./...").Run(); gerr != nil { return gerr }
    out, err = exec.Command("go", "build", "-mod=readonly", "./...").CombinedOutput()
}
return err

Prevention

When it happens

Trigger: Building/importing a package whose module is not in go.mod while -mod=readonly is set explicitly on the command line (go build -mod=readonly), and ld.allowMissingModuleImports is false. cfg.BuildModExplicit is true so the plain message form is chosen.

Common situations: Adding a new import in code then running 'go build -mod=readonly' before 'go get'; CI that hard-codes -mod=readonly for reproducibility; vendored projects where the vendor/modules.txt is missing the new module.

Related errors


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