{"record":{"id":"bc4d21ca91c7e47f","repo":"hashicorp/terraform","slug":"error-loading-state-w","errorCode":null,"errorMessage":"error loading state: %w","messagePattern":"error loading state: %w","errorType":"console","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/local/backend_local.go#L35-L71","documentation":"Returned by Local.localRun when b.StateMgr(op.Workspace) returns diagnostics with errors. This is the earliest state-loading failure: the state manager for the requested workspace could not even be constructed/opened before any refresh happens. The wrapped value is the aggregate diagnostics error.","triggerScenarios":"StateMgr fails to set up the state file manager for the workspace — e.g. the backend's StateMgr returns an error because the state path is invalid, the workspace doesn't exist, or a delegated backend (b.Backend.StateMgr) errored.","commonSituations":"Referencing a workspace that doesn't exist for a backend that requires it; corrupt or inaccessible local state path; misconfigured delegated backend (e.g. remote/cloud) that fails to resolve the workspace.","solutions":["Inspect the wrapped diagnostics error for the specific cause (workspace not found, path error, backend auth failure).","Verify the workspace exists with terraform workspace list; create it with terraform workspace new if missing.","Check the backend configuration and credentials if a delegated backend is involved."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Ensure the workspace exists before the operation.\nws, diags := b.Workspaces()\nif diags.HasErrors() { return diags }\nif !contains(ws, op.Workspace) { return fmt.Errorf(\"workspace %q not found\", op.Workspace) }","typeGuard":null,"tryCatchPattern":"s, sDiags := b.StateMgr(op.Workspace)\nif sDiags.HasErrors() {\n    return fmt.Errorf(\"error loading state: %w\", sDiags.Err())\n}","preventionTips":["Pre-create workspaces with terraform workspace new before referencing them.","Validate backend config and credentials before running operations.","Run terraform workspace list in CI to confirm the target workspace exists."],"tags":["state","backend","workspace","terraform"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}