hashicorp/terraform · error

Failed to initialize config loader

Error message

Failed to initialize config loader: %s

What it means

Thrown by terraform apply when c.initConfigLoader() returns a non-nil error, meaning Terraform could not set up the configuration loader needed to parse .tf files. The error is wrapped and appended to diagnostics as a config loader initialization failure. This is an early-stage failure that occurs before any configuration parsing or validation.

Solutions

  1. Verify the working directory exists and is readable: ls -la <directory>
  2. Re-run terraform init to rebuild the .terraform directory if it is missing or corrupted
  3. Check filesystem permissions for the user running terraform
  4. Confirm you are running from the correct directory (use -chdir if needed)
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify the working directory is accessible before running apply
func checkWorkdir(path string) error {
    info, err := os.Stat(path)
    if err != nil {
        return fmt.Errorf("working directory %s inaccessible: %w", path, err)
    }
    if !info.IsDir() {
        return fmt.Errorf("%s is not a directory", path)
    }
    // Check write access for .terraform dir
    tfDir := filepath.Join(path, ".terraform")
    if _, err := os.Stat(tfDir); err != nil && !os.IsNotExist(err) {
        return fmt.Errorf(".terraform directory issue: %w", err)
    }
    return nil
}

Try / catch

// Wrap terraform apply and surface config loader errors clearly
cmd := exec.Command("terraform", "apply", "-auto-approve")
output, err := cmd.CombinedOutput()
if err != nil {
    if strings.Contains(string(output), "Failed to initialize config loader") {
        return fmt.Errorf("config loader init failed — check directory and permissions: %s", output)
    }
    return err
}

Prevention

When it happens

Trigger: c.initConfigLoader() returns an error. This happens when the working directory is inaccessible, required directories do not exist, or there is a filesystem permission issue preventing Terraform from preparing its internal loader state.

Common situations: Running terraform apply in a directory without read/write permissions; missing or corrupted .terraform directory; running from a path that does not exist; filesystem errors on network-mounted volumes (NFS, SMB); running as a different user without appropriate access.

Related errors


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

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