Code works in .Net 5 but doesn't work in .Net 6

Viewed 353

I have reported this possible bug via the Visual Studio feedback tool but I have the following example code that runs in .Net 5 but doesn't work correctly in .Net 6 and I feel like this is either a bug or I'm missing something that changed between versions. I have the example code below along with the html output using the Visual Studio html visualizer from both methods. Does anyone have any possible input on this issue?

UPDATE: From different testing thanks to helpful suggestions from @GSerg I have found that this bug only happens in .Net 6 when you use html tags inside of a string interpolation or a stringbuilder and it chops off the text before the inserted variable and inserts it on the second line. I have attached a new screenshot showing this behavior using the visual studio visualizer.

var subect = "Subect Example";
var test = $"<p><strong><span style=\"font-size: 20px;\">{subject}</span></strong></p><p><span style=\"color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; " +
           $"font-size: small; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; " +
           $"text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; " +
           $"text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\">The following information is in beta testing and isn't meant for a live portfolio. " +
           $"Use this information for paper trading only until further notice.</span></p>";

.Net 5 HTML Output: enter image description here

.Net 6 HTML Output: enter image description here

.Net 6 Text Output: enter image description here

1 Answers

This seems to be a bug in Visual Studio 2022 which is still in preview so it's not unexpected. While this bug is not fixed you can use the dotnet cli to build as a workaround.

The following code reproduces the bug

string two= "2";
string test = $"1 {two} 3" 
    + $" 4" 
    + $" 5";
Console.WriteLine(test);

When building this code as .NET 6.0 with Visual Studio 2022 preview 3.1 the code is compiled to

    string two = "2";
    DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(8, 1);
    defaultInterpolatedStringHandler.AppendLiteral(" 3");
    defaultInterpolatedStringHandler.AppendFormatted(two);
    defaultInterpolatedStringHandler.AppendLiteral("1 ");
    defaultInterpolatedStringHandler.AppendLiteral(" 4");
    defaultInterpolatedStringHandler.AppendLiteral(" 5");
    string test = defaultInterpolatedStringHandler.ToStringAndClear();
    Console.WriteLine(test);

which produces the output 321 4 5

But this is specific to building .NET 6.0 with Visual Studio. When building with dotnet build The code is compiled to

    string two = "2";
    string test = "1 " + two + " 3 4 5";
    Console.WriteLine(test);

Which produces the correct output.

Related