{"record":{"id":"8de06fa186028408","repo":"hashicorp/terraform","slug":"invalid-syntax-w","errorCode":null,"errorMessage":"invalid syntax: %w","messagePattern":"invalid syntax: %w","errorType":"validation","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/workdir/backend_state.go#L64-L100","documentation":"Thrown by ParseBackendStateFile during the first-pass JSON decode of the backend state file (.terraform/terraform.tfstate backend metadata). json.Unmarshal of just the version field failed, meaning the file content is not valid JSON at all. The %w is the json error (syntax error, truncated file, etc.).","triggerScenarios":"Any command that reads the backend state file when that file is not valid JSON: truncated by a crash, binary/corrupt content, manually edited and broken, or written by an incompatible tool.","commonSituations":"Terraform process killed mid-write leaving a partial file; file synced incompletely from network storage; editor saved it as text with BOM/CRLF issues; another tool overwrote it; disk corruption.","solutions":["Restore from the backup file (.terraform/terraform.tfstate.backup) if present.","If the backend is remote, delete the corrupt local backend metadata and re-run `terraform init` to regenerate it.","Validate the file is well-formed JSON with a linter before re-running.","Re-run `terraform init -reconfigure` to rebuild backend state from config."],"exampleFix":"# before: corrupt .terraform/terraform.tfstate\n# invalid syntax: invalid character '\\x00' looking for beginning of value\n\n# after: restore from backup, or regenerate\ncp .terraform/terraform.tfstate.backup .terraform/terraform.tfstate\nterraform init","handlingStrategy":"validation","validationCode":"// before parsing, validate the backend state file is valid JSON\nfunc isValidJSON(path string) error {\n    raw, err := os.ReadFile(path)\n    if err != nil { return err }\n    var probe map[string]json.RawMessage\n    return json.Unmarshal(raw, &probe)\n}","typeGuard":"// type guard: confirm the file is JSON before parsing as backend state\nfunc looksLikeBackendState(raw []byte) bool {\n    var probe struct{ Version int `json:\"version\"` }\n    return json.Unmarshal(raw, &probe) == nil\n}","tryCatchPattern":null,"preventionTips":["Never hand-edit the backend state file.","Restore from .backup if a file is truncated by a crash.","Regenerate with `terraform init -reconfigure` rather than fixing JSON by hand.","Keep file-sync/AV from truncating small state files."],"tags":["backend","json","parsing","corruption","filesystem"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}