Search code examples
asp.net-coreazure-pipelinesasp.net-core-3.1razorgeneratorc#-9.0

Azure DevOps: “rzc generate exited with code 2”


I have an ASP.NET Core 3.x project that is built and deployed using Azure DevOps Pipelines. After upgrading the project to use C# 9.0—but not ASP.NET Core 5.0—my dotnet build (DotNetCoreCLI@2) task warns:

Unknown C# language version 9.0.

Error

Immediately after the warning, the task fails with the following error:

##[error]C:\Program Files\dotnet\sdk\3.1.404\Sdks\Microsoft.NET.Sdk.Razor\build\netstandard2.0\Microsoft.NET.Sdk.Razor.CodeGeneration.targets(150,5): Error : rzc generate exited with code 2.

Task

There's nothing particularly special about my build task, but for completeness:

- task: DotNetCoreCLI@2
  displayName: 'dotnet build'
  inputs:
    projects: '*.sln'
    arguments: '/p:configuration=$(BuildConfiguration) --no-restore'

Conditions

It's worth noting that:

  • The project builds fine using dotnet build on my local machine.
  • If I remove the ASP.NET Core 3.x project, leaving only my .NET Core 3.x class libraries, everything builds fine.
  • If I replace the DotNetCoreCLI@2 task with a VSBuild@1 task, everything also builds fine.

Given that, this issue only occurs when the project meets all of the following conditions:

  • The project uses the either the Microsoft.NET.Sdk.Web or the Microsoft.NET.Sdk.Razor SDK.
  • The project uses a TargetFramework of either netcoreapp3.0 or netcoreapp3.1.
  • The project uses a LangVersion of 9.0.
  • The project is built using the Azure DevOps DotNetCoreCLI@2 task.
<Project Sdk="Microsoft.NET.Sdk.Razor">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>9.0</LangVersion>
  </PropertyGroup>
</Project>

Any idea on how to resolve this issue?


Solution

  • This issue isn't specific to Azure DevOps Pipelines. Instead, the issue appears to be due to an incompatibility between the Razor compiler built into the .NET 3.x SDK and C# 9.0. This necessitates the availability of the .NET 5.x SDK in order to compile a project with Razor views and C# 9.0—even if that project itself is a .NET 3.x project.

    Azure DevOps Pipelines

    As this question is specifically about Azure DevOps Pipelines, this can be resolved by explicitly adding the Use .NET Core SDK task to your pipeline and configuring it use the .NET 5.x SDK:

    - task: UseDotNet@2
      displayName: 'Use .NET Core SDK 5.x'
      inputs:
        version: 5.x
        includePreviewVersions: true
    

    Notes

    • Even though .NET 5 is released, you still must set includePreviewVersions currently.

    • The .NET 5 SDK can still build the project, despite it being an ASP.NET Core 3.x project.

    • Other tasks in your pipeline (such as gitversion, in my case) may explicitly depend on the .NET Core 3.x SDK; if so, you'll need to install that as well:

      steps:
      - task: UseDotNet@2
        displayName: 'Use .NET Core SDK 3.x'
        inputs:
          version: 3.x
      

    This step is probably obvious if you've upgraded your project to use .NET 5.0 and ASP.NET Core 5.0, but may not be obvious if you're still using .NET 3.x and have only upgraded to C# 9.0—and, in fact, it still isn’t necessary if your .NET project doesn’t need to compile Razor.