stride3d/stride · error · ArgumentException

Object must be a PackageVersion

Error message

Object must be a PackageVersion

What it means

PackageVersion implements IComparable; its non-generic CompareTo(object) accepts null (returns 1) but any non-null object that is not a PackageVersion cannot be ordered against it, so it throws an ArgumentException naming the obj parameter.

Solutions

  1. Convert/parse comparands to PackageVersion first (e.g. PackageVersion.Parse) then compare.
  2. Use the generic CompareTo(PackageVersion) path or OrderBy(v => v) on a strongly typed IEnumerable<PackageVersion>.
  3. Guard with `if (x is PackageVersion pv)` before calling CompareTo.
  4. Use explicit Comparer<PackageVersion>.Default instead of Comparer.Default on object collections.

Example fix

// before
list.Sort(); // list is List<object> containing strings
version.CompareTo("1.2.0"); // throws
// after
var versions = list.Cast<string>().Select(PackageVersion.Parse).ToList();
versions.Sort();
version.CompareTo(PackageVersion.Parse("1.2.0"));
Defensive patterns

Strategy: type-guard

Validate before calling

if (other is PackageVersion pv) result = version.CompareTo(pv); else result = version.CompareTo(PackageVersion.Parse(other?.ToString() ?? ""));

Type guard

bool IsPackageVersion(object? o) => o is PackageVersion;

Try / catch

try { cmp = version.CompareTo(obj); }
catch (ArgumentException ex) when (ex.Message.Contains("PackageVersion")) { /* convert obj via Parse, then retry */ }

Prevention

When it happens

Trigger: Calling packageVersion.CompareTo(someString) or CompareTo(someOtherType) directly, or indirectly via non-generic sorting APIs (e.g. Array.Sort on object[], Comparer.Default with mixed types) that pass non-PackageVersion items.

Common situations: Sorting heterogeneous collections from deserialized config/manifest data where versions are strings; LINQ OrderBy on object sequences; comparing a PackageVersion to a string or Version instance by mistake.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Design/PackageVersion.cs:241

    }

    private static Version NormalizeVersionValue(Version version)
    {
        return new Version(version.Major,
            version.Minor,
            Math.Max(version.Build, 0),
            Math.Max(version.Revision, 0));
    }

    public int CompareTo(object? obj)
    {
        if (ReferenceEquals(obj, null))
        {
            return 1;
        }
        if (obj is not PackageVersion other)
        {
            throw new ArgumentException($"Object must be a {nameof(PackageVersion)}", nameof(obj));
        }
        return CompareTo(other);
    }

    public int CompareTo(PackageVersion? other)
    {
        if (ReferenceEquals(other, null))
        {
            return 1;
        }

        int result = Version.CompareTo(other.Version);

        if (result != 0)
        {
            return result;
        }

View on GitHub (pinned to 96fad776d2)