stride3d/stride · error · InvalidOperationException

Cannot query package with dependencies when it is not…

Error message

Cannot query package with dependencies when it is not attached to a session

What it means

FillPackageDependencies walks rootPackage.Session.Packages to collect transitive dependencies, so it fundamentally requires the root package to be attached to a session (its Session back-reference must be non-null). When called on a detached package it throws InvalidOperationException instead of returning an incomplete dependency set.

Solutions

  1. Attach the package to a PackageSession (load it through the session, or add it to session.Packages) before querying dependencies.
  2. Restructure the code so dependency analysis only runs on session-managed packages.
  3. If you only need the declared dependency list, read rootPackage.Container.FlattenedDependencies directly instead of calling FindDependencies.
  4. Guard the call: if (rootPackage.Session == null) skip or load into a session first.

Example fix

// before
var package = Package.Load(path);
var deps = package.FindDependencies(...); // throws: no session

// after
using var session = new PackageSession();
session.LoadPackageOrSolution(path);
var deps = session.CurrentPackage.FindDependencies(...);
Defensive patterns

Strategy: validation

Validate before calling

if (package.Session == null)
    throw new InvalidOperationException("Load the package into a PackageSession before querying dependencies.");
var deps = package.FindDependencies(...);

Type guard

bool IsSessionAttached(Package p) => p.Session != null;

Try / catch

try { var deps = package.FindDependencies(...); }
catch (InvalidOperationException e) when (e.Message.Contains("not attached to a session"))
{ /* attach package to a session and retry */ }

Prevention

When it happens

Trigger: Calling FindDependencies (or otherwise triggering FillPackageDependencies) with a Package whose Session is null — i.e. a package loaded or constructed outside of a PackageSession and never attached to one.

Common situations: Loading a raw .sdpkg file directly for inspection and then asking for its dependencies; creating a Package in a unit test without a session; a package removed from its session but still referenced by code that queries dependencies.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at sources/assets/Stride.Core.Assets/PackageExtensions.cs:96

    {
        return packages.Any(package => package.Assets.ContainsById(assetId));
    }

    /// <summary>
    /// Determines whether the specified packages contains an asset by its location.
    /// </summary>
    /// <param name="packages">The packages.</param>
    /// <param name="location">The location.</param>
    /// <returns><c>true</c> if the specified packages contains asset; otherwise, <c>false</c>.</returns>
    public static bool ContainsAsset(this IEnumerable<Package> packages, UFile location)
    {
        return packages.Any(package => package.Assets.Find(location) != null);
    }

    private static void FillPackageDependencies(Package rootPackage, PackageCollection packagesFound)
    {
        var session = rootPackage.Session
            ?? throw new InvalidOperationException("Cannot query package with dependencies when it is not attached to a session");
        foreach (var dependency in rootPackage.Container.FlattenedDependencies.Select(x => x.Package).NotNull())
        {
            if (!packagesFound.Contains(dependency))
                packagesFound.Add(dependency);
        }
    }
}

View on GitHub (pinned to 96fad776d2)