hashicorp/terraform · error

Failed to marshal get attr step name %#v: %s

Error message

Failed to marshal get attr step name %#v: %s

What it means

In encodePath, json.Marshal(s.Name) for a cty.GetAttrStep name failed. s.Name is a Go string; json.Marshal of a string fails only when the bytes are not valid UTF-8. So this error indicates a malformed attribute name in a path.

Source

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

	}

	return json.Marshal(jsonPaths)
}

func encodePath(path cty.Path) (json.RawMessage, error) {
	steps := make([]json.RawMessage, 0, len(path))
	for _, step := range path {
		switch s := step.(type) {
		case cty.IndexStep:
			key, err := ctyjson.Marshal(s.Key, s.Key.Type())
			if err != nil {
				return nil, fmt.Errorf("Failed to marshal index step key %#v: %s", s.Key, err)
			}
			steps = append(steps, key)
		case cty.GetAttrStep:
			name, err := json.Marshal(s.Name)
			if err != nil {
				return nil, fmt.Errorf("Failed to marshal get attr step name %#v: %s", s.Name, err)
			}
			steps = append(steps, name)
		default:
			return nil, fmt.Errorf("Unsupported path step %#v (%t)", step, step)
		}
	}
	return json.Marshal(steps)
}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Report as a bug; attribute names are expected to be valid UTF-8 identifiers.
  2. Identify the schema/attribute producing the invalid name (often a provider issue).
  3. Avoid or sanitize the configuration path that surfaces the bad attribute name.
Defensive patterns

Strategy: validation

Validate before calling

// Validate GetAttrStep names are valid UTF-8 before marshaling.
for _, ra := range plan.RelevantAttributes {
    for _, step := range ra.Attr {
        if gas, ok := step.(cty.GetAttrStep); ok {
            if !utf8.ValidString(gas.Name) {
                return fmt.Errorf("invalid UTF-8 attribute name %q in relevant attribute path", gas.Name)
            }
        }
    }
}

Try / catch

out, err := jsonplan.Marshal(config, plan, sf, schemas)
if err != nil && strings.Contains(err.Error(), "marshal get attr step name") {
    // an attribute name is not valid UTF-8; likely a provider schema bug
}
return err

Prevention

When it happens

Trigger: A GetAttrStep whose Name contains invalid UTF-8 bytes. Reachable via any path encoding (relevant attributes or replace_paths).

Common situations: Extremely rare. A provider schema returning an attribute name that is not valid UTF-8, or a corrupted path/state; essentially an invariant violation since schema attribute names are expected to be valid identifiers.

Related errors


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