Unity-Technologies/UnityCsReference · error · ArgumentException

{version} is not valid Semantic Version

Error message

{version} is not valid Semantic Version

What it means

Thrown by SemVersionParser.Parse (SemVersion.cs:243, ArgumentException) when TryParse returns false. TryParse fails on null/empty input or when the major version component cannot be consumed as digits (the parser requires the string to begin with a numeric major version; minor/patch/prerelease/build are optional). This internal semver parser is used to parse Unity/package version strings during script compilation setup.

Source

Thrown at Editor/Mono/Scripting/ScriptCompilation/SemVersion.cs:243

        {
            return VersionTypeTraitsUtils.IsCharDigit(c) || VersionTypeTraitsUtils.IsCharLetter(c);
        }

        public bool IsAllowedCharacter(char c)
        {
            return VersionTypeTraitsUtils.IsCharDigit(c) || c == '.' || c == '-' || VersionTypeTraitsUtils.IsCharLetter(c);
        }
    }

    internal static class SemVersionParser
    {
        public static SemVersion Parse(string version, bool strict = false)
        {
            if (TryParse(version, out var result) && result.HasValue)
            {
                return result.Value;
            }
            throw new ArgumentException($"{version} is not valid Semantic Version");
        }

        public static bool TryParse(string version, out SemVersion? result)
        {
            if (string.IsNullOrEmpty(version))
            {
                result = null;
                return false;
            }
            int cursor = 0;

            int major = 0;
            int minor = 0;
            int patch = 0;
            string prerelease = null;
            string build = null;

            //Doing this instead because RegEx is impressively slow

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Provide a string starting with a numeric major version: "MAJOR.MINOR.PATCH" with optional '-prerelease' and '+build'.
  2. Strip non-numeric prefixes (e.g. a leading 'v') before parsing.
  3. Prefer TryParse over Parse when the input source is untrusted, and handle the false return explicitly.

Example fix

// before
var v = SemVersionParser.Parse("v1.2.3");
// after
var cleaned = raw.TrimStart('v', 'V');
var v = SemVersionParser.TryParse(cleaned, out var parsed) && parsed.HasValue
    ? parsed.Value
    : throw new ArgumentException($"'{raw}' is not a valid Semantic Version");
Defensive patterns

Strategy: validation

Validate before calling

// Prefer TryParse; only throw when you can surface a meaningful error
static SemVersion ParseStrict(string raw)
{
    var cleaned = (raw ?? string.Empty).Trim().TrimStart('v', 'V');
    if (SemVersionParser.TryParse(cleaned, out var v) && v.HasValue)
        return v.Value;
    throw new ArgumentException($"'{raw}' is not a valid Semantic Version (expected MAJOR.MINOR.PATCH)");
}

Type guard

static bool IsValidSemVer(string s)
{
    if (string.IsNullOrEmpty(s)) return false;
    return SemVersionParser.TryParse(s.TrimStart('v','V'), out var _);
}

Prevention

When it happens

Trigger: Passing a non-semver string such as "v1.0.0" (leading 'v'), "latest", "", "1.0.0-beta.2+build.1" is actually fine, but "1.x", "version-1", or any value whose first non-consumed char is non-numeric in the major slot fails.

Common situations: A package manifest or tooling feeding a version label, branch name, or human-readable string where a strict semver was expected. Leading prefix like 'v'. Pre-release-only labels without a numeric base.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/abf668767c515616. Report an issue: GitHub.