{"record":{"id":"b24a68b24f0b030e","repo":"hashicorp/terraform","slug":"failed-to-load-state-w-b24a68","errorCode":null,"errorMessage":"Failed to load state: %w","messagePattern":"Failed to load state: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/show.go","lineNumber":407,"sourceCode":"\tvar stateFile *statefile.File\n\tstateFile, err = statefile.Read(file)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"Error reading %s as a statefile: %w\", path, err)\n\t}\n\treturn stateFile, nil\n}\n\n// getStateFromBackend returns the State for the current workspace, if available.\nfunc getStateFromBackend(b backend.Backend, workspace string) (*statefile.File, error) {\n\t// Get the state store for the given workspace\n\tstateStore, sDiags := b.StateMgr(workspace)\n\tif sDiags.HasErrors() {\n\t\treturn nil, fmt.Errorf(\"Failed to load state manager: %w\", sDiags.Err())\n\t}\n\n\t// Refresh the state store with the latest state snapshot from persistent storage\n\tif err := stateStore.RefreshState(); err != nil {\n\t\treturn nil, fmt.Errorf(\"Failed to load state: %w\", err)\n\t}\n\n\t// Get the latest state snapshot and return it\n\tstateFile := statemgr.Export(stateStore)\n\treturn stateFile, nil\n}\n","sourceCodeStart":389,"sourceCodeEnd":414,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/show.go#L389-L414","documentation":"Wrapped error from getStateFromBackend when stateStore.RefreshState() fails after the state manager was successfully created. RefreshState pulls the latest state snapshot from persistent storage (S3 object GET, cloud API call, local file read). Failure means the manager exists but could not read/refresh the actual state bytes — typically a transport or storage-layer error.","triggerScenarios":"Running 'terraform show' with no path where StateMgr succeeded but RefreshState errored — network failure reading the remote state object, lock acquisition failure, object deleted between manager init and refresh, or local state file read error.","commonSituations":"Transient network error hitting S3/cloud mid-command; state lock held by another process; state object deleted out-of-band after init; local state file removed or became unreadable; IAM/credential change between init and refresh.","solutions":["Retry the command after confirming network connectivity to the backend.","Check for a stale state lock and clear it if safe ('terraform force-unlock').","Verify the state object still exists in the backend storage.","Re-run 'terraform init' if credentials/region changed."],"exampleFix":"// before: transient read failure on remote state\n$ terraform show  # Failed to load state: ...\n// after\n$ terraform force-unlock <lock-id>   # if locked\n$ terraform show                      # retry","handlingStrategy":"retry","validationCode":"// Pre-flight: for remote backends, a quick connectivity probe before 'terraform show'\npackage main\n\nfunc preflightBackendConnectivity(endpoint string) error {\n\tclient := http.Client{Timeout: 5 * time.Second}\n\tresp, err := client.Get(endpoint)\n\tif err != nil { return fmt.Errorf(\"backend unreachable: %w\", err) }\n\tresp.Body.Close()\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"// Wrap 'terraform show' with bounded retry on transient state-load failures\nfunc showWithRetry(max int) error {\n\tvar last error\n\tfor i := 0; i < max; i++ {\n\t\tif err := runTerraformShow(); err == nil { return nil } else { last = err }\n\t\ttime.Sleep(time.Duration(i*i) * time.Second)\n\t}\n\treturn last\n}","preventionTips":["Retry transient remote-state read failures with backoff.","Clear stale locks with 'terraform force-unlock' when safe.","Monitor backend storage health (bucket, cloud API) to catch outages early."],"tags":["show","state","backend","refresh","network"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}