{"record":{"id":"9920b08024a453ed","repo":"hashicorp/terraform","slug":"error-loading-state-w-9920b0","errorCode":null,"errorMessage":"error loading state: %w","messagePattern":"error loading state: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloud/backend_context.go","lineNumber":44,"sourceCode":"func (b *Cloud) LocalRun(ctx context.Context, op *backendrun.Operation) (*backendrun.LocalRun, statemgr.Full, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\tret := &backendrun.LocalRun{\n\t\tPlanOpts: &terraform.PlanOpts{\n\t\t\tMode:    op.PlanMode,\n\t\t\tTargets: op.Targets,\n\t\t},\n\t}\n\n\top.StateLocker = op.StateLocker.WithContext(ctx)\n\n\t// Get the remote workspace name.\n\tremoteWorkspaceName := b.getRemoteWorkspaceName(op.Workspace)\n\n\t// Get the latest state.\n\tlog.Printf(\"[TRACE] cloud: requesting state manager for workspace %q\", remoteWorkspaceName)\n\tstateMgr, 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, diags\n\t}\n\n\tlog.Printf(\"[TRACE] cloud: requesting state lock for workspace %q\", remoteWorkspaceName)\n\tif diags := op.StateLocker.Lock(stateMgr, op.Type.String()); diags.HasErrors() {\n\t\treturn 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 remote 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] cloud: reading remote state for workspace %q\", remoteWorkspaceName)\n\tif err := stateMgr.RefreshState(); err != nil {","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/cloud/backend_context.go#L26-L62","documentation":"In LocalRun (backend_context.go:42-46), the call to b.StateMgr(op.Workspace) returned diagnostics with errors. StateMgr is responsible for constructing the remote state manager (which involves fetching the workspace, locking setup, etc.); when it fails to build the manager, the wrapped error propagates. This is a state-infrastructure failure, not a data-content failure.","triggerScenarios":"b.StateMgr(op.Workspace) at backend_context.go:42 returns sDiags with HasErrors(). Occurs during a local-run path (forcing ops locally on a cloud-configured workspace) when the state manager could not be constructed.","commonSituations":"Workspace not found / no access (deeper stack frames). Locking preconditions unmet. Token/API issues surfacing through StateMgr. Cloud block misconfiguration that prevents resolving the workspace.","solutions":["Examine the wrapped diagnostic (sDiags.Err()) for the root cause; it usually reveals workspace-access or API errors.","Verify the cloud block organization/workspace/tags resolve correctly (see fetchWorkspace errors 484/485).","Re-run `terraform init` to reconfigure the backend and refresh credentials.","Confirm the token has read + lock permissions on the target workspace."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-flight: construct the state manager separately and report.\nsm, diags := b.StateMgr(ws)\nif diags.HasErrors() { return diags }","typeGuard":null,"tryCatchPattern":"// Surface the wrapped root cause for StateMgr failures.\nif sDiags.HasErrors() {\n    return structuredStateError(\"manager-construction\", sDiags.Err())\n}","preventionTips":["Verify workspace access (name/org/token) before local runs.","Run `terraform init` after any cloud block change.","Grant the token read+lock permissions on the workspace."],"tags":["terraform","state","cloud-backend","local-run"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}