{"record":{"id":"abf668767c515616","repo":"Unity-Technologies/UnityCsReference","slug":"version-is-not-valid-semantic-version","errorCode":null,"errorMessage":"{version} is not valid Semantic Version","messagePattern":"(.+?) is not valid Semantic Version","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Scripting/ScriptCompilation/SemVersion.cs","lineNumber":243,"sourceCode":"        {\n            return VersionTypeTraitsUtils.IsCharDigit(c) || VersionTypeTraitsUtils.IsCharLetter(c);\n        }\n\n        public bool IsAllowedCharacter(char c)\n        {\n            return VersionTypeTraitsUtils.IsCharDigit(c) || c == '.' || c == '-' || VersionTypeTraitsUtils.IsCharLetter(c);\n        }\n    }\n\n    internal static class SemVersionParser\n    {\n        public static SemVersion Parse(string version, bool strict = false)\n        {\n            if (TryParse(version, out var result) && result.HasValue)\n            {\n                return result.Value;\n            }\n            throw new ArgumentException($\"{version} is not valid Semantic Version\");\n        }\n\n        public static bool TryParse(string version, out SemVersion? result)\n        {\n            if (string.IsNullOrEmpty(version))\n            {\n                result = null;\n                return false;\n            }\n            int cursor = 0;\n\n            int major = 0;\n            int minor = 0;\n            int patch = 0;\n            string prerelease = null;\n            string build = null;\n\n            //Doing this instead because RegEx is impressively slow","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Scripting/ScriptCompilation/SemVersion.cs#L225-L261","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Provide a string starting with a numeric major version: \"MAJOR.MINOR.PATCH\" with optional '-prerelease' and '+build'.","Strip non-numeric prefixes (e.g. a leading 'v') before parsing.","Prefer TryParse over Parse when the input source is untrusted, and handle the false return explicitly."],"exampleFix":"// before\nvar v = SemVersionParser.Parse(\"v1.2.3\");\n// after\nvar cleaned = raw.TrimStart('v', 'V');\nvar v = SemVersionParser.TryParse(cleaned, out var parsed) && parsed.HasValue\n    ? parsed.Value\n    : throw new ArgumentException($\"'{raw}' is not a valid Semantic Version\");","handlingStrategy":"validation","validationCode":"// Prefer TryParse; only throw when you can surface a meaningful error\nstatic SemVersion ParseStrict(string raw)\n{\n    var cleaned = (raw ?? string.Empty).Trim().TrimStart('v', 'V');\n    if (SemVersionParser.TryParse(cleaned, out var v) && v.HasValue)\n        return v.Value;\n    throw new ArgumentException($\"'{raw}' is not a valid Semantic Version (expected MAJOR.MINOR.PATCH)\");\n}","typeGuard":"static bool IsValidSemVer(string s)\n{\n    if (string.IsNullOrEmpty(s)) return false;\n    return SemVersionParser.TryParse(s.TrimStart('v','V'), out var _);\n}","tryCatchPattern":null,"preventionTips":["Normalize inputs: strip a leading 'v'/'V' and whitespace before parsing.","Use TryParse for any version string sourced from user input, branches, or manifests.","Constrain version fields at the input boundary (regex ^v?\\d+) rather than relying on Parse to reject."],"tags":["unity","script-compilation","semver","parsing","versioning"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}