hashicorp/terraform · error

Failed to initialize config loader

Error message

Failed to initialize config loader: %s

What it means

QueryCommand opReq construction calls c.initConfigLoader(); on failure the error is wrapped and the query operation request cannot be built. Identical pattern to plan/refresh but on the query command path. The loader is responsible for parsing config and preparing module installation state.

Solutions

  1. Run terraform validate / terraform fmt to catch HCL errors.
  2. Run terraform init to prepare the loader and modules.
  3. Run from the config root or use -chdir.
  4. Inspect the wrapped %s for the precise parse/load failure.

Example fix

# before: query fails on loader init
terraform query
# after
terraform init
terraform validate
terraform query
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check config loads before the query command.
if _, err := c.initConfigLoader(); err != nil {
    return fmt.Errorf("config loader failed; run 'terraform init' and 'terraform validate': %w", err)
}

Prevention

When it happens

Trigger: terraform query (the query/plan-with-policy command) is invoked; c.initConfigLoader() returns err — config parse failures, missing config dir, or module installer state issues.

Common situations: Running query with unparseable .tf files; outside the config dir; after a backend or module change without terraform init; missing files referenced by the configuration.

Related errors


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

Appendix: 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 d32a084675)