{"record":{"id":"a0295393b53a78ba","repo":"hashicorp/terraform","slug":"invalid-syntax-no-format-version-number","errorCode":null,"errorMessage":"invalid syntax: no format version number","messagePattern":"invalid syntax: no format version number","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/workdir/backend_state.go","lineNumber":90,"sourceCode":"\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 {\n\t\treturn nil, fmt.Errorf(\"invalid syntax: %w\", err)\n\t}\n\tif stateFile.Backend == nil && stateFile.Remote != nil {\n\t\t// It's very unlikely to get here, but one way it could happen is\n\t\t// if this working directory was most recently used with Terraform v0.8\n\t\t// or earlier, which didn't yet include the concept of backends.\n\t\t// This error message assumes that's the case.\n\t\treturn nil, fmt.Errorf(\"this working directory uses legacy remote state and so must first be upgraded using Terraform v0.9\")","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/workdir/backend_state.go#L72-L108","documentation":"ParseBackendStateFile successfully parsed JSON but the top-level `version` field was 0 (the zero value), which means the field is missing entirely or explicitly null. Version 0 is impossible for JSON state (snapshot version 0 was gob-encoded binary), so this is treated as a malformed/unsupported file.","triggerScenarios":"A .terraform/terraform.tfstate file whose JSON is valid but lacks a numeric `version` key, e.g. a hand-rolled stub, a different tool's JSON, or a backend state produced by a fork that omits the version field.","commonSituations":"User-generated or third-party tool wrote a JSON file in .terraform/ without the schema Terraform expects; partial merge/clobber of the file.","solutions":["Confirm the file is meant to be a Terraform backend state file (it may have been overwritten by another tool).","Delete the bogus file and `terraform init` to recreate it from the backend block.","If migrating from another tool, write a proper backend \"<type>\" {} block and init instead."],"exampleFix":"# before — .terraform/terraform.tfstate contains just {\"backend\":{...}}\n\n# after — let terraform own the file\nrm .terraform/terraform.tfstate\nterraform init","handlingStrategy":"validation","validationCode":"// pre-flight: reject files missing the version field up front\nvar sniff struct{ Version int `json:\"version\"` }\n_ = json.Unmarshal(src, &sniff)\nif sniff.Version == 0 {\n    return nil, errors.New(\"backend state file lacks a version field; regenerate with `terraform init`\")\n}","typeGuard":"func hasVersionField(src []byte) bool {\n    var s struct{ Version int `json:\"version\"` }\n    _ = json.Unmarshal(src, &s)\n    return s.Version != 0\n}","tryCatchPattern":null,"preventionTips":["Don't write .terraform/terraform.tfstate from external tooling.","If migrating tooling, delete the file and let `terraform init` recreate it."],"tags":["backend","version-field","schema","init"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}