hashicorp/terraform · error

error marshaling prior state: %s

Error message

error marshaling prior state: %s

What it means

Top-level wrapper: jsonstate.Marshal(sf, schemas) failed while building the prior_state section. State serialization usually fails because a resource in the prior state has no loaded schema to interpret it with.

Source

Thrown at internal/command/jsonplan/plan.go:337

			return nil, fmt.Errorf("error in marshaling deferred action invocations: %s", err)
		}
	}

	// output.OutputChanges
	if output.OutputChanges, err = MarshalOutputChanges(p.Changes); err != nil {
		return nil, fmt.Errorf("error in marshaling output changes: %s", err)
	}

	// output.Checks
	if p.Checks != nil && p.Checks.ConfigResults.Len() > 0 {
		output.Checks = jsonchecks.MarshalCheckStates(p.Checks)
	}

	// output.PriorState
	if sf != nil && !sf.State.Empty() {
		output.PriorState, err = jsonstate.Marshal(sf, schemas)
		if err != nil {
			return nil, fmt.Errorf("error marshaling prior state: %s", err)
		}
	}

	// output.Config
	output.Config, err = jsonconfig.Marshal(config, schemas)
	if err != nil {
		return nil, fmt.Errorf("error marshaling config: %s", err)
	}

	// output.Changes.ActionInvocations
	if p.Changes.ActionInvocations != nil {
		if output.ActionInvocations, err = MarshalActionInvocations(p.Changes.ActionInvocations, schemas); err != nil {
			return nil, fmt.Errorf("error marshaling action invocations: %s", err)
		}
	}

	return json.Marshal(output)
}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Run `terraform init` to install all providers the state references.
  2. Align provider versions (check .terraform.lock.hcl).
  3. Inspect state with `terraform state list` and remove orphaned resources no longer in config.
Defensive patterns

Strategy: try-catch

Try / catch

out, err := jsonplan.Marshal(config, plan, sf, schemas)
if err != nil && strings.Contains(err.Error(), "marshaling prior state") {
    // a resource in prior state lacks a schema; run terraform init
}
return err

Prevention

When it happens

Trigger: sf != nil and the state is non-empty, and marshaling it errors. Typically an inner 'no schema found' style error for a resource still present in state.

Common situations: Prior state references a resource whose provider was removed or downgraded; provider not initialized; state file referencing a resource type no longer defined by the installed provider; corrupted state.

Related errors


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