razor: [8.0.100-rc.2.23469.4] "error CS8938: The #line directive value is missing or out of range" exception in MVC app when generating html with Html Helpers

Issue Description:

When retargeting SmartStore (.NET 7.0) and grandnode (.netcore 3.1) apps to .NET 8.0, we get “error CS8938: The #line directive value is missing or out of range” exception for cshtml.g.cs files during building. This exception starts from dotnet-sdk-8.0.100-rc.2.23456.6.

Application Name: Smartstore, grandnode OS: Windows 10 21H2 CPU: X64 .NET Build Number: dotnet-sdk-8.0.100-rc.2.23469.4

Verify Scenarios:

  1. Windows10 21h2 x64 + dotnet-sdk-8.0.100-preview.7.23374.6: Pass
  2. Windows10 21h2 x64 + dotnet-sdk-8.0.100-rc.1.23455.8: Pass
  3. Windows10 21h2 x64 + dotnet-sdk-8.0.100-rc.2.23454.1: Pass
  4. Windows10 21h2 x64 + dotnet-sdk-8.0.100-rc.2.23456.6: Fail
  5. Windows10 21h2 x64 + dotnet-sdk-8.0.100-rc.2.23469.4: Fail
  6. Windows10 21h2 x64 + dotnet-sdk-8.0.100-rc.2.23472.8: Fail

Minimal Repro steps (Demo attached): myapp.zip

The machine has dotnet-sdk-8.0.100-rc.2.23456.6 or higher version installed. ​​1. Open command prompt window to create a MVC project with command: “dotnet new mvc” 2. Open “\Views\Home\Index.cshtml” page file to add below code:

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
@(Html.TextBox("mytestbox")
)  
</div>
  1. Build the project.

Expected Result: Build successful.

Actual Result: Build failed with following exception: C:\Users\v-weiwc\Desktop\myapp\myapp\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators. RazorSourceGenerator\Views_Home_Index_cshtml.g.cs(48,17): error CS8938: The #line directive value is missing or out of range [C:\Users\v-weiwc\Desktop\myapp\myapp\myapp.csproj]

Findings:

  1. This error start from dotnet-sdk-8.0.100-rc.2.23456.6, previous .NET 8.0 version don’t have this build issue.
  2. This error occurs with both VS2022 Preview (Version 17.8.0 Preview 2.0) and dotnet build command.
  3. If we use VS2022 with “Use previews of the .NET SDK” enabled, and the machine has .NET 8.0 SDK installed, it can be built successfully in VS2022, but still failed with dotnet build command.

@dotnet-actwx-bot @dotnet/compat

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 25 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, this should only affect code formatted in a way where you have a parenthesis ) on a separate line, like:

@(Html.TextBox("mytestbox")
)

If you change that to the following, the error should go away.

@(Html.TextBox("mytestbox"))

Anyway, this should be fixed in the next release of .NET 8.

Thanks all for your patience while we got a workaround for this issue ready to go. We’ve published a toolset razor compiler package that can be installed to fix the build from the command line. Add this reference to the csproj of your project:

<PackageReference Include="Microsoft.Net.Compilers.Razor.Toolset" Version="7.0.0-preview.23512.5">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

Make sure to remove this package after you upgrade to the next version of .NET 8: it’s only needed for RC2 due to our fixes for this issue not quite making the final build.

Thanks all for your patience while we got a workaround for this issue ready to go. We’ve published a toolset razor compiler package that can be installed to fix the build from the command line. Add this reference to the csproj of your project:

<PackageReference Include="Microsoft.Net.Compilers.Razor.Toolset" Version="7.0.0-preview.23512.5">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

Make sure to remove this package after you upgrade to the next version of .NET 8: it’s only needed for RC2 due to our fixes for this issue not quite making the final build.

This fixed things for me.