{"record":{"id":"07d3fb4c4d21b076","repo":"hashicorp/terraform","slug":"error-reading-s-as-a-statefile-w","errorCode":null,"errorMessage":"Error reading %s as a statefile: %w","messagePattern":"Error reading (.+?) as a statefile: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/show.go","lineNumber":392,"sourceCode":"\tif buildDiags.HasErrors() {\n\t\treturn nil, diags\n\t}\n\n\treturn config, diags\n}\n\n// getStateFromPath returns a statefile if the user-supplied path points to a statefile.\nfunc getStateFromPath(path string) (*statefile.File, error) {\n\tfile, err := os.Open(path)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"Error loading statefile: %w\", err)\n\t}\n\tdefer file.Close()\n\n\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","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/show.go#L374-L410","documentation":"Wrapped error from getStateFromPath when os.Open succeeded but statefile.Read(file) failed to parse the contents as a Terraform state file. This means the file exists and is readable but its bytes are not a valid state document (wrong magic, corrupt JSON, or it is actually a plan/other file). It confirms the path resolved to a file, just not a state file.","triggerScenarios":"Running 'terraform show <path>' where <path> is a plan file (binary or JSON plan bookmark), a random JSON/text file, a truncated state file, or a state file written by an incompatible Terraform version whose format statefile.Read rejects.","commonSituations":"Pointing 'terraform show' at a saved plan (should be handled by plan parser, but if that also failed this is the fallback error); state file truncated by a crashed run; manually concatenated/edited state JSON; version skew between writer and reader CLI.","solutions":["Confirm the file is a genuine state file: it should contain a top-level 'version' and 'serial' field in JSON.","If it is a plan file, ensure you are not corrupting it; use 'terraform show -json <plan>' appropriately.","Restore the state file from a backup if it is truncated/corrupt.","Generate the state file with a compatible Terraform version."],"exampleFix":"// before: 'terraform show ./backup.json' where backup.json is a plan export\n// after: point at a real state file\n$ terraform show ./terraform.tfstate","handlingStrategy":"type-guard","validationCode":"// Pre-flight: sniff the file to decide if it looks like a state file before passing to 'terraform show'\npackage main\n\nimport \"encoding/json\"\n\nfunc looksLikeStateFile(path string) error {\n\tb, err := os.ReadFile(path)\n\tif err != nil { return err }\n\tvar probe struct{ Version int `json:\"version\"`; Serial int `json:\"serial\"` }\n\tif err := json.Unmarshal(b, &probe); err != nil {\n\t\treturn fmt.Errorf(\"%s is not valid JSON state; not a state file\", path)\n\t}\n\tif probe.Version == 0 { return fmt.Errorf(\"%s missing state 'version' field\", path) }\n\treturn nil\n}","typeGuard":"// Guard: narrow a file path to a likely state file before use\nfunc isLikelyStateFile(path string) bool {\n\tb, err := os.ReadFile(path)\n\tif err != nil { return false }\n\tvar probe struct{ Version int `json:\"version\"` }\n\tif err := json.Unmarshal(b, &probe); err != nil { return false }\n\treturn probe.Version > 0\n}","tryCatchPattern":null,"preventionTips":["Don't pass plan files or arbitrary JSON to 'terraform show'.","Keep state backups intact; avoid manual edits that break JSON validity.","Generate state with a Terraform version compatible with the one reading it."],"tags":["show","statefile","parse","corruption"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}