hashicorp/terraform · error
Failed to initialize config loader: %s
Error message
Failed to initialize config loader: %s
What it means
Wrapped error from RefreshCommand.operation when c.initConfigLoader() fails. Refresh updates state metadata against real resources and requires the config loader to parse the configuration (to know which resources to refresh). Failure here prevents the operation from being submitted. %s embeds the NewLoader error.
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 c9def3e214)
Solutions
- Read the embedded error to find the underlying loader cause.
- Repair or unset TF_CLI_CONFIG_FILE; ensure ~/.terraformrc is valid.
- Make the working/data directory writable.
- Run with TF_LOG=trace for the precise failure.
Example fix
// before $ terraform refresh Failed to initialize config loader: ... // after $ chmod -R u+w .terraform && terraform init && terraform refresh
Defensive patterns
Strategy: validation
Validate before calling
// Same preflight as plan/query: CLI config + data dir
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
- Run 'terraform init' before 'terraform refresh'.
- Prefer 'terraform apply -refresh-only' over standalone refresh where supported.
- Keep CLI config valid and the working/data directory writable.
When it happens
Trigger: Running 'terraform refresh' when configload.NewLoader errors — invalid CLI config, inaccessible modules directory, or services/registry init failure.
Common situations: Identical root causes to 727/729: unreadable TF_CLI_CONFIG_FILE, read-only .terraform dir in CI, network blocking services discovery, container lacks write access.
Related errors
- Failed to initialize config loader: %w
- Failed to initialize config loader: %s
- Failed to initialize config loader: %s
- Failed to initialize config loader: %s
- error loading plugin path: %s
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/d19e61b034945cd7.
Report an issue: GitHub.