{"record":{"id":"37ab0cb029798320","repo":"stride3d/stride","slug":"cannot-find-the-specified-assetitem-instance-in-the-session","errorCode":null,"errorMessage":"Cannot find the specified AssetItem instance in the session","messagePattern":"Cannot find the specified AssetItem instance in the session","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/PackageSession.Extensions.cs","lineNumber":36,"sourceCode":"    {\n        // create the compile root package and package session\n        var assetPackageCloned = new Package();\n        //the following line is necessary to attach a session to the package\n        // ReSharper disable once UnusedVariable\n        _ = new PackageSession(assetPackageCloned);\n\n        AddAssetToCompilePackage(session, originalAssetItem, assetPackageCloned);\n\n        return assetPackageCloned;\n    }\n\n    public static void AddAssetToCompilePackage(this PackageSession session, AssetItem originalAssetItem, Package assetPackageCloned)\n    {\n        ArgumentNullException.ThrowIfNull(originalAssetItem);\n\n        // Find the asset from the session\n        var assetItem = originalAssetItem.Package?.FindAsset(originalAssetItem.Id)\n            ?? throw new ArgumentException(\"Cannot find the specified AssetItem instance in the session\");\n\n        // Calculate dependencies\n        // Search only for references\n        var dependencies = session.DependencyManager.ComputeDependencies(assetItem.Id, AssetDependencySearchOptions.Out | AssetDependencySearchOptions.Recursive, ContentLinkType.Reference)\n            ?? throw new InvalidOperationException(\"The asset doesn't exist in the dependency manager anymore\");\n        var assetItemRootCloned = dependencies.Item.Clone();\n\n        // Store the fullpath to the sourcefolder, this avoid us to clone hierarchy of packages\n        assetItemRootCloned.SourceFolder = assetItem.FullPath.GetParent();\n\n        if (assetPackageCloned.Assets.Find(assetItemRootCloned.Id) == null)\n            assetPackageCloned.Assets.Add(assetItemRootCloned);\n\n        // For each asset item dependency, clone it in the new package\n        foreach (var assetLink in dependencies.LinksOut)\n        {\n            // Only add assets not already added (in case of circular dependencies)\n            if (assetPackageCloned.Assets.Find(assetLink.Item.Id) == null)","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/PackageSession.Extensions.cs#L18-L54","documentation":"AddAssetToCompilePackage expects originalAssetItem to belong to a package currently loaded in the session; it re-finds the asset by Id in originalAssetItem.Package. If the package is null or the asset Id is not present, the passed AssetItem is not a live session asset and an ArgumentException is thrown.","triggerScenarios":"Calling session.AddAssetToCompilePackage(assetItem, clonedPackage) with an AssetItem that was cloned, detached, whose Package was unloaded, or whose asset was deleted/renamed in the session.","commonSituations":"Keeping stale AssetItem references across session reloads, using assets from a different session, building a compile package for an asset deleted in the editor.","solutions":["Re-query the live AssetItem from the session before calling (session.FindAsset(id))","Ensure the owning package is loaded in the session","Refresh the asset reference after any session reload or asset deletion"],"exampleFix":"// before\nsession.AddAssetToCompilePackage(staleItem, clonedPackage); // throws if stale\n// after\nvar live = staleItem.Package?.FindAsset(staleItem.Id) ?? session.FindAsset(staleItem.Id);\nif (live != null) session.AddAssetToCompilePackage(live, clonedPackage);","handlingStrategy":"validation","validationCode":"var live = session.FindAsset(originalAssetItem.Id);\nif (live == null) throw new InvalidOperationException(\"Asset is not part of this session; re-fetch it before building a compile package\");","typeGuard":"bool IsSessionAsset(PackageSession s, AssetItem i) => i?.Package != null && s.FindAsset(i.Id) != null;","tryCatchPattern":"try { session.AddAssetToCompilePackage(item, cloned); }\ncatch (ArgumentException ex) { logger.Error(ex, \"AssetItem not found in session\"); }","preventionTips":["Re-fetch AssetItems after any session reload","Don't cache AssetItem references across sessions","Validate asset existence before batch compile-package operations"],"tags":["assets","argument","state"],"backgroundTag":"entity-not-found","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"}