{"record":{"id":"74992dd5f62ba26c","repo":"stride3d/stride","slug":"package-must-be-added-to-an-existing-packagesession","errorCode":null,"errorMessage":"Package must be added to an existing PackageSession","messagePattern":"Package must be added to an existing PackageSession","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Templates/TemplateGeneratorContext.cs","lineNumber":28,"sourceCode":"{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"TemplateGeneratorContext\"/> class.\n    /// </summary>\n    /// <param name=\"session\">The session.</param>\n    public TemplateGeneratorContext(PackageSession session)\n    {\n        ArgumentNullException.ThrowIfNull(session);\n        Session = session;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"TemplateGeneratorContext\"/> class.\n    /// </summary>\n    /// <param name=\"package\">The package.</param>\n    public TemplateGeneratorContext(Package package)\n    {\n        ArgumentNullException.ThrowIfNull(package);\n        if (package.Session == null) throw new ArgumentException(\"Package must be added to an existing PackageSession\", nameof(package));\n        Package = package;\n        Session = package.Session;\n    }\n\n    /// <summary>\n    /// Gets or sets the current session.\n    /// </summary>\n    /// <value>The session.</value>\n    public PackageSession Session { get; private set; }\n\n    /// <summary>\n    /// Gets or sets the current package (may be null)\n    /// </summary>\n    /// <value>The package.</value>\n    public Package? Package { get; private set; }\n}\n","sourceCodeStart":10,"sourceCodeEnd":45,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Templates/TemplateGeneratorContext.cs#L10-L45","documentation":"The TemplateGeneratorContext constructor requires a Package that already belongs to a PackageSession, because template generation needs the Session to operate. Passing a detached Package (Session == null) throws this ArgumentException after the null check.","triggerScenarios":"Calling new TemplateGeneratorContext(package) with a package created in memory (e.g. new Package()) that was never added to a PackageSession via session.Packages.Add or SavePackage.","commonSituations":"Programmatic template generation scripts that construct a Package but forget to attach it to a session; tests building packages without a session; loading a package in isolation without an active editor session.","solutions":["Create/open a PackageSession and add the package to it before constructing TemplateGeneratorContext","Load the package through PackageSession.LoadProject/Package loads so Session is set","Guard the call with `if (package.Session == null)` and attach the package first","In tests, build the session fixture and register the package before invoking the generator"],"exampleFix":"// before\nvar context = new TemplateGeneratorContext(new Package());\n// after\nvar session = new PackageSession();\nvar package = new Package();\nsession.Packages.Add(package);\nvar context = new TemplateGeneratorContext(package);","handlingStrategy":"validation","validationCode":"if (package == null) throw new ArgumentNullException(nameof(package));\nif (package.Session == null) throw new InvalidOperationException(\"Attach package to a PackageSession before generating templates\");","typeGuard":"bool IsPackageInSession(Package? p) => p?.Session != null;","tryCatchPattern":"try { var ctx = new TemplateGeneratorContext(package); } catch (ArgumentException ex) when (ex.Message.Contains(\"PackageSession\")) { session.Packages.Add(package); retry(); }","preventionTips":["Always create packages through a PackageSession","Assert package.Session != null in unit tests before template generation","Load packages via PackageSession APIs rather than constructing Package directly"],"tags":["templates","packages","argument-validation","session"],"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"}