The following code throws a stack overflow exception. ```C# var formatter = new SpanFormatter(); formatter.AddFormatter<int>(x => $"Int: {x}"); Span span = formatter.Format(42); ``` The issue is the SpanFormatter [recurses on the parameter](https://github.com/dotnet/System.CommandLine/blob/master/src/System.CommandLine/Rendering/SpanFormatter.cs#L134), and each time looks up the formatter, which causes it to loop back on itself. If you specify a format parameter the problem no longer occurs: ```C# var formatter = new SpanFormatter(); formatter.AddFormatter<int>(x => $"Int: {x:F0}"); Span span = formatter.Format(42); ``` This is due to the different [code path that handles the format](https://github.com/dotnet/System.CommandLine/blob/master/src/System.CommandLine/Rendering/SpanFormatter.cs#L125-L131). We also need to consider how to handle this in the generic case. Consider what happens if a format specifier is declared on `string`. ```C# var formatter = new SpanFormatter(); formatter.AddFormatter<string>(x => $"Prefix: {x}"); Span span = formatter.Format("42"); ```
Activity
adamsitnik commentedon Jun 19, 2025
Hello!
We have decided to deprecate the System.CommandLine.Rendering package: #2576 (comment)
We recommend using https://www.nuget.org/packages/Spectre.Console which became the golden standard in the .NET Ecosystem