{"record":{"id":"7c59c818b4ed1c67","repo":"hashicorp/terraform","slug":"set-item-just-set-doesn-t-exist","errorCode":null,"errorMessage":"set item just set doesn't exist","messagePattern":"set item just set doesn't exist","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/legacy/helper/schema/field_writer_map.go","lineNumber":331,"sourceCode":"\n\t\t// Build the set by going over the list items in order and\n\t\t// hashing them into the set. The reason we go over the list and\n\t\t// not the `value` directly is because this forces all types\n\t\t// to become []interface{} (generic) instead of []string, which\n\t\t// most hash functions are expecting.\n\t\ts := schema.ZeroValue().(*Set)\n\t\ttempR := &MapFieldReader{\n\t\t\tMap:    BasicMapReader(tempW.Map()),\n\t\t\tSchema: tempSchemaMap,\n\t\t}\n\t\tfor i := 0; i < v.Len(); i++ {\n\t\t\tis := strconv.FormatInt(int64(i), 10)\n\t\t\tresult, err := tempR.ReadField(append(tempAddr, is))\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif !result.Exists {\n\t\t\t\tpanic(\"set item just set doesn't exist\")\n\t\t\t}\n\n\t\t\ts.Add(result.Value)\n\t\t}\n\n\t\tvalue = s\n\t}\n\n\t// Clear any keys that match the set address first. This is necessary because\n\t// it's always possible and sometimes may be necessary to write to a certain\n\t// writer layer more than once with different set data each time, which will\n\t// lead to different keys being inserted, which can lead to determinism\n\t// problems when the old data isn't wiped first.\n\tw.clearTree(addr)\n\n\tif value.(*Set) == nil {\n\t\tw.result[k+\".#\"] = \"0\"\n\t\treturn nil","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/legacy/helper/schema/field_writer_map.go#L313-L349","documentation":"Panics in MapFieldWriter.setSet (field_writer_map.go:331) during the slice->set conversion path. Each list element is written to a temporary MapFieldWriter and immediately re-read with tempR.ReadField; if result.Exists is false the write/read round-trip is broken and it panics. This is an internal invariant, not a normal caller error.","triggerScenarios":"A TypeSet field fed a []interface{} value whose element type cannot round-trip through the flatmap writer/reader (e.g. Elem misconfigured as nil or an unexpected concrete type, or a custom Set hash function that writes elements under a code the reader cannot locate).","commonSituations":"Provider schema bugs where a set's Elem is nil, a bare ValueType, or a struct instead of &schema.Schema/&schema.Resource; or a custom Set func that mishandles a type.","solutions":["Verify the set field's Elem is &schema.Schema{...} or &schema.Resource{...}.","If you supply a custom Set (hash) function, ensure it handles every concrete type the set will hold.","Reproduce with TestResourceData/TestResourceDataRaw to confirm the value round-trips before exercising it in plan/apply."],"exampleFix":"// before\n\"ports\": { Type: schema.TypeSet, Elem: nil },\n// after\n\"ports\": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeInt} },","handlingStrategy":"validation","validationCode":"func setElemOk(s *schema.Schema) bool {\n    if s.Type != schema.TypeSet { return true }\n    switch s.Elem.(type) {\n    case *schema.Schema, *schema.Resource: return true\n    }\n    return s.Set != nil // custom hash bypasses the round-trip path\n}","typeGuard":"func isSchemaOrResource(v interface{}) bool {\n    switch v.(type) {\n    case *schema.Schema, *schema.Resource: return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Always set Elem to &schema.Schema or &schema.Resource on TypeSet.","If using a custom Set func, test it with every concrete type the set holds.","Round-trip test set values with TestResourceData."],"tags":["terraform","schema","set","panic","internal-invariant"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}