{"record":{"id":"89760568c3931df4","repo":"hashicorp/terraform","slug":"error-loading-state-w-897605","errorCode":null,"errorMessage":"error loading state: %w","messagePattern":"error loading state: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote/backend_context.go","lineNumber":45,"sourceCode":"func (b *Remote) 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] backend/remote: 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] backend/remote: 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] backend/remote: reading remote state for workspace %q\", remoteWorkspaceName)\n\tif err := stateMgr.RefreshState(); err != nil {","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/remote/backend_context.go#L27-L63","documentation":"Thrown early in the context-building path when b.StateMgr(op.Workspace) returns diagnostics with errors. StateMgr constructs the remote state manager (which sets up the workspace + state blob access); failure here means the state manager could not even be initialized, before any state read attempt.","triggerScenarios":"b.StateMgr(workspace) returns sDiags with HasErrors(). Caused by the underlying remote state manager failing to resolve the workspace/storage, token issues surfaced during manager construction, or internal state-manager setup errors.","commonSituations":"Workspace inaccessible at manager-construction time; token lacks state read scope; transient error building the state client; backend misconfiguration exposed during StateMgr init.","solutions":["Inspect the wrapped sDiags.Err() for the precise construction failure.","Verify token, organization, and workspace/prefix are correct (same checks as the org/workspace errors).","Retry 'terraform init' then the operation - transient construction errors often clear.","Re-run 'terraform login' if the wrapped error indicates auth."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// On StateMgr diagnostics errors, surface the wrapped diagnostics and retry transient ones.\nsm, sDiags := b.StateMgr(op.Workspace)\nif sDiags.HasErrors() {\n    if isTransient(sDiags.Err()) { /* retry after backoff */ }\n    return fmt.Errorf(\"state manager init failed: %w\", sDiags.Err())\n}","preventionTips":["Run 'terraform init' to surface state-manager setup issues early.","Verify token/workspace access before running operations that need state.","Retry transient construction errors with backoff."],"tags":["backend","remote-backend","state","state-manager","init","go"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}