{"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/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote/backend_context.go#L27-L63","documentation":"In LocalRun(), b.StateMgr(op.Workspace) returned diagnostics containing errors. StateMgr is responsible for reading/creating the workspace and the version check, so this wraps any failure from that stage (e.g. workspace read/create failure or the version-mismatch guard) into a single 'error loading state' diagnostic. The %w preserves the underlying cause for unwrapping.","triggerScenarios":"LocalRun() calls StateMgr(); the returned sDiags.HasErrors() is true. The underlying cause is whatever StateMgr returned — typically error 400 (Failed to retrieve workspace), 401 (Error creating workspace), or 402 (version mismatch).","commonSituations":"Any local-execution operation (`terraform plan`/`apply` when the workspace is in local execution mode or forceLocal) where the workspace can't be read/created or the Terraform version conflicts; the surface message is generic, so unwrap to find the real cause.","solutions":["Unwrap the %w error (errors.Unwrap / diags.Err()) to reveal the underlying StateMgr failure and address that directly.","Fix the root cause per that underlying error: token/org/name (workspace read), permissions (create), or version alignment.","Re-run `terraform init` to re-resolve the backend and re-attempt the StateMgr setup."],"exampleFix":"// before - generic 'error loading state' hides the cause\n// after - in Go tooling, unwrap to report specifics:\nif err := diags.Err(); err != nil {\n  var target error = err\n  for errors.Unwrap(target) != nil {\n    target = errors.Unwrap(target)\n  }\n  log.Printf(\"underlying state load failure: %v\", target)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func hasStateMgrErrors(d tfdiags.Diagnostics) bool {\n    return d.HasErrors()\n}","tryCatchPattern":"stateMgr, sDiags := b.StateMgr(op.Workspace)\nif sDiags.HasErrors() {\n    return fmt.Errorf(\"error loading state: %w\", sDiags.Err())\n}","preventionTips":["Resolve the underlying StateMgr cause (workspace read/create/version) — unwrap %w.","Run `terraform init` to re-validate the backend after config/credential changes.","Keep backend config and credentials current to avoid read/create failures."],"tags":["state","workspace","local-run","diagnostics"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}