{"record":{"id":"3ee44bb1a995d5c1","repo":"hashicorp/terraform","slug":"failed-to-initialize-config-loader-s","errorCode":null,"errorMessage":"Failed to initialize config loader: %s","messagePattern":"Failed to initialize config loader: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/apply.go","lineNumber":307,"sourceCode":"\n\t// EXPERIMENTAL: maybe enable deferred actions\n\tif c.AllowExperimentalFeatures {\n\t\topReq.DeferralAllowed = args.DeferralAllowed\n\t} else if args.DeferralAllowed {\n\t\t// Belated flag parse error, since we don't know about experiments\n\t\t// support at actual parse time.\n\t\tdiags = diags.Append(tfdiags.Sourceless(\n\t\t\ttfdiags.Error,\n\t\t\t\"Failed to parse command-line flags\",\n\t\t\t\"The -allow-deferral flag is only valid in experimental builds of Terraform.\",\n\t\t))\n\t\treturn nil, diags\n\t}\n\n\tvar err error\n\topReq.ConfigLoader, err = c.initConfigLoader()\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to initialize config loader: %s\", err))\n\t\treturn nil, diags\n\t}\n\n\treturn opReq, diags\n}\n\nfunc (c *ApplyCommand) Help() string {\n\tif c.Destroy {\n\t\treturn c.helpDestroy()\n\t}\n\n\treturn c.helpApply()\n}\n\nfunc (c *ApplyCommand) Synopsis() string {\n\tif c.Destroy {\n\t\treturn \"Destroy previously-created infrastructure\"\n\t}","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/apply.go#L289-L325","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before (apply in an uninitialized dir)\nterraform apply\n# Failed to initialize config loader: ...\n\n# after\nterraform init\nterraform apply","handlingStrategy":"validation","validationCode":"// Before invoking the apply operation request builder, ensure the working\n// directory has been initialized and the data dir is writable.\nfunc canRunApply(workDir string) error {\n    dataDir := filepath.Join(workDir, os.Getenv(\"TF_DATA_DIR\"))\n    if os.Getenv(\"TF_DATA_DIR\") == \"\" {\n        dataDir = filepath.Join(workDir, \".terraform\")\n    }\n    if fi, err := os.Stat(dataDir); err != nil {\n        return fmt.Errorf(\"terraform not initialized (run 'terraform init'): %w\", err)\n    } else if !fi.IsDir() {\n        return fmt.Errorf(\"%s is not a directory\", dataDir)\n    }\n    if err := os.MkdirAll(filepath.Join(dataDir, \"modules\"), 0o755); err != nil {\n        return fmt.Errorf(\"modules dir not writable: %w\", err)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// opReq, diags := applyCommand.Run(args...)\nif diags.HasErrors() {\n    for _, d := range diags {\n        if strings.Contains(d.Description().Summary, \"Failed to initialize config loader\") {\n            // Surface the wrapped cause and tell the user to run 'terraform init'.\n            return fmt.Errorf(\"apply blocked: %s; run 'terraform init' first\", d.Description().Detail)\n        }\n    }\n}","preventionTips":["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."],"tags":["terraform","apply","config-loader","initialization","modules-dir"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}