{"record":{"id":"f98b413f9f9c84ba","repo":"hashicorp/terraform","slug":"null-state-found-in-test-execution","errorCode":null,"errorMessage":"null state found in test execution","messagePattern":"null state found in test execution","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/moduletest/graph/eval_context.go","lineNumber":623,"sourceCode":"}\n\n// GetState retrieves the current state for the specified key, exactly as it\n// specified within the current cache.\nfunc (ec *EvalContext) GetState(key string) *teststates.TestRunState {\n\tec.stateLock.Lock()\n\tdefer ec.stateLock.Unlock()\n\treturn ec.getState(key)\n}\n\nfunc (ec *EvalContext) getState(key string) *teststates.TestRunState {\n\tcurrent := ec.FileStates[key]\n\tif current == nil {\n\t\t// this shouldn't happen, all the states must be initialised prior to\n\t\t// the evaluation context being created.\n\t\t//\n\t\t// panic here, where the origin of the bug is instead of returning a\n\t\t// null state to panic later.\n\t\tpanic(\"null state found in test execution\")\n\t}\n\treturn current\n}\n\n// LoadState returns the correct state for the specified run block. This differs\n// from GetState in that it will load the state from any remote backend\n// specified within the run block rather than simply retrieve the cached state\n// (which might be empty for a run block with a backend if it hasn't executed\n// yet).\nfunc (ec *EvalContext) LoadState(run *configs.TestRun) (*states.State, error) {\n\tec.stateLock.Lock()\n\tdefer ec.stateLock.Unlock()\n\n\tcurrent := ec.getState(run.StateKey)\n\n\tif run.Backend != nil {\n\t\t// Then we'll load the state from the backend instead of just using\n\t\t// whatever was in the state.","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/moduletest/graph/eval_context.go#L605-L641","documentation":"Panics in EvalContext.getState (eval_context.go:623). It looks up a TestRunState by key in FileStates; if absent it panics, because all state keys must be initialized before the eval context is created. The comment notes this is a defensive panic to fail at the origin of the bug rather than returning nil and panicking later.","triggerScenarios":"A module test run block references a state key that was never initialized in FileStates — typically a harness bug in moduletest state setup, or a run block whose state_key does not match any prior run.","commonSituations":"Terraform test files (.tftest.hcl) where a run block's state_key references a non-existent run, duplicate/missing keys, or bugs in moduletest initialization.","solutions":["Verify every run block's state_key references an existing prior run.","Ensure no duplicate or missing state keys across the test file.","Check the .tftest.hcl structure and that the referenced run actually executes first."],"exampleFix":"// before\nrun \"second\" { state_key = \"nonexistent\" }\n// after\nrun \"first\"  {}\nrun \"second\" { state_key = \"first\" }","handlingStrategy":"validation","validationCode":"func stateKeyInitialized(fileStates map[string]*teststates.TestRunState, key string) error {\n    if _, ok := fileStates[key]; !ok {\n        return fmt.Errorf(\"state key %q is not initialized\", key)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure every state_key in a run block references an existing prior run.","Avoid duplicate/missing state keys in .tftest.hcl files.","Validate run references before executing the test suite."],"tags":["terraform","testing","moduletest","state","panic"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}