Skip to content

Blazor insignificant whitespace trimming is done by the 5.0 SDK even when not targeting 5.0 #28085

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

Closed
poke opened this issue Nov 23, 2020 · 7 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Milestone

Comments

@poke
Copy link
Contributor

poke commented Nov 23, 2020

With Blazor in 5.0, insignificant whitespace trimming was introduced for Razor components as per #23568. However, it appears that this is a tooling feature that applies regardless of what target framework the project has.

In my case, upgrading to the .NET 5 SDK caused an existing ASP.NET Core 3.1 application (targeting netcoreapp3.1) from experiencing a broken UI due to missing whitespace between elements which is now determined to be insignificant.

The diff in my case looks something like this which causes two button elements being right next to each other instead of being separated by (collapsed) whitespace:

 // …
             __builder.CloseComponent();
-            __builder.AddMarkupContent(116, "\r\n");
 #nullable restore
 #line 62 "…\ItemDetails.razor"
         if (Item.HasChildren)
         {
 
 #line default
 #line hidden
 #nullable disable
-            __builder.AddContent(117, "            ");
-            __builder.OpenComponent<…Button>(118);
+            __builder.OpenComponent<…Button>(91);
// …

I know the ship has probably already sailed but maybe there’s a way to tell the SDK to keep the old tooling when targeting 3.1. Otherwise, aspnet/Announcements#426 should probably updated that this will already affect existing applications even when upgrading just the SDK.

@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components investigate labels Nov 23, 2020
@mkArtakMSFT mkArtakMSFT added this to the 5.0.1 milestone Nov 23, 2020
@mkArtakMSFT mkArtakMSFT modified the milestones: 5.0.1, 5.0.2 Nov 25, 2020
@springy76
Copy link

It is not the 5.0 SDK alone. I installed 5.0 SDK 2 days ago, worked yesterday all day without any visibile changes and at the end of yesterday I upgraded VS from 16.7 to 16.8 - and now this morning "everything" looked different (some parts of my app got really ugly due to missing spaces).

@springy76
Copy link

and @preservewhitespace true at the top of my blazor file is not understood (using 3.1.9 since weeks).

@SteveSandersonMS
Copy link
Member

@mkArtakMSFT Yes, it does look like this is a bug.

In Razor < 5.0, there was a more limited form of whitespace removal (just removing the leading/trailing whitespace at the top level of the component). In 5.0, we extended this to remove leading/trailing whitespace on all elements within the component too. We added an option to control whitespace preservation, with the options being "use new 5.0 behavior" or "don't strip any whitespace at all". However, when making this change, we didn't create any mechanism to continue using the 3.x limited-whitespace-stripping behavior - the code that implemented that is just gone.

To fix this, I think we'll reintroduce a forked copy of the ComponentsWhitespacePass from the 3.1 release code and start using that copy when RazorLanguageVersion < 5.0.

@SteveSandersonMS
Copy link
Member

@poke

I know the ship has probably already sailed but maybe there’s a way to tell the SDK to keep the old tooling when targeting 3.1.

If you create a global.json file at the root of your project, you can tell dotnet to keep using the 3.1 SDK even though 5.0 is also installed.

That's just a short-term workaround. For 5.0.2 we should be able to include a proper fix for this so that 3.1-targeting apps get the 3.1 behavior even when using the 5.x SDK.

@poke
Copy link
Contributor Author

poke commented Nov 27, 2020

@SteveSandersonMS Yup, pinning the SDK works fine as a workaround (that’s how I verified this behavior in the first place).

The fix for 5.0.2 looks really promising! I expected this to be a lot more complicated to get both versions working. Thanks! ☺️

@springy76
Copy link

global.json, yeah?

referencing any of 3.1.x (.400, .402, .403) results in

Found .NET SDK, but did not find dotnet.dll at [C:\Program Files\dotnet\sdk\3.1.403\dotnet.dll]
(or the according number)

dotnet --info
Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

.NET SDKs installed:
  2.1.802 [C:\Program Files\dotnet\sdk]
  3.0.101 [C:\Program Files\dotnet\sdk]
  3.1.201 [C:\Program Files\dotnet\sdk]
  3.1.400 [C:\Program Files\dotnet\sdk]
  3.1.402 [C:\Program Files\dotnet\sdk]
  3.1.403 [C:\Program Files\dotnet\sdk]
  5.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

@SteveSandersonMS
Copy link
Member

@springy76 There's something wrong with your installation of the .NET Core SDK. You may need to reinstall whatever specific SDK version you want to use. This isn't a Blazor-specific issue but rather is about the SDK itself, so if you have further trouble with that I'd recommend filing an issue at https://github.com/dotnet/sdk/issues. For reference, please see dotnet/sdk#6180.

@mkArtakMSFT mkArtakMSFT added bug This issue describes a behavior which is not expected - a bug. and removed investigate labels Dec 9, 2020
@ghost ghost added Done This issue has been fixed and removed Working labels Dec 10, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Projects
None yet
Development

No branches or pull requests

4 participants