format: Cannot `dotnet format` ASP.NET Core projects

After upgrading to 6.0.2 (6.0.200) I got errors when trying to format projects using the web SDK, i.e.

<Project Sdk="Microsoft.NET.Sdk.Web">

Initially I assumed that I had something wrong or unexpected in the project I was working on and/or in my environment, as the error is basically the same as that in https://github.com/dotnet/format/issues/1500; unfortunately in my case even removing all other SDKs did not fix the issue.

EDIT: since the report, new docker images have been released; the reproducer is now much simpler: https://github.com/dotnet/format/issues/1519#issuecomment-1043607496

I tried to reproduce it using one of the standard docker images, but the latest ones are 6.0.102, not 6.0.200, so I wrote this Dockerfile. Its first stage is based on https://github.com/dotnet/dotnet-docker/blob/17971807fc0f4ff5a0eca53deb2d85fff990d95c/src/sdk/6.0/bullseye-slim/amd64/Dockerfile updating just DOTNET_SDK_VERSION and dotnet_sha512 The second stage is the actual reproducer: it creates a simple webapp (from the template) and tries to format it

ARG REPO=mcr.microsoft.com/dotnet/aspnet
FROM $REPO:6.0.2-bullseye-slim-amd64 AS sdk-6.0.200

ENV \
    # Unset ASPNETCORE_URLS from aspnet base image
    ASPNETCORE_URLS= \
    # Do not generate certificate
    DOTNET_GENERATE_ASPNET_CERTIFICATE=false \
    # Do not show first run text
    DOTNET_NOLOGO=true \
    # SDK version
    DOTNET_SDK_VERSION=6.0.200 \
    # Enable correct mode for dotnet watch (only mode supported in a container)
    DOTNET_USE_POLLING_FILE_WATCHER=true \
    # Unset Logging__Console__FormatterName from aspnet base image
    Logging__Console__FormatterName= \
    # Skip extraction of XML docs - generally not useful within an image/container - helps performance
    NUGET_XMLDOC_MODE=skip \
    # PowerShell telemetry for docker image usage
    POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-DotnetSDK-Debian-11

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        curl \
        git \
        wget \
    && rm -rf /var/lib/apt/lists/*

# Install .NET SDK
RUN curl -fSL --output dotnet.tar.gz https://dotnetcli.azureedge.net/dotnet/Sdk/$DOTNET_SDK_VERSION/dotnet-sdk-$DOTNET_SDK_VERSION-linux-x64.tar.gz \
    && dotnet_sha512='334f3ea4bfeb736bed61c5896796a09d640b0ae74605c514edc5869c395befb7cfc795b58c922f14560e7d41c89c073c62ed01eefc6d9f13aa916e3478949c24' \
    && echo "$dotnet_sha512  dotnet.tar.gz" | sha512sum -c - \
    && mkdir -p /usr/share/dotnet \
    && tar -oxzf dotnet.tar.gz -C /usr/share/dotnet ./packs ./sdk ./sdk-manifests ./templates ./LICENSE.txt ./ThirdPartyNotices.txt \
    && rm dotnet.tar.gz \
    # Trigger first run experience by running arbitrary cmd
    && dotnet help

# Install PowerShell global tool
RUN powershell_version=7.2.1 \
    && curl -fSL --output PowerShell.Linux.x64.$powershell_version.nupkg https://pwshtool.blob.core.windows.net/tool/$powershell_version/PowerShell.Linux.x64.$powershell_version.nupkg \
    && powershell_sha512='bfd0fac3fe5e905156d28433dbf4978d5cea69a4f1b63cb5b8d903f865f0febf4a1b0476a5b84a8e3509b354c44b0cd9e79b31a105176f03b90693ff51c7bb0b' \
    && echo "$powershell_sha512  PowerShell.Linux.x64.$powershell_version.nupkg" | sha512sum -c - \
    && mkdir -p /usr/share/powershell \
    && dotnet tool install --add-source / --tool-path /usr/share/powershell --version $powershell_version PowerShell.Linux.x64 \
    && dotnet nuget locals all --clear \
    && rm PowerShell.Linux.x64.$powershell_version.nupkg \
    && ln -s /usr/share/powershell/pwsh /usr/bin/pwsh \
    && chmod 755 /usr/share/powershell/pwsh \
    # To reduce image size, remove the copy nupkg that nuget keeps.
    && find /usr/share/powershell -print | grep -i '.*[.]nupkg$' | xargs rm

# my reproducer

FROM sdk-6.0.200 AS my-test

WORKDIR /app
RUN dotnet new webapp
RUN dotnet format -v diag --verify-no-changes

This reliably reproduces the issue and results in the errors:

Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

I am unsure if this is actually a dotnet format problem or some kind of packaging issue of the SDK. In the second case, sorry for the noise 😇

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 49
  • Comments: 36 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Is there an eta for a fix on this? There are a lot of issues being opened and this seems like a blocking issue for many.

This will be fixed tin the 6.0.202 SDK release. I will leave this issue open until that release is downloadable.

The matters are being conflated - Generally the CI/CD process where dotnet format gets run and where an app is hosted are different environments. The patch addresses a DoS vulnerability in runtime/hosting environments which generally isn’t a vulnerability in CI/CD processes.

However, that same patch shouldn’t break shouldn’t break everyone’s CI/CD processes that run dotnet format.

What you describe in installing the dotnet-format global tool is a workaround, and it may be a valid, good workaround, but it doesn’t “Solve the problem”

The reason that myself and possibly others are here is because the dotnet format commands in my CI/CD pipelines are now broken because of this patch release and that needs to be addressed.

Install dotnet-format dotnet tool install --global dotnet-format and use dotnet-format, to solve the problem.

The .NET 6.0.202 SDK has been released. The SDK-provided dotnet format tool works again in my environment (both in docker and directly) 🚀

I started noticing this problem yesterday. We have a test automation framework based on .NET 6 that gets built in Azure DevOps. We have a pipeline that runs to build the framework and check formatting whenever someone submits a PR.

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
  - task: UseDotNet@2
    displayName: 'Use .NET 6 SDK'
    inputs:
      version: 6.0.x
      packageType: sdk
      includePreviewVersions: false

  - task: DotNetCoreCLI@2
    displayName: 'Restore Packages'
    inputs:
      command: 'restore'

  - task: DotNetCoreCLI@2
    displayName: 'Format Check'
    inputs:
      command: 'custom'
      custom: 'format'
      arguments: '--severity warn --verify-no-changes'

  - task: DotNetCoreCLI@2
    displayName: 'Build Test Framework'
    inputs:
      command:  'build'

As you can see, the pipeline uses whatever the most recent version of the SDK is. Since 6.0.200, the format task is generating this error:

File name: 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

I was able to replicate the same thing on my local machine once I upgraded to 6.0.200. The framework itself is a pretty simple solution with four projects using using Microsoft.NET.Sdk.

@lsaudon The dotnet format command is now part of the dotnet 6.0 sdk so installing the separate dotnet-format tool should not be required - and indeed this worked with the 6.0.102 release - breaking changes should not be introduced in patch releases.

I don’t see any actual release notes for 6.0.201, but if anyone was hopeful, this is still broken.

A workaround until new SDKs are released would be to install dotnet-format as a global tool and invoke it with dotnet-format (the hyphen is important).

If you are only using .NET 6 SDKs: dotnet tool install -g dotnet-format --version "6.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json

If you are previewing .NET 7 SDKs: dotnet tool install -g dotnet-format --version "7.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json

@lsaudon The dotnet format command is now part of the dotnet 6.0 sdk so installing the separate dotnet-format tool should not be required - and indeed this worked with the 6.0.102 release - breaking changes should not be introduced in patch releases.

I know dotnet format is a part of the dotnet 6.0, but what is more important security patch or format code ? I think is a simple solution for use latest version of dotnet and use format too.

Also seeing this issue since I upgrade my sdk to 6.0.200

Any updates?

as pointed out in #1521 docker images for 6.0.200 have been released, so the reproducer is now much simpler:

FROM mcr.microsoft.com/dotnet/sdk:6.0.200 AS my-test

WORKDIR /app
RUN dotnet new webapp
RUN dotnet format -v diag --verify-no-changes

(note that this is the full Dockerfile, not the second stage of the build)

This issue is the same as #1433. @JoeRobich has been active in that issue and you can track the updates there.

Servicing runs on a monthly cadence, so if it’s not in the next patch-Tuesday release, it’s likely going to be in the release after that.

I’m seeing this issue in repos without any ASP.NET Core projects. Hoping this can be fixed soon to unblock moving to newer SDK versions supporting Visual Studio 17.1.

Also seeing this issue.

What I found: It still works for class library/console projects with

<Project Sdk="Microsoft.NET.Sdk">

It does not work for

<Project Sdk="Microsoft.NET.Sdk.Web">

as @ranma42 already mentioned.

I’ve had to completely disable linting in my pipeline because this just keeps happening. Disappointing. I never had issues when the tool was separate.