{"record":{"id":"f0ef1ffc9b8f74e6","repo":"stride3d/stride","slug":"asset-already-exist-in-this-collection","errorCode":null,"errorMessage":"Asset already exist in this collection","messagePattern":"Asset already exist in this collection","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/PackageAssetCollection.cs","lineNumber":320,"sourceCode":"    /// An asset with the same id [{0}] is already registered with the location [{1}].ToFormat(item.Id, location.Path);item\n    /// or\n    /// Asset location [{0}] cannot contain drive information.ToFormat(location);item\n    /// or\n    /// Asset location [{0}] must be relative and not absolute (not start with '/').ToFormat(location);item\n    /// or\n    /// Asset location [{0}] cannot start with relative '..'.ToFormat(location);item\n    /// </exception>\n    public void CheckCanAdd(AssetItem item)\n    {\n        // TODO better handle interaction\n        if (item == null)\n        {\n            throw new ArgumentNullException(nameof(item), \"Cannot add an empty asset item reference\");\n        }\n\n        if (registeredItems.Contains(item))\n        {\n            throw new ArgumentException(\"Asset already exist in this collection\", nameof(item));\n        }\n\n        if (item.Id == AssetId.Empty)\n        {\n            throw new ArgumentException(\"Cannot add an asset with an empty Id\", nameof(item));\n        }\n\n        if (item.Package != null && item.Package != Package)\n        {\n            throw new ArgumentException(\"Cannot add an asset that is already added to another package\", nameof(item));\n        }\n\n        // Note: we ignore name collisions if asset is not referenceable\n        var referenceable = item.Asset.GetType().GetCustomAttribute<AssetDescriptionAttribute>()?.Referenceable ?? true;\n\n        // Namespaced packages root their locations /Namespace/...; creation paths author\n        // unqualified locations, qualified here like the loaders do. Plain packages stay\n        // relative-only (that reservation is what makes rooted URLs collision-free).","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/PackageAssetCollection.cs#L302-L338","documentation":"CheckCanAdd rejects an AssetItem already present in registeredItems, throwing ArgumentException \"Asset already exist in this collection\". The collection enforces unique asset items (by identity) so duplicate registration is a caller bug.","triggerScenarios":"Calling Add twice with the same AssetItem, or adding an item that compares equal (Contains) to an existing one — e.g. re-running an import/scan that re-registers assets without clearing the collection.","commonSituations":"Reloading a package into an existing collection without Remove/Clear first; duplicate assets from overlapping directory scans; idempotency issues in scripts.","solutions":["Check collection.Contains(item) before Add, or use a try-add pattern","Remove/Clear existing items before re-adding during reloads","Deduplicate scan results before registering"],"exampleFix":"// before\nforeach (var item in scannedItems) package.Assets.Add(item);\n// after\nforeach (var item in scannedItems)\n    if (!package.Assets.Contains(item)) package.Assets.Add(item);","handlingStrategy":"validation","validationCode":"if (item is null || assets.Contains(item)) continue;","typeGuard":null,"tryCatchPattern":"try { assets.Add(item); } catch (ArgumentException ex) when (ex.Message.Contains(\"already exist\")) { log.Debug($\"Duplicate asset skipped: {item.Location}\"); }","preventionTips":["Deduplicate scanned assets before registration","Clear/remove items before re-adding on reload","Make import pipelines idempotent with Contains checks"],"tags":["duplicate","assets","collection"],"backgroundTag":"file-already-exists","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"}