{"record":{"id":"c017bfeb7d723617","repo":"stride3d/stride","slug":"cannot-detach-a-project-that-was-not-attached-to-this","errorCode":null,"errorMessage":"Cannot detach a project that was not attached to this session","messagePattern":"Cannot detach a project that was not attached to this session","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/PackageSession.cs","lineNumber":1413,"sourceCode":"            // TODO CSPROJ=XKPKG: skip it in a proper way? (context info)\n            if (!VSSolution.Projects.Contains(solutionProject.VSProject))\n            {\n                // Special case: let's put executable windows project first, so that Visual Studio use them as startup project (first project in .sln)\n                var insertPosition = (solutionProject.Type == ProjectType.Executable && solutionProject.Platform == PlatformType.Windows)\n                    ? 0\n                    : VSSolution.Projects.Count;\n                VSSolution.Projects.Insert(insertPosition, solutionProject.VSProject);\n            }\n        }\n\n        packages.Add(project.Package);\n    }\n\n    private void UnRegisterProject(PackageContainer project)\n    {\n        if (project.Session != this)\n        {\n            throw new InvalidOperationException(\"Cannot detach a project that was not attached to this session\");\n        }\n\n        if (project.Package is not null)\n            packages.Remove(project.Package);\n        if (project is SolutionProject solutionProject)\n        {\n            VSSolution.Projects.Remove(solutionProject.VSProject);\n        }\n\n        project.SetSessionInternal(null);\n    }\n\n    private void PackagesCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)\n    {\n        switch (e.Action)\n        {\n            case NotifyCollectionChangedAction.Add:\n                {","sourceCodeStart":1395,"sourceCodeEnd":1431,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/PackageSession.cs#L1395-L1431","documentation":"Thrown by PackageSession.UnRegisterProject when the project being detached is not registered with this session (project.Session != this). Detachment is only valid for projects owned by the session, otherwise internal collections (packages, solution projects) would be corrupted.","triggerScenarios":"Calling a detach/unregister API for a project whose Session is null (never attached) or points to a different PackageSession instance.","commonSituations":"Detaching a project that was loaded by another session; double-detaching the same project; attempting cleanup on a fresh project object that was never attached.","solutions":["Only call detach for projects where project.Session equals the target session.","Guard the call: skip detachment when project.Session is null or foreign.","Re-attach the project to this session first if it genuinely needs to be removed from this one."],"exampleFix":"// before\nsession.UnRegisterProject(project); // project belongs to another session\n// after\nif (project.Session == session)\n    session.UnRegisterProject(project);","handlingStrategy":"validation","validationCode":"if (project.Session == session)\n    session.UnRegisterProject(project);","typeGuard":"static bool CanDetach(PackageContainer project, PackageSession session) => project.Session == session;","tryCatchPattern":"try { session.UnRegisterProject(project); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"was not attached\"))\n{\n    // skip: nothing to detach\n}","preventionTips":["Track which session loaded each project.","Skip detach when project.Session is null (never attached).","Avoid double-detach by clearing your own references after detaching."],"tags":["state-management","sessions","packages"],"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"}