{"record":{"id":"80178c21bb3961d8","repo":"hashicorp/terraform","slug":"failed-to-initialize-config-loader-w","errorCode":null,"errorMessage":"Failed to initialize config loader: %w","messagePattern":"Failed to initialize config loader: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/modules.go","lineNumber":133,"sourceCode":"\n\t// Create a module reference resolver\n\tresolver := moduleref.NewResolver(internalManifest)\n\n\t// Crawl the Terraform config and find entries with references\n\tmanifestWithRef := resolver.Resolve(config)\n\n\t// Render the new manifest with references\n\treturn view.Display(*manifestWithRef)\n}\n\n// internalManifest will use the configuration loader to refresh and load the\n// internal manifest.\nfunc (c *ModulesCommand) internalManifest() (modsdir.Manifest, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\tloader, err := c.initConfigLoader()\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to initialize config loader: %w\", err))\n\t\treturn nil, diags\n\t}\n\n\tif err = loader.RefreshModules(); err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to refresh module manifest: %w\", err))\n\t\treturn nil, diags\n\t}\n\n\treturn loader.ModuleManifest(), diags\n}\n\nconst modulesCommandHelp = `\nUsage: terraform [global options] modules [options]\n\n  Prints out a list of all declared Terraform modules and their resolved versions\n  in a Terraform working directory.\n\nOptions:","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/modules.go#L115-L151","documentation":"Wrapped error from ModulesCommand.internalManifest when Meta.initConfigLoader() (configload.NewLoader) fails. The config loader is responsible for parsing .terraformrc/CLI config and setting up the module installer; failure here means Terraform cannot even begin to enumerate modules. The %w preserves the underlying cause (e.g. bad modules directory, services init failure).","triggerScenarios":"Running 'terraform modules' (or 'terraform modules -json') when configload.NewLoader returns an error — typically an inaccessible modules directory, a corrupt CLI config file, or a services/registry client initialization failure.","commonSituations":"TF_CLI_CONFIG_FILE points at a malformed or unreadable file; the modules dir (TF_DATA_DIR/.terraform/modules) has permission errors; running in a restricted filesystem/container where the loader cannot stat or create its working files.","solutions":["Inspect the wrapped error message for the specific cause (filesystem path, permission, or parse error).","Validate TF_CLI_CONFIG_FILE / ~/.terraformrc is valid HCL and readable.","Check the data directory (TF_DATA_DIR) exists and is writable; clear .terraform/modules if corrupted.","Run 'terraform modules' with TF_LOG=trace to surface the underlying NewLoader error."],"exampleFix":"// before: TF_CLI_CONFIG_FILE=/etc/tf/bad-config.tfrc  (malformed)\n// after: provide a valid CLI config or unset the var\n$ unset TF_CLI_CONFIG_FILE\n$ terraform modules","handlingStrategy":"validation","validationCode":"// Pre-flight: ensure CLI config and data dir are usable before invoking 'terraform modules'\npackage main\n\nfunc preflightConfigLoader(cliConfig, dataDir string) error {\n\tif cliConfig != \"\" {\n\t\tif _, err := os.ReadFile(cliConfig); err != nil {\n\t\t\treturn fmt.Errorf(\"TF_CLI_CONFIG_FILE unreadable: %w\", err)\n\t\t}\n\t}\n\tif info, err := os.Stat(dataDir); err == nil && !info.IsDir() {\n\t\treturn fmt.Errorf(\"data dir %s is not a directory\", dataDir)\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run 'terraform init' once before 'terraform modules' in fresh checkouts.","Keep TF_CLI_CONFIG_FILE pointing at version-controlled, valid HCL.","Make the data directory writable in CI runners."],"tags":["config-loader","modules","cli-config","initialization"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}