opentofu/opentofu · error

error in marshaling resource drift: %w

Error message

error in marshaling resource drift: %w

What it means

Wrapper for the drift stage: MarshalResourceChanges failed while encoding p.DriftedResources (drifted resources, NoOp-filtered except in refresh-only mode). The wrapped error is the real cause - typically 'no schema found for ...' for a drifted resource or a decode failure.

Source

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

	// output.ResourceDrift
	if len(p.DriftedResources) > 0 {
		// In refresh-only mode, we render all resources marked as drifted,
		// including those which have moved without other changes. In other plan
		// modes, move-only changes will be included in the planned changes, so
		// we skip them here.
		var driftedResources []*plans.ResourceInstanceChangeSrc
		if p.UIMode == plans.RefreshOnlyMode {
			driftedResources = p.DriftedResources
		} else {
			for _, dr := range p.DriftedResources {
				if dr.Action != plans.NoOp {
					driftedResources = append(driftedResources, dr)
				}
			}
		}
		output.ResourceDrift, err = MarshalResourceChanges(driftedResources, schemas)
		if err != nil {
			return nil, fmt.Errorf("error in marshaling resource drift: %w", err)
		}
	}

	if err := output.marshalRelevantAttrs(p); err != nil {
		return nil, fmt.Errorf("error marshaling relevant attributes for external changes: %w", err)
	}

	// output.ResourceChanges
	if p.Changes != nil {
		output.ResourceChanges, err = MarshalResourceChanges(p.Changes.Resources, schemas)
		if err != nil {
			return nil, fmt.Errorf("error in marshaling resource changes: %w", err)
		}
	}

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

View on GitHub (pinned to 3561785c48)

Solutions

  1. Run 'tofu init' so provider schemas match the plan's providers
  2. Regenerate the plan in the same environment before exporting JSON
  3. Unwrap the error: if it is the schema error, fix provider availability; if encoding, check versions
  4. Report an issue with the unwrapped cause if both steps check out
Defensive patterns

Strategy: try-catch

Try / catch

if _, err := jsonplan.Marshal(config, plan, sf, schemas); err != nil {
    if strings.Contains(err.Error(), "marshaling resource drift") {
        // root cause is usually a missing provider schema for a drifted resource: tofu init + replan
    }
    return err
}

Prevention

When it happens

Trigger: Marshaling a plan whose drifted resources reference a resource type missing from the supplied provider schemas, or whose before/after values fail decoding/JSON encoding.

Common situations: 'tofu plan -refresh-only' followed by JSON export where the provider schema is unavailable or versions drifted; refresh-only plans keep even NoOp drift entries, widening exposure.

Related errors


AI-assisted analysis of opentofu/opentofu@3561785c48 (2026-08-15). Data as JSON: /api/errors/a9a138978c3d8030. Report an issue: GitHub.