hashicorp/terraform · error

Failed to initialize config loader

Error message

Failed to initialize config loader: %w

What it means

ModulesCommand.internalManifest calls c.initConfigLoader() to obtain the config loader used to refresh and read the module manifest. If initialization fails (the config tree cannot be parsed, the module path is invalid, or required helpers are missing) the error is appended to diagnostics and returned, aborting `terraform modules`.

Solutions

  1. Run the command from the directory containing the root module (.tf files).
  2. Run terraform init first so module installation and the loader state are prepared.
  3. Fix any HCL syntax errors surfaced by the embedded %w error.
  4. Pass an explicit -chdir to point at the configuration root if your cwd differs.

Example fix

# before: run from wrong directory
terraform modules
# after
cd infra/live && terraform modules
# or
terraform -chdir=infra/live modules
Defensive patterns

Strategy: validation

Validate before calling

// Ensure cwd is a config root before invoking `terraform modules`.
if _, err := os.Stat("main.tf"); err != nil {
    return fmt.Errorf("no .tf files in cwd; pass -chdir or cd into the config root")
}

Prevention

When it happens

Trigger: Running `terraform modules`; initConfigLoader() returns a non-nil error, which propagates from parser/loader construction, module installation bookkeeping, or a missing configuration directory.

Common situations: Invoking `terraform modules` outside a Terraform configuration directory; configuration has HCL syntax errors; required_providers/module blocks reference unreachable sources; permissions on the config dir prevent reading.

Related errors


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

Appendix: source

Thrown at internal/command/modules.go:133

	// Create a module reference resolver
	resolver := moduleref.NewResolver(internalManifest)

	// Crawl the Terraform config and find entries with references
	manifestWithRef := resolver.Resolve(config)

	// Render the new manifest with references
	return view.Display(*manifestWithRef)
}

// internalManifest will use the configuration loader to refresh and load the
// internal manifest.
func (c *ModulesCommand) internalManifest() (modsdir.Manifest, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics

	loader, err := c.initConfigLoader()
	if err != nil {
		diags = diags.Append(fmt.Errorf("Failed to initialize config loader: %w", err))
		return nil, diags
	}

	if err = loader.RefreshModules(); err != nil {
		diags = diags.Append(fmt.Errorf("Failed to refresh module manifest: %w", err))
		return nil, diags
	}

	return loader.ModuleManifest(), diags
}

const modulesCommandHelp = `
Usage: terraform [global options] modules [options]

  Prints out a list of all declared Terraform modules and their resolved versions
  in a Terraform working directory.

Options:

View on GitHub (pinned to d32a084675)