{"record":{"id":"69833e48f3bbd5ad","repo":"louthy/language-ext","slug":"key-doesn-t-exist-in-map-change","errorCode":null,"errorMessage":"Key doesn't exist in map: {change}","messagePattern":"Key doesn't exist in map: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/TrieSet/TrieSet.cs","lineNumber":869,"sourceCode":"                                newNodes));\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) = 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));\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}\");\n                }\n                else if (env.Type == UpdateType.TrySetItem)\n                {\n                    // Key doesn't exist, so there's nothing to set\n                    return (0, this);\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));\n            }\n        }\n","sourceCodeStart":851,"sourceCodeEnd":887,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/TrieSet/TrieSet.cs#L851-L887","documentation":"LanguageExt's TrieSet-backed set/map structures distinguish strict updates (SetItem) from lenient ones (TrySetItem). When a strict SetItem update traverses the trie and reaches a branch where the key is absent, the Entries node's Update method throws this ArgumentException instead of silently no-oping. The message interpolates the missing key; the literal '{change}' appears if the exception is inspected without formatting or the value's ToString is '{change}'.","triggerScenarios":"Calling a SetItem-style update on a TrieSet-backed structure (e.g. setItem / [updateType=SetItem] via the trie root) with a key that is not currently present in the set, so traversal lands in the 'else' branch at TrieSet.cs:869 where neither EntryMap nor NodeMap contains the key's hash bit.","commonSituations":"Using setItem on a HashSet/TrieSet where the developer assumed the item exists; refactors that changed add to set; keys removed concurrently or between check and update; typos in key values.","solutions":["Use TrySetItem instead of SetItem so a missing key is a harmless no-op","Check containment first (e.g. set.Contains(key)) before calling SetItem","If the intent is insert-or-update, use Add instead of SetItem","Catch ArgumentException around the update if missing keys are expected"],"exampleFix":"// before\nvar set = TrieSet.create(1, 2);\nvar updated = set.SetItem(3); // throws ArgumentException\n\n// after\nvar updated = set.TrySetItem(3); // no-op if missing, or:\nif (set.Contains(3)) { var updated = set.SetItem(3); }","handlingStrategy":"validation","validationCode":"if (set.Contains(key)) { var updated = set.SetItem(key); } else { /* handle absence */ }","typeGuard":"bool CanSet<T>(TrieSet<T> set, T key) => set.Contains(key);","tryCatchPattern":"try { updated = set.SetItem(key); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Key doesn't exist\")) { updated = set.TrySetItem(key); }","preventionTips":["Prefer TrySetItem when key presence is uncertain","Treat SetItem as a strict replace-only operation","Use Contains checks before strict updates","Deduplicate or validate keys before bulk updates"],"tags":["csharp","languageext","immutables","collection","argument-exception"],"backgroundTag":"record-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"}