{"record":{"id":"bcb3918829c32eab","repo":"stride3d/stride","slug":"unable-to-find-the-non-value-type-key-in-the-dictionary-of","errorCode":null,"errorMessage":"Unable to find the non-value type key in the dictionary of cloned keys.","messagePattern":"Unable to find the non-value type key in the dictionary of cloned keys\\.","errorType":"exception","errorClass":"KeyNotFoundException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Reflection/CollectionItemIdentifiers.cs","lineNumber":164,"sourceCode":"    }\n\n    public void CloneInto(CollectionItemIdentifiers target, IReadOnlyDictionary<object, object>? referenceTypeClonedKeys)\n    {\n        target.keyToIdMap.Clear();\n        target.deletedItems.Clear();\n        foreach (var key in keyToIdMap)\n        {\n            if (key.Key.GetType().IsValueType || referenceTypeClonedKeys == null)\n            {\n                target.Add(key.Key, key.Value);\n            }\n            else if (referenceTypeClonedKeys.TryGetValue(key.Key, out var clonedKey))\n            {\n                target.Add(clonedKey, key.Value);\n            }\n            else\n            {\n                throw new KeyNotFoundException(\"Unable to find the non-value type key in the dictionary of cloned keys.\");\n            }\n        }\n        foreach (var deletedItem in DeletedItems)\n        {\n            target.MarkAsDeleted(deletedItem);\n        }\n    }\n\n    public bool IsDeleted(ItemId itemId)\n    {\n        return DeletedItems.Contains(itemId);\n    }\n\n    public IEnumerator<KeyValuePair<object, ItemId>> GetEnumerator() => keyToIdMap.GetEnumerator();\n\n    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();\n}\n","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Reflection/CollectionItemIdentifiers.cs#L146-L182","documentation":"CollectionItemIdentifiers.CloneInto copies the identifier state into a target instance. For reference-type keys it relies on a dictionary of previously cloned keys (referenceTypeClonedKeys) populated during the clone session; when it encounters a key that was never registered there, it cannot map the key into the clone and throws KeyNotFoundException. This prevents silently creating a clone whose items reference untracked keys.","triggerScenarios":"Calling CloneInto when the source collection contains an object key that was never passed through Clone (so referenceTypeClonedKeys has no entry for it) — e.g. items added to the collection after cloning began, or keys shared across multiple collections where only one was cloned.","commonSituations":"Deep-cloning a package/asset whose identified collection gained new entries between the Clone and CloneInto calls; cloning two collections that share the same key objects; partially completed clone workflows in editor tooling.","solutions":["Ensure every item is cloned via the same CollectionItemIdentifiers instance's Clone before CloneInto, so all reference-type keys are registered in referenceTypeClonedKeys.","Re-run the full clone operation (fresh Clone followed immediately by CloneInto) instead of reusing a stale clone-state object.","Verify no items were added or removed between Clone and CloneInto; snapshot the collection first.","As a workaround, construct a new CollectionItemIdentifiers and re-add each item with a fresh id rather than patching clone state."],"exampleFix":"// before: items added after Clone are unknown to referenceTypeClonedKeys\nvar cloneState = identifiers.Clone();\nidentifiers.Add(ItemId.New(), newItem);\nidentifiers.CloneInto(cloneState);\n\n// after: add items before cloning, or start a fresh clone session\nidentifiers.Add(ItemId.New(), newItem);\nvar cloneState = identifiers.Clone();\nidentifiers.CloneInto(cloneState);","handlingStrategy":"try-catch","validationCode":"if (!identifiers.TryGetValue(key.Key, out _))\n    throw new InvalidOperationException($\"Key {key.Key} was not registered by Clone before CloneInto\");","typeGuard":null,"tryCatchPattern":"try { identifiers.CloneInto(target); }\ncatch (KeyNotFoundException) { identifiers = new CollectionItemIdentifiers(); /* restart clone session */ }","preventionTips":["Perform Clone and CloneInto back-to-back with no mutation in between","Never share reference-type keys across independent clone sessions","Snapshot collections before cloning workflows","Re-create clone state instead of reusing stale objects"],"tags":["clone","dictionary","collection","state"],"backgroundTag":"internal-invariant-violation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}