clockworklabs/SpacetimeDB · error · ArgumentException

Argument must be a Uuid

Error message

Argument must be a Uuid

What it means

Uuid is SpacetimeDB's 128-bit UUID type. Its untyped IComparable.CompareTo(object?) overload accepts only another Uuid or null (null sorts after); any other runtime type throws ArgumentException because ordering against unrelated types is undefined.

Solutions

  1. Use the typed CompareTo(Uuid) overload or the <, > operators
  2. Pattern-match the boxed value before comparing: if (o is Uuid u)
  3. Keep collections of ids typed as Uuid[] rather than object[]

Example fix

// before
int cmp = uuid.CompareTo(boxedGuid); // ArgumentException: Argument must be a Uuid

// after
if (boxedGuid is Uuid other)
{
    int cmp = uuid.CompareTo(other);
}
Defensive patterns

Strategy: type-guard

Validate before calling

static bool CanCompareWithUuid(object? other) => other is null || other is SpacetimeDB.Uuid;

Type guard

static bool IsUuid(object? value) => value is SpacetimeDB.Uuid;

Try / catch

try
{
    var cmp = ((IComparable)uuidValue).CompareTo(boxed);
}
catch (ArgumentException)
{
    // convert to Uuid (e.g. from Guid) or reject the mixed comparison
}

Prevention

When it happens

Trigger: Calling CompareTo(object?) with a boxed Guid, string, U128, or other non-Uuid value — e.g. Array.Sort on object[] or comparing ids deserialized as object.

Common situations: Sorting mixed identifier collections; generic comparison utilities that accept object; converting Uuid to Guid or string and comparing the boxed original.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20). Data as JSON: /api/errors/1dd77a551b4e9e2e. Report an issue: GitHub.

Appendix: source

Thrown at crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs:309

        return ToGuid().ToString();
    }

    public readonly int CompareTo(Uuid other) => value.CompareTo(other.value);

    /// <inheritdoc cref="IComparable.CompareTo(object)" />
    public int CompareTo(object? value)
    {
        if (value is Uuid other)
        {
            return CompareTo(other);
        }
        else if (value is null)
        {
            return 1;
        }
        else
        {
            throw new ArgumentException("Argument must be a Uuid", nameof(value));
        }
    }

    public static bool operator <(Uuid l, Uuid r) => l.CompareTo(r) < 0;

    public static bool operator >(Uuid l, Uuid r) => l.CompareTo(r) > 0;

    public readonly partial struct BSATN : IReadWrite<Uuid>
    {
        public Uuid Read(BinaryReader reader) => new(new SpacetimeDB.BSATN.U128Stdb().Read(reader));

        public void Write(BinaryWriter writer, Uuid value) =>
            new SpacetimeDB.BSATN.U128Stdb().Write(writer, value.value);

        // --- / auto-generated ---

        // --- customized ---
        public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>

View on GitHub (pinned to 6dee26c6ef)