hashicorp/terraform · error

attempted to encode a malformed backend state file; it conta

Error message

attempted to encode a malformed backend state file; it contains state for both a 'backend' and a 'state_store' block. This is a bug in Terraform and should be reported.

What it means

EncodeBackendStateFile at backend_state.go:127 refuses to serialize a BackendStateFile that has BOTH Backend and StateStore set. Unlike error 763 (a corrupt file already on disk), this means Terraform's own in-memory code produced an impossible object — the message explicitly calls it a Terraform bug to report.

Source

Thrown at internal/command/workdir/backend_state.go:128

	if stateFile.Backend != nil && stateFile.StateStore != nil {
		return nil, fmt.Errorf("encountered a malformed backend state file that contains state for both a 'backend' and a 'state_store' block")
	}
	if stateFile.StateStore != nil && stateFile.StateStore.ProviderSupplyMode == "" {
		// Check for this, as lacking this data can cause problems later when an empty provider version
		// is encountered. This error will make debugging much easier.
		return nil, fmt.Errorf("encountered a malformed backend state file with a 'state_store' block that is missing the required 'provider_supply_mode' property")
	}

	return &stateFile, nil
}

func EncodeBackendStateFile(f *BackendStateFile) ([]byte, error) {
	f.Version = 3 // we only support version 3
	f.TFVersion = version.SemVer.String()

	switch {
	case f.Backend != nil && f.StateStore != nil:
		return nil, fmt.Errorf("attempted to encode a malformed backend state file; it contains state for both a 'backend' and a 'state_store' block. This is a bug in Terraform and should be reported.")
	case f.Backend == nil && f.StateStore == nil:
		// This is valid - if the user has a backend state file and an implied local backend in use
		// the backend state file exists but has no Backend data.
	case f.Backend != nil:
		// Not implementing anything here - risk of breaking changes
	case f.StateStore != nil:
		err := f.StateStore.Validate()
		if err != nil {
			return nil, err
		}
	default:
		panic("error when determining whether backend state file was valid. This is a bug in Terraform and should be reported.")
	}

	return json.MarshalIndent(f, "", "  ")
}

func (f *BackendStateFile) DeepCopy() *BackendStateFile {

View on GitHub (pinned to c9def3e214)

Solutions

  1. Report a bug to the Terraform project including the CLI version, the stack trace, and how the directory was initialized.
  2. As an immediate workaround, remove .terraform/ and re-run 'terraform init'.
  3. Avoid any custom code/tooling that constructs a BackendStateFile with both Backend and StateStore populated.

Example fix

// before — internal call:
f.Backend = &BackendConfigState{...}
f.StateStore = &StateStoreConfigState{...}
json.Marshal(EncodeBackendStateFile(f)) // -> error 765 (bug)

// after — set exactly one:
f.StateStore = &StateStoreConfigState{...}
// f.Backend stays nil
Defensive patterns

Strategy: validation

Validate before calling

// Invariant: a BackendStateFile carries at most one of Backend / StateStore.
func assertSingleStorageMode(f *BackendStateFile) error {
    if f.Backend != nil && f.StateStore != nil {
        return errors.New("BackendStateFile has both Backend and StateStore set")
    }
    return nil
}
// call assertSingleStorageMode(f) before EncodeBackendStateFile(f).

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: An internal call path sets both f.Backend and f.StateStore non-nil before invoking EncodeBackendStateFile (backend_state.go:127 switch case).

Common situations: A code regression during the backend<->state_store transition; should never arise from user configuration alone.

Understand the failure class

Related errors


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