{"record":{"id":"9d7036a90b30e950","repo":"stride3d/stride","slug":"cannot-add-the-asset-0-1-to-package-2-3-it-is-already-in","errorCode":null,"errorMessage":"Cannot add the asset [{0}] [{1}] to package [{2}] [{3}]: it is already in package [{4}] [{5}] in the current session","messagePattern":"Cannot add the asset \\[(.+?)\\] \\[(.+?)\\] to package \\[(.+?)\\] \\[(.+?)\\]: it is already in package \\[(.+?)\\] \\[(.+?)\\] in the current session","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/PackageAssetCollection.cs","lineNumber":382,"sourceCode":"        {\n            throw new ArgumentException(\"Asset location [{0}] cannot contain drive information\".ToFormat(location), nameof(item));\n        }\n\n        if (location.GetDirectory()?.StartsWith(\"..\", StringComparison.Ordinal) == true)\n        {\n            throw new ArgumentException(\"Asset location [{0}] cannot start with relative '..'\".ToFormat(location), nameof(item));\n        }\n\n        // Double check that this asset is not already stored in another package for this session\n        if (Package.Session != null)\n        {\n            foreach (var otherPackage in Package.Session.Packages)\n            {\n                if (otherPackage != Package)\n                {\n                    if (otherPackage.Assets.ContainsById(item.Id))\n                    {\n                        throw new ArgumentException(\"Cannot add the asset [{0}] [{1}] to package [{2}] [{3}]: it is already in package [{4}] [{5}] in the current session\".ToFormat(item.Id, item.Location, Package.Meta.Name, Package.FullPath, otherPackage.Meta.Name, otherPackage.FullPath));\n                    }\n                }\n            }\n        }\n    }\n\n    public IEnumerator<AssetItem> GetEnumerator()\n    {\n        return registeredItems.GetEnumerator();\n    }\n\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return GetEnumerator();\n    }\n\n    void ICollection.CopyTo(Array array, int index)\n    {","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/PackageAssetCollection.cs#L364-L400","documentation":"Within a Stride editing session, each asset id must exist in exactly one package. CheckCanAdd performs a session-wide scan: if another package in Package.Session.Packages already contains the id, the add is rejected with a message naming both packages, keeping session state consistent for references and undo.","triggerScenarios":"Calling package.Assets.Add(item) while Package.Session is non-null and some other package in the session already contains an asset with item.Id (found via ContainsById). The throw uses the constructed formatted message without an paramName argument.","commonSituations":"Duplicating an asset into another package without regenerating its id; loading the same asset file into two packages of one session; scripted tools that copy assets between packages and forget new ids.","solutions":["Assign a new id to the duplicated asset: item.Id = AssetId.New() before adding.","Remove the asset from the other package first if the intent is a move between packages.","Search the session for the existing owner (otherPackage.Assets[id]) and update it there instead of adding a copy.","Run the copy/move through session-level package operations rather than raw per-package Add."],"exampleFix":"// before\npackageB.Assets.Add(assetItem); // assetItem.Id still owned by packageA\n\n// after\nvar copy = new AssetItem(assetItem.Location, (Asset)assetItem.Asset.Clone()) { Id = AssetId.New() };\npackageB.Assets.Add(copy);","handlingStrategy":"validation","validationCode":"foreach (var p in package.Session.Packages)\n    if (p != package && p.Assets.ContainsById(item.Id))\n        item.Id = AssetId.New();\npackage.Assets.Add(item);","typeGuard":"bool IdFreeInSession(Package target, AssetId id) =>\n    target.Session?.Packages.All(p => p == target || !p.Assets.ContainsById(id)) ?? true;","tryCatchPattern":"try { package.Assets.Add(item); }\ncatch (ArgumentException e) when (e.Message.Contains(\"already in package\"))\n{ item.Id = AssetId.New(); package.Assets.Add(item); }","preventionTips":["Always regenerate ids when duplicating assets across packages","Perform cross-package moves through session-level operations","Run ContainsById checks across the whole session in copy tooling"],"tags":["assets","session","duplicate","id"],"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"}