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
- Run the command from the directory containing the root module (.tf files).
- Run terraform init first so module installation and the loader state are prepared.
- Fix any HCL syntax errors surfaced by the embedded %w error.
- 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
- Always run `terraform modules` after `terraform init`.
- Run Terraform commands from the configuration root or use -chdir.
- Validate configuration with terraform validate first.
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
- Failed to initialize config loader
- Failed to initialize config loader
- Failed to initialize config loader
- A managed resource address is required. Importing into a…
- at most 1 action can be invoked per operation
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)