{"record":{"id":"9b876e00767fc537","repo":"hashicorp/terraform","slug":"error-when-preparing-state-store-config-for-planfi","errorCode":null,"errorMessage":"error when preparing state store config for planfile: %s","messagePattern":"error when preparing state store config for planfile: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/workdir/statestore_config_state.go","lineNumber":122,"sourceCode":"\t\treturn err\n\t}\n\ts.ConfigRaw = buf\n\treturn nil\n}\n\n// PlanData produces an alternative representation of the receiver that is\n// suitable for storing in a plan. The current workspace must additionally\n// be provided, to be stored alongside the state store configuration.\n//\n// The state_store configuration schema is required in order to properly\n// encode the state store-specific configuration settings.\nfunc (s *StateStoreConfigState) PlanData(storeSchema *configschema.Block, providerSchema *configschema.Block, workspaceName string) (*plans.StateStore, error) {\n\tif s == nil {\n\t\tpanic(\"PlanData called on a nil *StateStoreConfigState receiver. This is a bug in Terraform and should be reported.\")\n\t}\n\n\tif err := s.Validate(); err != nil {\n\t\treturn nil, fmt.Errorf(\"error when preparing state store config for planfile: %s\", err)\n\t}\n\n\tstoreConfigVal, err := s.Config(storeSchema)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode state_store config: %w\", err)\n\t}\n\tproviderConfigVal, err := s.Provider.Config(providerSchema)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode state_store's nested provider config: %w\", err)\n\t}\n\n\tvar providerVersion *version.Version\n\tswitch s.ProviderSupplyMode {\n\tcase getproviders.BuiltIn, getproviders.Reattached, getproviders.DevOverride:\n\t\t// For built-in providers, reattached providers, and developer overrides, we don't require version information to be present in the state file, so we should be tolerant of it being missing.\n\t\t// In this case we can just use a placeholder version that will never actually be used for anything, but allows us to avoid returning an error when trying to save state store data to a plan file.\n\t\tproviderVersion = version.Must(version.NewVersion(\"0.0.0\"))\n\tcase getproviders.ManagedByTerraform:","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/workdir/statestore_config_state.go#L104-L140","documentation":"PlanData at statestore_config_state.go:116 produces a representation of the state store for embedding in a plan file. It calls Validate() first (line 121) and wraps any failure with 'error when preparing state store config for planfile:'. The underlying cause is one of the validation errors 767-772.","triggerScenarios":"Saving/applying a plan when the working directory's state_store config is incomplete or corrupt, so s.Validate() returns non-nil inside PlanData.","commonSituations":"Running 'terraform plan -out=tfplan' against a directory whose .terraform/terraform.tfstate has a malformed state_store block.","solutions":["Read the appended text to identify which validation error (767-772) triggered it and fix that root cause.","Re-run 'terraform init' to regenerate a clean state_store record, then retry the plan.","Verify required_providers and the state_store block are both well-formed in configuration."],"exampleFix":"// before\n$ terraform plan -out=tfplan\nError: error when preparing state store config for planfile: ... provider data is missing\n\n// after\n$ rm -rf .terraform && terraform init\n$ terraform plan -out=tfplan","handlingStrategy":"try-catch","validationCode":"// Run Validate() yourself before PlanData so you control the error surface.\nif err := s.Validate(); err != nil {\n    // handle the specific validation cause, optionally re-init\n}","typeGuard":"null","tryCatchPattern":"if _, err := s.PlanData(storeSchema, provSchema, ws); err != nil {\n    if strings.Contains(err.Error(), \"preparing state store config for planfile\") {\n        // underlying Validate() failed; re-init and retry\n    }\n    return err\n}","preventionTips":["Validate state_store config before producing a plan file.","Re-init after any provider/state-store change before planning.","Surface the underlying validation error to the user verbatim."],"tags":["terraform","plan","state-store","validation"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}