hashicorp/terraform · error

Failed to initialize config loader

Error message

Failed to initialize config loader: %s

What it means

RefreshCommand opReq construction calls c.initConfigLoader(); on failure the error is wrapped and the refresh operation request cannot be built. Same pattern as plan/query. Note refresh is deprecated in favor of 'terraform apply -refresh-only'.

Solutions

  1. Run terraform fmt and terraform validate before refresh.
  2. Run terraform init to ensure the loader is ready.
  3. Prefer 'terraform apply -refresh-only' as refresh is deprecated.
  4. Inspect the inner %s for the exact loader failure location.

Example fix

# before: deprecated refresh fails on parse error
terraform refresh
# after
terraform fmt
terraform apply -refresh-only
Defensive patterns

Strategy: validation

Validate before calling

// Surface parse errors before refresh.
if exit := runTerraform("validate"); exit != 0 {
    return fmt.Errorf("fix validate errors before refreshing")
}

Prevention

When it happens

Trigger: terraform refresh is invoked; c.initConfigLoader() returns err — HCL parse errors, unreadable config directory, or inconsistent module installer state.

Common situations: Editing .tf files introducing syntax errors then running refresh; running refresh outside the config directory; partial init; removed or renamed files still referenced.

Related errors


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

Appendix: source

Thrown at internal/command/refresh.go:161

	// EXPERIMENTAL: maybe enable deferred actions
	if c.AllowExperimentalFeatures {
		opReq.DeferralAllowed = args.DeferralAllowed
	} else if args.DeferralAllowed {
		// Belated flag parse error, since we don't know about experiments
		// support at actual parse time.
		diags = diags.Append(tfdiags.Sourceless(
			tfdiags.Error,
			"Failed to parse command-line flags",
			"The -allow-deferral flag is only valid in experimental builds of Terraform.",
		))
		return nil, diags
	}

	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
}

func (c *RefreshCommand) Help() string {
	helpText := `
Usage: terraform [global options] refresh [options]

  Update the state file of your infrastructure with metadata that matches
  the physical resources they are tracking.

  This will not modify your infrastructure, but it can modify your
  state file to update metadata. This metadata might cause new changes
  to occur when you generate a plan or call apply next.

Options:

View on GitHub (pinned to d32a084675)