stride3d/stride · error · ArgumentException

Cannot find the specified AssetItem instance in the session

Error message

Cannot find the specified AssetItem instance in the session

What it means

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.

Solutions

  1. Re-query the live AssetItem from the session before calling (session.FindAsset(id))
  2. Ensure the owning package is loaded in the session
  3. Refresh the asset reference after any session reload or asset deletion

Example fix

// before
session.AddAssetToCompilePackage(staleItem, clonedPackage); // throws if stale
// after
var live = staleItem.Package?.FindAsset(staleItem.Id) ?? session.FindAsset(staleItem.Id);
if (live != null) session.AddAssetToCompilePackage(live, clonedPackage);
Defensive patterns

Strategy: validation

Validate before calling

var live = session.FindAsset(originalAssetItem.Id);
if (live == null) throw new InvalidOperationException("Asset is not part of this session; re-fetch it before building a compile package");

Type guard

bool IsSessionAsset(PackageSession s, AssetItem i) => i?.Package != null && s.FindAsset(i.Id) != null;

Try / catch

try { session.AddAssetToCompilePackage(item, cloned); }
catch (ArgumentException ex) { logger.Error(ex, "AssetItem not found in session"); }

Prevention

When it happens

Trigger: 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.

Common situations: Keeping stale AssetItem references across session reloads, using assets from a different session, building a compile package for an asset deleted in the editor.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/37ab0cb029798320. Report an issue: GitHub.

Appendix: source

Thrown at sources/assets/Stride.Core.Assets/PackageSession.Extensions.cs:36

    {
        // create the compile root package and package session
        var assetPackageCloned = new Package();
        //the following line is necessary to attach a session to the package
        // ReSharper disable once UnusedVariable
        _ = new PackageSession(assetPackageCloned);

        AddAssetToCompilePackage(session, originalAssetItem, assetPackageCloned);

        return assetPackageCloned;
    }

    public static void AddAssetToCompilePackage(this PackageSession session, AssetItem originalAssetItem, Package assetPackageCloned)
    {
        ArgumentNullException.ThrowIfNull(originalAssetItem);

        // Find the asset from the session
        var assetItem = originalAssetItem.Package?.FindAsset(originalAssetItem.Id)
            ?? throw new ArgumentException("Cannot find the specified AssetItem instance in the session");

        // Calculate dependencies
        // Search only for references
        var dependencies = session.DependencyManager.ComputeDependencies(assetItem.Id, AssetDependencySearchOptions.Out | AssetDependencySearchOptions.Recursive, ContentLinkType.Reference)
            ?? throw new InvalidOperationException("The asset doesn't exist in the dependency manager anymore");
        var assetItemRootCloned = dependencies.Item.Clone();

        // Store the fullpath to the sourcefolder, this avoid us to clone hierarchy of packages
        assetItemRootCloned.SourceFolder = assetItem.FullPath.GetParent();

        if (assetPackageCloned.Assets.Find(assetItemRootCloned.Id) == null)
            assetPackageCloned.Assets.Add(assetItemRootCloned);

        // For each asset item dependency, clone it in the new package
        foreach (var assetLink in dependencies.LinksOut)
        {
            // Only add assets not already added (in case of circular dependencies)
            if (assetPackageCloned.Assets.Find(assetLink.Item.Id) == null)

View on GitHub (pinned to 96fad776d2)