Unity-Technologies/UnityCsReference · error · ExpressionNotValidException
Invalid character '{rightSymbol}' in expression
Error message
Invalid character '{rightSymbol}' in expression What it means
Thrown by ParseExpression (VersionRanges.cs:119) when the LAST character is neither an allowed version last-character (a digit or letter, per the version type traits) nor one of the legal right delimiters ']' or ')'. It is the lexical rejection of an unsupported closing token, mirroring error 721 for the trailing position.
Source
Thrown at Editor/Mono/Scripting/ScriptCompilation/VersionRanges.cs:119
if (!versionTypeTraits.IsAllowedFirstCharacter(expression[0]))
{
leftSymbol = expression[0];
if (!Contains(k_LeftValidSymbols, leftSymbol))
{
throw new ExpressionNotValidException($"Invalid character '{leftSymbol}' in expression", expression);
}
begin++;
}
var lastChar = expression[end];
if (!versionTypeTraits.IsAllowedLastCharacter(lastChar))
{
rightSymbol = lastChar;
if (!Contains(k_RightValidSymbols, rightSymbol))
{
throw new ExpressionNotValidException($"Invalid character '{rightSymbol}' in expression", expression);
}
end--;
}
if ((leftSymbol != default(char) && rightSymbol == default(char)) ||
(leftSymbol == default(char) && rightSymbol != default(char)))
{
throw new ExpressionNotValidException("Incomplete expression, missing symbol in start or end", expression);
}
int nextVersion;
string leftVersionString = PopVersionString(expression, begin, end, out nextVersion, versionTypeTraits);
var hasLeftVersion = !string.IsNullOrEmpty(leftVersionString);
if (hasLeftVersion)
{
expressionParsedData.leftVersion = (TVersion)m_versionTypeStaticFunctionalityProxy.Parse(leftVersionString);
}View on GitHub (pinned to 225b0fbdb5)
Solutions
- Close the range with the proper delimiter: '[1.0,2.0,' -> '[1.0,2.0]'.
- Remove operator suffixes: '2.0>=' -> '(,2.0]'; '2.0<=' -> '(,2.0]'.
- Trim trailing whitespace and stray punctuation: '1.0 ' -> '1.0', '1.0.' -> '1.0'.
- Complete the version token so it does not end in '.' or '*'.
Example fix
// before "[1.0,2.0," // or "2.0<=" or "1.0 " // after "[1.0,2.0]" // or "(,2.0]" or "1.0"
Defensive patterns
Strategy: validation
Validate before calling
// Last char must be a digit, a letter, ']', or ')'.
static bool HasValidClosing(string e) =>
!string.IsNullOrEmpty(e) &&
(char.IsDigit(e[e.Length-1]) ||
VersionTypeTraitsUtils.IsCharLetter(e[e.Length-1]) ||
e[e.Length-1] == ']' || e[e.Length-1] == ')'); Type guard
static bool ClosesWithLegalToken(string e) =>
!string.IsNullOrEmpty(e) &&
(char.IsDigit(e[e.Length-1]) ||
(e[e.Length-1] >= 'a' && e[e.Length-1] <= 'z') ||
(e[e.Length-1] >= 'A' && e[e.Length-1] <= 'Z') ||
e[e.Length-1] == ']' || e[e.Length-1] == ')'); Try / catch
try { var def = ranges.GetExpression(expr); }
catch (ExpressionNotValidException ex) when (ex.Message.Contains("Invalid character")) {
ReportToUser($"{expr}: bad closing character. End with a digit, letter, ']' or ')'.");
} Prevention
- Always close ranges with ']' or ')' before leaving the field.
- Trim trailing whitespace and operator suffixes (> , =) from pasted text.
- Avoid dangling '.' or '*' at the end of a version token.
When it happens
Trigger: Expressions whose last character is '<', '>', '=', ',', whitespace, '.', '*', '+', '[', '(', etc. Concrete inputs: '[1.0,2.0,' (trailing comma, missing closer), '2.0<=' or '2.0>=', '1.0 ' (trailing space), '1.0.' (dangling dot), '1.0*' (trailing wildcard at end), '[1.0('.
Common situations: Typing a half-finished range and forgetting the closing delimiter ('[1.0,2.0,'); a trailing space from copy-paste; an operator suffix ('2.0>='); an incomplete version token ending in '.' ('1.0.').
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Invalid character '{leftSymbol}' in expression
- Incomplete expression, missing symbol in start or end
- '{value}' is not valid in the expression
- '{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/59f9c6ce67b36878.
Report an issue: GitHub.