{"record":{"id":"f0d758a60ea5c3c7","repo":"hashicorp/terraform","slug":"cannot-decode-s-from-flatmap","errorCode":null,"errorMessage":"cannot decode %s from flatmap","messagePattern":"cannot decode (.+?) from flatmap","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/configs/hcl2shim/flatmap.go","lineNumber":176,"sourceCode":"\nfunc hcl2ValueFromFlatmapValue(m map[string]string, key string, ty cty.Type) (cty.Value, error) {\n\tvar val cty.Value\n\tvar err error\n\tswitch {\n\tcase ty.IsPrimitiveType():\n\t\tval, err = hcl2ValueFromFlatmapPrimitive(m, key, ty)\n\tcase ty.IsObjectType():\n\t\tval, err = hcl2ValueFromFlatmapObject(m, key+\".\", ty.AttributeTypes())\n\tcase ty.IsTupleType():\n\t\tval, err = hcl2ValueFromFlatmapTuple(m, key+\".\", ty.TupleElementTypes())\n\tcase ty.IsMapType():\n\t\tval, err = hcl2ValueFromFlatmapMap(m, key+\".\", ty)\n\tcase ty.IsListType():\n\t\tval, err = hcl2ValueFromFlatmapList(m, key+\".\", ty)\n\tcase ty.IsSetType():\n\t\tval, err = hcl2ValueFromFlatmapSet(m, key+\".\", ty)\n\tdefault:\n\t\terr = fmt.Errorf(\"cannot decode %s from flatmap\", ty.FriendlyName())\n\t}\n\n\tif err != nil {\n\t\treturn cty.DynamicVal, err\n\t}\n\treturn val, nil\n}\n\nfunc hcl2ValueFromFlatmapPrimitive(m map[string]string, key string, ty cty.Type) (cty.Value, error) {\n\trawVal, exists := m[key]\n\tif !exists {\n\t\treturn cty.NullVal(ty), nil\n\t}\n\tif rawVal == UnknownVariableValue {\n\t\treturn cty.UnknownVal(ty), nil\n\t}\n\n\tvar err error","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/configs/hcl2shim/flatmap.go#L158-L194","documentation":"Returned by hcl2ValueFromFlatmapValue (flatmap.go:176) when decoding a flatmap (the legacy string-keyed map representation of Terraform state) into a cty.Value whose type does not match any recognized kind. The switch covers primitive, object, tuple, map, list, and set; reaching the default means the cty.Type is something none of those checks recognize — typically a capsule type, a dynamically-typed value, or an internally-inconsistent type descriptor.","triggerScenarios":"Calling HCL2ValueFromFlatmap with a cty.Type that is not one of the six handled kinds (e.g. a cty.Capsule type, or a type produced by cty.DynamicPseudoType that was not resolved before decoding). Also reachable when a custom/extended cty type leaks into the flatmap round-trip path.","commonSituations":"State migration or import code that passes a schema type containing dynamic pseudo-types without first resolving them. Provider schemas that declare attributes with unsupported underlying types. Hand-edited or third-party-tampered state files paired with a schema that doesn't match.","solutions":["Print ty.FriendlyName() (it appears in the message) and confirm whether that type is supposed to be representable in a flatmap — only primitive/object/tuple/map/list/set are supported.","If the type is cty.DynamicPseudoType, resolve it to a concrete type before calling HCL2ValueFromFlatmap; flatmap cannot encode fully-dynamic values.","Ensure the cty.Type used for decoding matches the type that originally produced the flatmap (type mismatch between schema versions causes this).","If you have a custom capsule type, decode it through a dedicated path instead of the generic flatmap shim."],"exampleFix":"// before\nval, err := hcl2shim.HCL2ValueFromFlatmap(m, key, myCapsuleType)\n\n// after — flatmap only supports the 6 standard kinds; use a concrete type\nval, err := hcl2shim.HCL2ValueFromFlatmap(m, key, cty.Map(cty.String))","handlingStrategy":"validation","validationCode":"// Only the 6 standard cty kinds are decodable from a flatmap\nfunc isFlatmapDecodable(ty cty.Type) bool {\n    return ty.IsPrimitiveType() || ty.IsObjectType() || ty.IsTupleType() ||\n        ty.IsMapType() || ty.IsListType() || ty.IsSetType()\n}\n// Resolve dynamic types and reject unsupported kinds BEFORE decoding:\nif !isFlatmapDecodable(ty) {\n    return cty.DynamicVal, fmt.Errorf(\"flatmap cannot decode type %s\", ty.FriendlyName())\n}","typeGuard":"func isFlatmapDecodable(ty cty.Type) bool {\n    return ty.IsPrimitiveType() || ty.IsObjectType() || ty.IsTupleType() ||\n        ty.IsMapType() || ty.IsListType() || ty.IsSetType()\n}","tryCatchPattern":"val, err := hcl2shim.HCL2ValueFromFlatmap(m, key, ty)\nif err != nil {\n    // fall back to a null of the type rather than propagating a decode failure\n    log.Printf(\"flatmap decode failed for %s: %v\", ty.FriendlyName(), err)\n    return cty.NullVal(ty), nil\n}","preventionTips":["Never pass cty.DynamicPseudoType or capsule types into the flatmap round-trip without resolving them first.","Keep the cty.Type used for decoding identical to the one that produced the flatmap.","Validate schemas with InternalValidate to catch exotic types before they reach state I/O."],"tags":["flatmap","hcl2shim","state","cty","type-mismatch"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}