{"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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/cloud/backend_context.go#L26-L62","documentation":"Pre-operation setup calls b.StateMgr(op.Workspace); if constructing the state manager itself emits diagnostic errors, the first one is wrapped as 'error loading state'. This is about building/initializing the remote state manager, not yet reading state—the next step (RefreshState) has its own error.","triggerScenarios":"b.StateMgr(workspace) returns sDiags.HasErrors(): backend cannot construct the state manager for the workspace—workspace lookup failure inside the state manager, bad backend configuration, locked-state manager init issues, or schema errors in the state config.","commonSituations":"Backend block misconfigured (missing credentials, wrong workspace name); state manager cannot resolve the remote workspace; partial backend config after a refactor; bucket/workspace naming changed.","solutions":["Re-run `terraform init` to validate and refresh backend configuration.","Inspect the wrapped diagnostic for the underlying cause (workspace lookup, auth, schema).","Confirm cloud block fields (hostname, organization, workspace name) are correct.","Verify the token has read access on the workspace's state."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"func backendConfigured(b *Cloud) error {\n    if b.Organization == \"\" { return fmt.Errorf(\"cloud block missing organization\") }\n    if b.Hostname == \"\" { return fmt.Errorf(\"cloud block missing hostname\") }\n    if b.client == nil { return fmt.Errorf(\"cloud client uninitialized; run terraform init\") }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run `terraform init` to validate and store backend configuration.","Keep cloud block fields (hostname, organization, workspace) consistent across envs.","Verify the token has read access on the workspace's state.","Re-init after any backend block refactor."],"tags":["tfe","hcp","cloud-backend","state","init"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}