{"record":{"id":"bc4d21ca91c7e47f","repo":"hashicorp/terraform","slug":"error-loading-state-w","errorCode":null,"errorMessage":"error loading state: %w","messagePattern":"error loading state: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/local/backend_local.go","lineNumber":53,"sourceCode":"\t// seems bad but we're preserving it for now until we have time to\n\t// properly design this API, vs. just preserving whatever it currently\n\t// happens to do.\n\top.Type = backendrun.OperationTypeInvalid\n\n\top.StateLocker = op.StateLocker.WithContext(ctx)\n\n\tlr, _, stateMgr, diags := b.localRun(ctx, op)\n\treturn lr, stateMgr, diags\n}\n\nfunc (b *Local) localRun(ctx context.Context, op *backendrun.Operation) (*backendrun.LocalRun, *configload.Snapshot, statemgr.Full, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\t// Get the latest state.\n\tlog.Printf(\"[TRACE] backend/local: requesting state manager for workspace %q\", op.Workspace)\n\ts, sDiags := b.StateMgr(op.Workspace)\n\tif sDiags.HasErrors() {\n\t\tdiags = diags.Append(fmt.Errorf(\"error loading state: %w\", sDiags.Err()))\n\t\treturn nil, nil, nil, diags\n\t}\n\tlog.Printf(\"[TRACE] backend/local: requesting state lock for workspace %q\", op.Workspace)\n\tif diags := op.StateLocker.Lock(s, op.Type.String()); diags.HasErrors() {\n\t\treturn nil, nil, nil, diags\n\t}\n\n\tdefer func() {\n\t\t// If we're returning with errors, and thus not producing a valid\n\t\t// context, we'll want to avoid leaving the workspace locked.\n\t\tif diags.HasErrors() {\n\t\t\tdiags = diags.Append(op.StateLocker.Unlock())\n\t\t}\n\t}()\n\n\tlog.Printf(\"[TRACE] backend/local: reading remote state for workspace %q\", op.Workspace)\n\tif err := s.RefreshState(); err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"error loading state: %w\", err))","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/local/backend_local.go#L35-L71","documentation":"Thrown by Local.localRun when b.StateMgr(op.Workspace) returns diagnostics with errors. StateMgr is responsible for creating/opening the state manager for the named workspace, so this error means Terraform could not even obtain a handle to the state before trying to read it.","triggerScenarios":"b.StateMgr fails inside createState (cannot create the state file path) or, when the Local backend wraps another backend, the wrapped backend's StateMgr returns errors. HasErrors() is true on the returned sDiags, and the whole diagnostics error is wrapped with %w.","commonSituations":"The state directory does not exist and cannot be created (permission denied on parent), the workspace name is invalid, or a remote backend wrapped behind Local is unreachable. Also seen when TF_DATA_DIR points to a read-only location.","solutions":["Inspect the wrapped sDiags.Err() for the real cause (it is included via %w).","Ensure the state path parent directory exists and is writable: `mkdir -p .terraform/tfstate` and `chmod u+w` on TF_DATA_DIR.","Verify the workspace name is valid (no path separators, not empty).","If a remote backend is wrapped behind Local, fix the remote connection (creds, endpoint) before retrying."],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"// Verify the state path is usable before invoking the local run.\nfunc statePathUsable(dir string) error {\n    if err := os.MkdirAll(dir, 0o755); err != nil {\n        return fmt.Errorf(\"cannot create state dir %s: %w\", dir, err)\n    }\n    probe := filepath.Join(dir, \".writeprobe\")\n    if err := os.WriteFile(probe, []byte{}, 0o644); err != nil {\n        return fmt.Errorf(\"state dir %s not writable: %w\", dir, err)\n    }\n    _ = os.Remove(probe)\n    return nil\n}","typeGuard":"null","tryCatchPattern":"_, stateMgr, diags := b.LocalRun(ctx, op)\nif diags.HasErrors() {\n    if strings.Contains(diags.Err().Error(), \"error loading state\") {\n        // surface a hint about state path / permissions\n    }\n    return diags\n}","preventionTips":["Ensure TF_DATA_DIR and the state workspace dir are writable by the terraform process.","Validate workspace names before passing them in (no path separators).","When wrapping a remote backend behind Local, verify remote connectivity in a preflight check."],"tags":["local-backend","state","state-manager","io"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}