hashicorp/terraform · error

error when preparing state store config for planfile: %s

Error message

error when preparing state store config for planfile: %s

What it means

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.

Source

Thrown at internal/command/workdir/statestore_config_state.go:122

		return err
	}
	s.ConfigRaw = buf
	return nil
}

// PlanData produces an alternative representation of the receiver that is
// suitable for storing in a plan. The current workspace must additionally
// be provided, to be stored alongside the state store configuration.
//
// The state_store configuration schema is required in order to properly
// encode the state store-specific configuration settings.
func (s *StateStoreConfigState) PlanData(storeSchema *configschema.Block, providerSchema *configschema.Block, workspaceName string) (*plans.StateStore, error) {
	if s == nil {
		panic("PlanData called on a nil *StateStoreConfigState receiver. This is a bug in Terraform and should be reported.")
	}

	if err := s.Validate(); err != nil {
		return nil, fmt.Errorf("error when preparing state store config for planfile: %s", err)
	}

	storeConfigVal, err := s.Config(storeSchema)
	if err != nil {
		return nil, fmt.Errorf("failed to decode state_store config: %w", err)
	}
	providerConfigVal, err := s.Provider.Config(providerSchema)
	if err != nil {
		return nil, fmt.Errorf("failed to decode state_store's nested provider config: %w", err)
	}

	var providerVersion *version.Version
	switch s.ProviderSupplyMode {
	case getproviders.BuiltIn, getproviders.Reattached, getproviders.DevOverride:
		// 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.
		// 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.
		providerVersion = version.Must(version.NewVersion("0.0.0"))
	case getproviders.ManagedByTerraform:

View on GitHub (pinned to c9def3e214)

Solutions

  1. Read the appended text to identify which validation error (767-772) triggered it and fix that root cause.
  2. Re-run 'terraform init' to regenerate a clean state_store record, then retry the plan.
  3. Verify required_providers and the state_store block are both well-formed in configuration.

Example fix

// before
$ terraform plan -out=tfplan
Error: error when preparing state store config for planfile: ... provider data is missing

// after
$ rm -rf .terraform && terraform init
$ terraform plan -out=tfplan
Defensive patterns

Strategy: try-catch

Validate before calling

// Run Validate() yourself before PlanData so you control the error surface.
if err := s.Validate(); err != nil {
    // handle the specific validation cause, optionally re-init
}

Type guard

null

Try / catch

if _, err := s.PlanData(storeSchema, provSchema, ws); err != nil {
    if strings.Contains(err.Error(), "preparing state store config for planfile") {
        // underlying Validate() failed; re-init and retry
    }
    return err
}

Prevention

When it happens

Trigger: Saving/applying a plan when the working directory's state_store config is incomplete or corrupt, so s.Validate() returns non-nil inside PlanData.

Common situations: Running 'terraform plan -out=tfplan' against a directory whose .terraform/terraform.tfstate has a malformed state_store block.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/9b876e00767fc537. Report an issue: GitHub.