Unity-Technologies/UnityCsReference · error · ExpressionNotValidException
'{value}' is not valid in the expression
Error message
'{value}' is not valid in the expression What it means
Thrown inside PopVersionString (VersionRanges.cs:171) while scanning a version-number token (between delimiters or around the comma separator) when a character is neither an allowed version character per IVersionTypeTraits.IsAllowedCharacter (digits, letters, '.', '-', and '/' for UnityVersion; digits, letters, '.', '-' for SemVersion) nor the wildcard '*'. NOTE: the exception message contains only the single offending character, not the whole expression, which makes it harder to locate.
Source
Thrown at Editor/Mono/Scripting/ScriptCompilation/VersionRanges.cs:171
if (begin > end)
{
return null;
}
int count = 0;
while (newBegin <= end)
{
var value = expression[newBegin];
if (value == ',')
{
newBegin++;
break;
}
if (!versionTypeTraits.IsAllowedCharacter(value) && value != '*')
{
throw new ExpressionNotValidException($"'{value}' is not valid in the expression");
}
count++;
newBegin++;
}
return expression.Substring(begin, count);
}
private struct ExpressionParsedData
{
public TVersion leftVersion;
public TVersion rightVersion;
public ExpressionTypeKey GenerateExpressionTypeKey;
}
}
}
View on GitHub (pinned to 225b0fbdb5)
Solutions
- Remove interior whitespace: '[1.0, 2.0]' -> '[1.0,2.0]'.
- Drop unsupported characters from version tokens: '1.0_2' -> '1.0.2'; strip '+build'.
- Keep only allowed characters (digits, letters, '.', '-', '/', and the '*' wildcard) inside version tokens.
- If you need a wildcard, use '*' as a body character (e.g. '[1.0,2.*]'); it is the only non-trait character permitted in the token body.
Example fix
// before "[1.0, 2.0]" // or "1.0_2" or "[1.0,2.0+rc1]" // after "[1.0,2.0]" // or "1.0.2" or "[1.0,2.0]"
Defensive patterns
Strategy: validation
Validate before calling
// Version-token body chars: digit, letter, '.', '-', '/', and '*' wildcard.
// Reject interior whitespace and punctuation BEFORE parsing.
static readonly char[] k_AllowedBodyExtras = { '.', '-', '/', '*' };
static bool BodyHasOnlyAllowedChars(string expr) {
if (string.IsNullOrEmpty(expr)) return false;
foreach (var c in expr) {
if (c == ',' || c == '[' || c == ']' || c == '(' || c == ')') continue;
if (char.IsDigit(c) || char.IsLetter(c)) continue;
if (System.Array.IndexOf(k_AllowedBodyExtras, c) >= 0) continue;
if (char.IsWhiteSpace(c)) return false; // interior space — the #1 cause
return false;
}
return true;
}
// Strongest check: run the full k_ValidRange regex, which has no interior spaces. Type guard
static bool HasNoInteriorSpaces(string e) =>
!string.IsNullOrEmpty(e) && !e.Contains(" ") && !e.Contains("\t"); Try / catch
// NOTE: ex.Message contains ONLY the offending char, not the expression.
// Log the expression yourself for context.
try { var def = ranges.GetExpression(expr); }
catch (ExpressionNotValidException ex) {
ReportToUser($"{expr}: character '{ex.Message}' is not allowed in a version token.");
} Prevention
- Never put a space after the comma inside a range: write '[1.0,2.0]', not '[1.0, 2.0]'.
- Strip SemVer build metadata ('+001') and underscores from version tokens before use.
- Remember the message reports only the bad char — always log the full expression alongside it.
- Use '*' only inside a token body, never as the whole token.
When it happens
Trigger: A disallowed character inside a version token: '_', '+', space, '<', '>', '=', '@', '~', etc. The single most common case is a SPACE AFTER THE COMMA inside a range ('[1.0, 2.0]') — the right-side scan hits the space, which is neither an allowed char nor '*', so it throws with value=' '. Also '1.0_2', '1.0+build', '~1.0', '1.0@2'.
Common situations: Typing '[1.0, 2.0]' with a space after the comma (the Define-Constraints trimmer does not trim interior spaces); appending SemVer build metadata '1.0+001' (not supported by this tokenizer); using an underscore inside a pre-release tag.
Related errors
- Invalid character '{leftSymbol}' in expression
- Invalid character '{rightSymbol}' in expression
- Incomplete expression, missing symbol in start or end
- '{expression}' is not a valid expression
- Unknown expression: {expression}
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/85cfa5699ccbc69f.
Report an issue: GitHub.