{"record":{"id":"520e5b0af15f9e79","repo":"hashicorp/terraform","slug":"no-state","errorCode":null,"errorMessage":"no state","messagePattern":"no state","errorType":"exception","errorClass":"ErrNoState","httpStatus":null,"severity":"info","filePath":"internal/states/statefile/read.go","lineNumber":20,"sourceCode":"// SPDX-License-Identifier: BUSL-1.1\n\npackage statefile\n\nimport (\n\t\"encoding/json\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\n\tversion \"github.com/hashicorp/go-version\"\n\n\t\"github.com/hashicorp/terraform/internal/tfdiags\"\n\ttfversion \"github.com/hashicorp/terraform/version\"\n)\n\n// ErrNoState is returned by ReadState when the state file is empty.\nvar ErrNoState = errors.New(\"no state\")\n\n// ErrUnusableState is an error wrapper to indicate that we *think* the input\n// represents state data, but can't use it for some reason (as explained in the\n// error text). Callers can check against this type with errors.As() if they\n// need to distinguish between corrupt state and more fundamental problems like\n// an empty file.\ntype ErrUnusableState struct {\n\tinner error\n}\n\nfunc errUnusable(err error) *ErrUnusableState {\n\treturn &ErrUnusableState{inner: err}\n}\n\nfunc (e *ErrUnusableState) Error() string {\n\treturn e.inner.Error()\n}\n","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/states/statefile/read.go#L2-L38","documentation":"ErrNoState is returned by statefile.Read when there is no state to read: either the reader is a typed-nil *os.File, or the bytes read are empty (length 0). It is the sentinel that callers use to distinguish 'no state yet' from corrupt/unreadable state (which is reported as ErrUnusableState or wrapped diagnostics). It is expected on first run or after `terraform init` with no prior state.","triggerScenarios":"statefile.Read at read.go:55-57 (nil *os.File) or read.go:74-76 (empty src). Common callers are state managers reading from disk/object storage.","commonSituations":"First run of Terraform in a fresh workspace; `terraform state push` of an empty file; backend bucket is empty; state file was deleted; misconfigured backend path returning empty content.","solutions":["If a fresh workspace, treat ErrNoState as normal (no prior state) and proceed with planning.","If state was expected, verify the backend configuration (workspace key, bucket, path) and that the state object exists.","Distinguish with errors.Is(err, statefile.ErrNoState) rather than treating it as a fatal error.","Restore from backup if the state file was accidentally emptied/deleted."],"exampleFix":"// before\nf, err := statefile.Read(r)\nif err != nil {\n    return err\n}\n\n// after\nf, err := statefile.Read(r)\nif errors.Is(err, statefile.ErrNoState) {\n    return nil, nil // no prior state — first run\n}\nif err != nil {\n    return nil, err\n}","handlingStrategy":"try-catch","validationCode":"// Check the file/object is non-empty before reading\nfi, err := os.Stat(path)\nif err == nil && fi.Size() == 0 {\n    // expect ErrNoState — handle accordingly\n}","typeGuard":"func isNoState(err error) bool {\n    return errors.Is(err, statefile.ErrNoState)\n}","tryCatchPattern":"f, err := statefile.Read(r)\nif errors.Is(err, statefile.ErrNoState) {\n    // first run / no prior state — proceed with empty state\n    return newState(), nil\n}\nif err != nil {\n    return nil, err\n}","preventionTips":["Always check errors.Is(err, statefile.ErrNoState) separately from real errors.","Validate backend configuration when state is unexpectedly empty.","Back up state objects so accidental deletion is recoverable."],"tags":["state","sentinel","first-run","terraform"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}