stride3d/stride · error · InvalidOperationException

A PackageViewModel cannot contain two isntances of…

Error message

A PackageViewModel cannot contain two isntances of DependencyCategoryViewModel

What it means

PackageViewModel's Content collection comparer assumes at most one DependencyCategoryViewModel item. The sort comparison throws InvalidOperationException when two DependencyCategoryViewModel instances are compared, which would indicate a corrupted Content collection.

Solutions

  1. Ensure only one DependencyCategoryViewModel is added to Content
  2. Reuse the existing dependencies category instance
  3. Deduplicate Content items before sorting

Example fix

// before
content.Add(new DependencyCategoryViewModel(serviceProvider, package));
// after
if (!content.OfType<DependencyCategoryViewModel>().Any()) content.Add(new DependencyCategoryViewModel(serviceProvider, package));
Defensive patterns

Strategy: validation

Validate before calling

if (content.OfType<DependencyCategoryViewModel>().Count() > 1) throw new InvalidOperationException("Duplicate DependencyCategoryViewModel in Content");

Type guard

bool HasSingleDependencyCategory(System.Collections.IEnumerable c) => c.OfType<DependencyCategoryViewModel>().Take(2).Count() < 2;

Try / catch

try { SortContent(content); } catch (InvalidOperationException) { /* deduplicate and re-sort */ }

Prevention

When it happens

Trigger: Adding a second DependencyCategoryViewModel to PackageViewModel.Content, causing the sort comparer to encounter xDependencies and yDependencies both non-null.

Common situations: Custom plugins or re-initialization code inserting a duplicate dependencies category into the Content collection.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at sources/editor/Stride.Core.Assets.Editor/ViewModel/PackageViewModel.cs:791

            if (xAssets != null)
            {
                if (yAssets != null)
                    return string.Compare(xAssets.Name, yAssets.Name, StringComparison.InvariantCultureIgnoreCase);
                return -1;
            }
            if (xProject != null)
            {
                if (yProject != null)
                {
                    return xProject.CompareTo(yProject);
                }
                return yAssets != null ? 1 : -1;
            }
            if (xDependencies != null)
            {
                if (yDependencies != null)
                    throw new InvalidOperationException("A PackageViewModel cannot contain two isntances of DependencyCategoryViewModel");
                return 1;
            }
            throw new InvalidOperationException("Unable to sort the given items for the Content collection of PackageViewModel");
        }
    }
    public abstract class PickablePackageViewModel : DispatcherViewModel
    {
        protected PickablePackageViewModel([NotNull] IViewModelServiceProvider serviceProvider)
            : base(serviceProvider)
        {
        }

        public abstract string Name { get; }

        public abstract DependencyRange DependencyRange { get; }
    }

    public class LoadedPickablePackageViewModel : PickablePackageViewModel

View on GitHub (pinned to 96fad776d2)