git-ecosystem/git-credential-manager · error · ArgumentException

A GitVersion object is required for comparison.

Error message

A GitVersion object is required for comparison.

What it means

GitVersion.CompareTo(object) accepts only GitVersion instances; comparing against any other type (including strings like "2.40.0" or ints) throws ArgumentException, per the IComparable contract. Use GitVersion.Parse for strings or the strongly-typed CompareTo(GitVersion) overload.

Solutions

  1. Compare GitVersion to GitVersion: GitVersion.Parse("2.33.0").CompareTo(version).
  2. Use typed collections (List<GitVersion>) so the generic comparer is used.
  3. In custom sort logic, call GitVersion.Parse on strings before comparison.

Example fix

// before
version.CompareTo("2.33.0") // ArgumentException
// after
version.CompareTo(GitVersion.Parse("2.33.0"))
Defensive patterns

Strategy: type-guard

Validate before calling

// before comparing:
if (obj is not GitVersion other) throw new ArgumentException(nameof(obj));
int cmp = other.CompareTo(version);

Type guard

bool isGitVersion(object o) => o is GitVersion;

Try / catch

try { result = version.CompareTo(obj); }
catch (ArgumentException) { result = version.CompareTo(GitVersion.Parse(obj.ToString())); }

Prevention

When it happens

Trigger: Calling object-typed CompareTo (e.g. via IComparable in Array.Sort, List.Sort with non-generic comparisons, or Comparer<object>) with a boxed non-GitVersion value such as a string.

Common situations: Sorting a mixed collection where one element is a string version; comparing GitVersion directly to a string literal with .CompareTo("2.33.0"); reflection-based comparisons.

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 git-ecosystem/git-credential-manager@e8ce762cd0 (2026-09-11). Data as JSON: /api/errors/df4fbba77ddc5e0f. Report an issue: GitHub.

Appendix: source

Thrown at src/Core/GitVersion.cs:72

                {
                    return ToString();
                }

                return _originalString;
            }
        }

        public int CompareTo(object obj)
        {
            if (obj is null)
            {
                return 1;
            }

            GitVersion other = obj as GitVersion;
            if (other == null)
            {
                throw new ArgumentException("A GitVersion object is required for comparison.", "obj");
            }

            return CompareTo(other);
        }

        public int CompareTo(GitVersion other)
        {
            if (other is null)
            {
                return 1;
            }

            // Compare for as many components as the two versions have in common. If a
            // component does not exist in a components list, it is assumed to be 0.
            int thisCount = _components.Count, otherCount = other._components.Count;
            for (int i = 0; i < Math.Max(thisCount, otherCount); i++)
            {
                int thisComponent = i < thisCount ? _components[i] : 0;

View on GitHub (pinned to e8ce762cd0)