{"record":{"id":"4a601e1da448330a","repo":"stride3d/stride","slug":"cannot-query-package-with-dependencies-when-it-is-not","errorCode":null,"errorMessage":"Cannot query package with dependencies when it is not attached to a session","messagePattern":"Cannot query package with dependencies when it is not attached to a session","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/PackageExtensions.cs","lineNumber":96,"sourceCode":"    {\n        return packages.Any(package => package.Assets.ContainsById(assetId));\n    }\n\n    /// <summary>\n    /// Determines whether the specified packages contains an asset by its location.\n    /// </summary>\n    /// <param name=\"packages\">The packages.</param>\n    /// <param name=\"location\">The location.</param>\n    /// <returns><c>true</c> if the specified packages contains asset; otherwise, <c>false</c>.</returns>\n    public static bool ContainsAsset(this IEnumerable<Package> packages, UFile location)\n    {\n        return packages.Any(package => package.Assets.Find(location) != null);\n    }\n\n    private static void FillPackageDependencies(Package rootPackage, PackageCollection packagesFound)\n    {\n        var session = rootPackage.Session\n            ?? throw new InvalidOperationException(\"Cannot query package with dependencies when it is not attached to a session\");\n        foreach (var dependency in rootPackage.Container.FlattenedDependencies.Select(x => x.Package).NotNull())\n        {\n            if (!packagesFound.Contains(dependency))\n                packagesFound.Add(dependency);\n        }\n    }\n}\n","sourceCodeStart":78,"sourceCodeEnd":104,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/PackageExtensions.cs#L78-L104","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Attach the package to a PackageSession (load it through the session, or add it to session.Packages) before querying dependencies.","Restructure the code so dependency analysis only runs on session-managed packages.","If you only need the declared dependency list, read rootPackage.Container.FlattenedDependencies directly instead of calling FindDependencies.","Guard the call: if (rootPackage.Session == null) skip or load into a session first."],"exampleFix":"// before\nvar package = Package.Load(path);\nvar deps = package.FindDependencies(...); // throws: no session\n\n// after\nusing var session = new PackageSession();\nsession.LoadPackageOrSolution(path);\nvar deps = session.CurrentPackage.FindDependencies(...);","handlingStrategy":"validation","validationCode":"if (package.Session == null)\n    throw new InvalidOperationException(\"Load the package into a PackageSession before querying dependencies.\");\nvar deps = package.FindDependencies(...);","typeGuard":"bool IsSessionAttached(Package p) => p.Session != null;","tryCatchPattern":"try { var deps = package.FindDependencies(...); }\ncatch (InvalidOperationException e) when (e.Message.Contains(\"not attached to a session\"))\n{ /* attach package to a session and retry */ }","preventionTips":["Always load packages through PackageSession for analysis","Never query dependencies on ad-hoc loaded packages","Detach-and-reuse patterns should re-attach before dependency queries"],"tags":["assets","session","invalid-operation","dependencies"],"backgroundTag":"invalid-state-transition","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"}