spectreconsole/spectre.console · error · InvalidOperationException
Encountered unknown markup token.
Error message
Encountered unknown markup token.
What it means
A defensive guard in the AnsiMarkup.Parse token loop: the token's Kind is none of Open, Close, or Text. Because MarkupTokenKind only defines those three values and the tokenizer only ever yields them, this branch is effectively unreachable under normal operation.
Source
Thrown at src/Spectre.Console.Ansi/AnsiMarkup.cs:100
link = linkStack.Pop();
}
else if (token.Kind == MarkupTokenKind.Text)
{
if (result.Count > 0 && result[^1].Style.Equals(style) && Equals(result[^1].Link, link))
{
// Merge segments with same style and link
result[^1].Text += token.Value;
}
else
{
result.Add(
new AnsiMarkupSegment(
token.Value, style.Value, link));
}
}
else
{
throw new InvalidOperationException("Encountered unknown markup token.");
}
}
if (styleStack.Count > 0)
{
throw new InvalidOperationException("Unbalanced markup stack. Did you forget to close a tag?");
}
// Try resolving auto links (if any)
ResolveAutoLinks(result);
return result;
}
private static void ResolveAutoLinks(List<AnsiMarkupSegment> segments)
{
for (var segmentIndex = 0; segmentIndex < segments.Count; segmentIndex++)
{View on GitHub (pinned to 0acc92fada)
Solutions
- Treat occurrence as an internal library bug and report it upstream with the input and stack trace
- If you have forked/extended MarkupTokenKind, add a matching case in AnsiMarkup.Parse
- Catch InvalidOperationException defensively when rendering untrusted markup
Defensive patterns
Strategy: try-catch
Try / catch
try { var segments = AnsiMarkup.Parse(markup); }
catch (InvalidOperationException ex) when (ex.Message == "Encountered unknown markup token.")
{ /* internal/defensive: report upstream and fall back to plain rendering */ } Prevention
- If you fork MarkupTokenKind, add the matching case in AnsiMarkup.Parse
- Report an unprovoked occurrence as a library bug with a reproducer
When it happens
Trigger: Not producible by any valid or invalid input string through the public API. Could only fire if MarkupTokenKind were extended with a new member, or a custom/reflective token source injected an unexpected enum value.
Common situations: Effectively never seen by library consumers; it is an exhaustiveness safeguard against future enum additions or corrupted token state.
Related errors
- Could not parse style.
- Encountered closing tag when none was expected near position
- Unbalanced markup stack. Did you forget to close a tag?
- Encountered unescaped ']' token at position {_reader.Positio
- Encountered malformed markup tag at position {pos}.
AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13).
Data as JSON: /api/errors/851e465dde648dc8.
Report an issue: GitHub.