{"record":{"id":"8de06fa186028408","repo":"hashicorp/terraform","slug":"invalid-syntax-w","errorCode":null,"errorMessage":"invalid syntax: %w","messagePattern":"invalid syntax: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/workdir/backend_state.go","lineNumber":82,"sourceCode":"// Returns an error if the content is not valid syntax, or if the file is\n// of an unsupported format version.\n//\n// This does not immediately decode the embedded backend config, and so\n// it's possible that a subsequent call to [BackendConfigState.Config] will\n// return further errors even if this call succeeds.\nfunc ParseBackendStateFile(src []byte) (*BackendStateFile, error) {\n\t// To avoid any weird collisions with as-yet-unknown future versions of\n\t// the format, we'll do a first pass of decoding just the \"version\"\n\t// property, and then decode the rest only if we find the version number\n\t// that we're expecting.\n\ttype VersionSniff struct {\n\t\tVersion   int    `json:\"version\"`\n\t\tTFVersion string `json:\"terraform_version,omitempty\"`\n\t}\n\tvar versionSniff VersionSniff\n\terr := json.Unmarshal(src, &versionSniff)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid syntax: %w\", err)\n\t}\n\tif versionSniff.Version == 0 {\n\t\t// This could either mean that it's explicitly \"version\": 0 or that\n\t\t// the version property is missing. We'll assume the latter here\n\t\t// because state snapshot version 0 was an encoding/gob binary format\n\t\t// rather than a JSON format and so it would be very weird for\n\t\t// that to show up in a JSON file.\n\t\treturn nil, fmt.Errorf(\"invalid syntax: no format version number\")\n\t}\n\tif versionSniff.Version != 3 {\n\t\treturn nil, fmt.Errorf(\"unsupported backend state version %d; you may need to use Terraform CLI v%s to work in this directory\", versionSniff.Version, versionSniff.TFVersion)\n\t}\n\n\t// If we get here then we can be sure that this file at least _thinks_\n\t// it's format version 3.\n\tvar stateFile BackendStateFile\n\terr = json.Unmarshal(src, &stateFile)\n\tif err != nil {","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/workdir/backend_state.go#L64-L100","documentation":"First-line parse failure in ParseBackendStateFile: json.Unmarshal of the version-sniff struct failed, meaning the backend state file (.terraform/terraform.tfstate) is not valid JSON at all. The %w carries the json decoder's offset/reason.","triggerScenarios":"Pointing Terraform at a backend state file that is empty, truncated, binary (e.g. legacy gob format), or contains a syntax error (trailing comma, unquoted key, BOM, etc.).","commonSituations":"File corrupted by a crashed write, edited by hand with a typo, replaced by a non-JSON file (e.g. a Terraform state snapshot or a log), or transferred with encoding damage (CRLF/BOM on Windows).","solutions":["Open .terraform/terraform.tfstate and check it parses as JSON (jq . < file).","Restore from version control or backup if corrupted.","If the file is empty/garbage, delete it and run `terraform init` to regenerate.","Re-save as UTF-8 without BOM and Unix line endings."],"exampleFix":"# diagnose\njq . .terraform/terraform.tfstate\n\n# regenerate if unrecoverable\nrm .terraform/terraform.tfstate\nterraform init","handlingStrategy":"validation","validationCode":"// pre-flight: confirm the file is valid JSON before handing it to the parser\nif !json.Valid(src) {\n    return nil, fmt.Errorf(\"backend state file is not valid JSON; re-run `terraform init`\")\n}","typeGuard":"func looksLikeBackendState(src []byte) bool {\n    var sniff struct{ Version int `json:\"version\"` }\n    return json.Unmarshal(src, &sniff) == nil && sniff.Version > 0\n}","tryCatchPattern":"f, err := ParseBackendStateFile(src)\nif err != nil {\n    if !json.Valid(src) {\n        return nil, errors.New(\"backend state file corrupt; delete it and re-run `terraform init`\")\n    }\n    return nil, err\n}","preventionTips":["Never hand-edit the backend state file.","Add .terraform/ to .gitignore and manage config via the backend block.","Snapshot the workspace before upgrades."],"tags":["backend","json-parse","corruption","init"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}