hashicorp/terraform · error
Failed to initialize config loader: %s
Error message
Failed to initialize config loader: %s
What it means
Emitted by the `apply` command when `Meta.initConfigLoader()` fails to build a `configload.Loader` (internal/command/meta_config.go:465). The loader owns module reading, the configuration source bundle, and service hooks; apply cannot construct its operation request without it. The `%s` is the underlying error returned by `configload.NewLoader`, so it is the real root cause and must be read to diagnose.
Source
Thrown at internal/command/apply.go:307
// 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 *ApplyCommand) Help() string {
if c.Destroy {
return c.helpDestroy()
}
return c.helpApply()
}
func (c *ApplyCommand) Synopsis() string {
if c.Destroy {
return "Destroy previously-created infrastructure"
}View on GitHub (pinned to c9def3e214)
Solutions
- Read the wrapped `%s` error first — it names the exact failure (module dir, services, permissions).
- Run `terraform init` in the working directory to materialize `.terraform` and the modules directory before applying.
- Check read/write permissions on the working directory and the `TF_DATA_DIR` (default `.terraform`) path.
- If `.terraform` is corrupt or locked, remove it (`rm -rf .terraform`) and re-run `terraform init`.
- Confirm `TF_DATA_DIR` is not set to a read-only or non-existent parent.
Example fix
# before (apply in an uninitialized dir) terraform apply # Failed to initialize config loader: ... # after terraform init terraform apply
Defensive patterns
Strategy: validation
Validate before calling
// Before invoking the apply operation request builder, ensure the working
// directory has been initialized and the data dir is writable.
func canRunApply(workDir string) error {
dataDir := filepath.Join(workDir, os.Getenv("TF_DATA_DIR"))
if os.Getenv("TF_DATA_DIR") == "" {
dataDir = filepath.Join(workDir, ".terraform")
}
if fi, err := os.Stat(dataDir); err != nil {
return fmt.Errorf("terraform not initialized (run 'terraform init'): %w", err)
} else if !fi.IsDir() {
return fmt.Errorf("%s is not a directory", dataDir)
}
if err := os.MkdirAll(filepath.Join(dataDir, "modules"), 0o755); err != nil {
return fmt.Errorf("modules dir not writable: %w", err)
}
return nil
} Try / catch
// opReq, diags := applyCommand.Run(args...)
if diags.HasErrors() {
for _, d := range diags {
if strings.Contains(d.Description().Summary, "Failed to initialize config loader") {
// Surface the wrapped cause and tell the user to run 'terraform init'.
return fmt.Errorf("apply blocked: %s; run 'terraform init' first", d.Description().Detail)
}
}
} Prevention
- Always run `terraform init` before `apply` in CI and local workflows.
- Pin a clean working directory per run; do not reuse a partially-written `.terraform`.
- Surface the wrapped `%s` cause verbatim — it identifies the true failure.
- Gate apply behind a check that `.terraform/modules` exists and is writable.
When it happens
Trigger: Running `terraform apply` (or `terraform destroy`, which reuses this path) in a directory where `configload.NewLoader(&configload.Config{ModulesDir: m.modulesDir(), ...})` returns a non-nil error. This occurs when the loader cannot initialize its module registry/modules directory or cannot wire up the configured Services.
Common situations: Running `apply` before `terraform init` so the `.terraform` modules directory is missing/locked; filesystem permission errors on the working directory; a stale or half-written `.terraform` left by a crashed prior run; a non-standard `TF_DATA_DIR` pointing at an unwritable location.
Related errors
- error asking for approval: %w
- Failed to initialize config loader: %w
- Failed to initialize config loader: %s
- Failed to initialize config loader: %s
- Failed to initialize config loader: %s
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/3ee44bb1a995d5c1.
Report an issue: GitHub.