templating: File encoding is not maintained when first line of file is a conditional statement

The file generated after doing dotnet new has a different encoding than what was in the original file for the template.

Repro:

  • Install dotnet 3.1 sdk
  • Unzip C:\Program Files\dotnet\templates\3.1.0\microsoft.dotnet.web.projecttemplates.3.1.3.1.0.nupkg and go to Web.ProjectTemplates/content/BlazorServerWeb-Csharp/
  • Verify the encoding of App.razor (It should be UTF-8 with BOM)
  • Now create a new app with dotnet new blazor
  • Open the generated App.razor and check its encoding

Expected: UTF-8 with BOM

Actual: Windows Codepage 1252

cc @phenning

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for fixing the issue @grinrag. Do you know what version of the .NET Core SDK this fix would appear in?

This looks like a bug when the first line of the processed file contains a conditional construct such as #if.

I just verified that either adding a blank line to the top of the file, or moving the #if to the next line as follows results in the encoding being preserved.

@*
#if (NoAuth)
<Router AppAssembly="@typeof(Program).Assembly">

@ajaybhargavb This could be used as a a workaround in the template content until the root cause of underlying bug is found and fixed.