{"record":{"id":"02ac1c1de9dcf8e1","repo":"hashicorp/terraform","slug":"failed-to-marshal-index-step-key-v-s","errorCode":null,"errorMessage":"Failed to marshal index step key %#v: %s","messagePattern":"Failed to marshal index step key %#v: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/jsonplan/plan.go","lineNumber":1072,"sourceCode":"\tfor _, path := range pathList {\n\t\tjsonPath, err := encodePath(path)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tjsonPaths = append(jsonPaths, jsonPath)\n\t}\n\n\treturn json.Marshal(jsonPaths)\n}\n\nfunc encodePath(path cty.Path) (json.RawMessage, error) {\n\tsteps := make([]json.RawMessage, 0, len(path))\n\tfor _, step := range path {\n\t\tswitch s := step.(type) {\n\t\tcase cty.IndexStep:\n\t\t\tkey, err := ctyjson.Marshal(s.Key, s.Key.Type())\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"Failed to marshal index step key %#v: %s\", s.Key, err)\n\t\t\t}\n\t\t\tsteps = append(steps, key)\n\t\tcase cty.GetAttrStep:\n\t\t\tname, err := json.Marshal(s.Name)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"Failed to marshal get attr step name %#v: %s\", s.Name, err)\n\t\t\t}\n\t\t\tsteps = append(steps, name)\n\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(\"Unsupported path step %#v (%t)\", step, step)\n\t\t}\n\t}\n\treturn json.Marshal(steps)\n}\n","sourceCodeStart":1054,"sourceCodeEnd":1087,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/jsonplan/plan.go#L1054-L1087","documentation":"In encodePath, ctyjson.Marshal(s.Key, s.Key.Type()) for a cty.IndexStep key failed. The index key is a cty.Value; this error means its type cannot be serialized to JSON, so the path cannot be encoded losslessly.","triggerScenarios":"A relevant-attribute or replace path includes an IndexStep whose key is of a non-JSON-serializable type (e.g. a complex object used as a set element key). Reached via encodePaths/encodePath from relevant attributes (607) or replace_paths encoding.","commonSituations":"Paths into set-typed attributes where elements are keyed by a structured type; an internal type-handling gap for an unusual attribute type; rarely, a corrupted path.","solutions":["Inspect the key shown via %#v to learn its type.","Avoid relying on that attribute path in replace detection if possible.","Report as a renderer bug if it occurs on a common/primitive key type."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Validate that every IndexStep key in recorded paths is JSON-serializable\n// before marshaling, so failures surface with context.\nfor _, ra := range plan.RelevantAttributes {\n    for _, step := range ra.Attr {\n        if idx, ok := step.(cty.IndexStep); ok {\n            if _, err := ctyjson.Marshal(idx.Key, idx.Key.Type()); err != nil {\n                return fmt.Errorf(\"unserializable index key %#v in relevant attribute path: %w\", idx.Key, err)\n            }\n        }\n    }\n}","typeGuard":"func serializablePathStep(step cty.PathStep) error {\n    switch s := step.(type) {\n    case cty.IndexStep:\n        _, err := ctyjson.Marshal(s.Key, s.Key.Type())\n        return err\n    case cty.GetAttrStep:\n        return nil // string name; validated separately\n    default:\n        return fmt.Errorf(\"unsupported step %T\", step)\n    }\n}","tryCatchPattern":"out, err := jsonplan.Marshal(config, plan, sf, schemas)\nif err != nil && strings.Contains(err.Error(), \"marshal index step key\") {\n    // a path index key is not JSON-serializable; capture the %#v value for triage\n}\nreturn err","preventionTips":["Avoid configurations that produce relevant-attribute paths keyed by complex object types.","When logging path-encoding errors, include the key's type (%T) not just its value.","Report occurrences on primitive key types as renderer bugs."],"tags":["paths","marshal","cty"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}