hashicorp/terraform · error

Failed to initialize config loader: %s

Error message

Failed to initialize config loader: %s

What it means

Wrapped error from QueryCommand.operation when c.initConfigLoader() fails. The query command (experimental, runs a speculative plan-like evaluation) needs the config loader to parse configuration and query files before executing. Failure aborts before the operation is built. %s embeds the NewLoader error.

Source

Thrown at internal/command/query.go:205

	generateConfigOut string,
	policyPaths []string,
) (*backendrun.Operation, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics

	// Build the operation
	opReq := c.Operation(be, viewType)
	opReq.Hooks = view.Hooks()
	opReq.ConfigDir = "."
	opReq.Type = backendrun.OperationTypePlan
	opReq.GenerateConfigOut = generateConfigOut
	opReq.View = view.Operation()
	opReq.Query = true
	opReq.PolicyPaths = policyPaths

	var err error
	opReq.ConfigLoader, err = c.initConfigLoader()
	if err != nil {
		diags = diags.Append(fmt.Errorf("Failed to initialize config loader: %s", err))
		return nil, diags
	}

	return opReq, diags
}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Inspect the embedded error for the specific loader failure.
  2. Validate/repair TF_CLI_CONFIG_FILE and ~/.terraformrc.
  3. Ensure the data directory and working directory are writable.
  4. Use TF_LOG=trace to locate the failing init step.

Example fix

// before
$ terraform query
Failed to initialize config loader: ...
// after
$ unset TF_CLI_CONFIG_FILE
$ terraform init && terraform query
Defensive patterns

Strategy: validation

Validate before calling

// Reuse the same preflight as plan: ensure CLI config + data dir are usable
package main

func preflightConfigLoader(cliConfig, dataDir string) error {
	if cliConfig != "" {
		if _, err := os.ReadFile(cliConfig); err != nil {
			return fmt.Errorf("TF_CLI_CONFIG_FILE unreadable: %w", err)
		}
	}
	if err := os.MkdirAll(dataDir, 0o755); err != nil {
		return fmt.Errorf("data dir not writable: %w", err)
	}
	return nil
}

Prevention

When it happens

Trigger: Running 'terraform query' (experimental) when configload.NewLoader errors — invalid CLI config, inaccessible modules directory, or services init failure. Same root causes as the plan/refresh loader errors.

Common situations: Same as 727/730: bad TF_CLI_CONFIG_FILE, read-only data directory, network/proxy blocking services discovery, container filesystem restrictions.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/8dc6c4137de447d7. Report an issue: GitHub.