{"record":{"id":"33a7540ee4c05b6d","repo":"louthy/language-ext","slug":"key-doesn-t-exist-in-map-change-key","errorCode":null,"errorMessage":"Key doesn't exist in map: {change.Key}","messagePattern":"Key doesn't exist in map: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/TrieMap/TrieMap.cs","lineNumber":2375,"sourceCode":"                    return (1, new Entries(newEntryMap, newNodeMap, newItems, newNodes), default, true);\n                }\n            }\n            else if (Bit.Get(NodeMap, mask))\n            {\n                // var nodeIndex = Index(NodeMap, mask);\n                var nodeIndex = BitCount((int)NodeMap & ((int)mask - 1));\n\n                var nodeToUpdate = Nodes[nodeIndex];\n                var (cd, newNode, ov, ch) = nodeToUpdate.Update(env, change, hash, section.Next());\n                var (newNodes, _) = SetItem(Nodes, nodeIndex, newNode, env.Mutate);\n                return (cd, new Entries(EntryMap, NodeMap, Items, newNodes), ov, ch);\n            }\n            else\n            {\n                if (env.Type == UpdateType.SetItem)\n                {\n                    // Key must already exist to set it\n                    throw new ArgumentException($\"Key doesn't exist in map: {change.Key}\");\n                }\n                else if (env.Type == UpdateType.TrySetItem)\n                {\n                    // Key doesn't exist, so there's nothing to set\n                    return (0, this, default, false);\n                }\n\n                // var entryIndex = Index(EntryMap, mask);\n                var entryIndex = BitCount((int)EntryMap & ((int)mask - 1));\n\n                // var entries = Bit.Set(EntryMap, mask, true);\n                var entries = EntryMap | mask;\n\n                var newItems = Insert(Items, entryIndex, change);\n                return (1, new Entries(entries, NodeMap, newItems, Nodes), default, true);\n            }\n        }\n","sourceCodeStart":2357,"sourceCodeEnd":2393,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/TrieMap/TrieMap.cs#L2357-L2393","documentation":"This throw fires in the TrieMap update traversal when the key is definitively absent (the node/section where it would live is empty or a terminal leaf) and the update type is SetItem. SetItem requires the key to already exist; TrySetItem returns without change instead. It is the strict-mode counterpart of the not-found-at-node case.","triggerScenarios":"Calling map.SetItem(key, value) for a key that is not in the TrieMap at all; also reached via bulk update APIs that set the update type to SetItem.","commonSituations":"Assuming SetItem upserts; calling SetItem after Remove on the same key; typos or casing mismatches between the add and set call sites.","solutions":["Use AddOrUpdate for insert-or-replace behavior","Use TrySetItem to make the update a no-op for missing keys","Add the key first (Add) then SetItem, or restructure to a single AddOrUpdate","Log/inspect the key with FindOption to confirm presence before SetItem"],"exampleFix":"// before\nmap = map.SetItem(missingKey, value);\n// after\nmap = map.TrySetItem(missingKey, value); // no-op if absent\n// or\nmap = map.AddOrUpdate(missingKey, value);","handlingStrategy":"validation","validationCode":"if (map.ContainsKey(key)) map = map.SetItem(key, value); else map = map.Add(key, value);","typeGuard":null,"tryCatchPattern":"try { map = map.SetItem(key, value); }\ncatch (ArgumentException) { /* key absent: add or skip */ }","preventionTips":["Use AddOrUpdate instead of SetItem for insert-or-replace","Use TrySetItem for optional updates","Check ContainsKey before strict SetItem"],"tags":["immutable-collections","setitem","key-not-found"],"backgroundTag":"resource-not-found","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}