Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation crashes if object contains square brackets #1763

Open
yaakov-h opened this issue Feb 19, 2025 · 0 comments
Open

Interpolation crashes if object contains square brackets #1763

yaakov-h opened this issue Feb 19, 2025 · 0 comments
Labels
bug Something isn't working needs triage

Comments

@yaakov-h
Copy link

Information

  • OS: Windows
  • Version: 0.49.1
  • Terminal: Windows Terminal

Describe the bug
When calling MarkupLineInterpolated on an interpolated string, where one of the interpolation holes is an object who's string representation contains square brackets, AnsiConsole throws an unhandled exception:

System.InvalidOperationException: 'Could not find color or style 'contains,'.'

To Reproduce

using Spectre.Console;

string _string = "string";
int _int = 123;
CoolThing _other_cool_thing = new();

Console.WriteLine($"I have a {_string} and an {_int} and a {_other_cool_thing}"); // works
AnsiConsole.MarkupLineInterpolated($"I have a {_string} and an {_int} and a {_other_cool_thing}"); // crashes
AnsiConsole.MarkupLineInterpolated($"I have a {_string} and an {_int} and a {_other_cool_thing.ToString()}"); // works, triggers IDE0071

class CoolThing
{
    public override string ToString() => "This[contains, braces].";
}

Expected behavior
All three Console/AnsiConsole lines print the same text to the console and none of them crash.

Additional context
The third print statement triggers the IDE0071 code analysis rule because calling ToString() on the value inside an interpolated string is supposed to be entirely redundant.

It seems that Markup.EscapeInterpolated only escapes strings and does not consider any other values could require escaping. Thus, pre-converting the object to a string avoids the error, but then the IDE goes "why did you do that, that's not necessary" and suggests code to the user which will then crash.

I would expect that if an object is intending to supply markup it is tagged somehow to do so, and all other objects would implicitly have their output escaped. (Doing so may require a custom interpolated string handler, rather than just hooking into FormattableString.)


Please upvote 👍 this issue if you are interested in it.

@yaakov-h yaakov-h added bug Something isn't working needs triage labels Feb 19, 2025
@github-project-automation github-project-automation bot moved this to Todo 🕑 in Spectre Console Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
Status: Todo 🕑
Development

No branches or pull requests

1 participant