dotnet / format

Home for the dotnet-format command
MIT License
1.91k stars 172 forks source link

MSBuild failures/warnings returns zero exit code #1453

Open Arthri opened 2 years ago

Arthri commented 2 years ago

I do not know of any other way to trigger this. Hence, I will demonstrate with Paket and missing references.

Reproduction

  1. Clone this repo: https://github.com/Arthri/dotnet-format-missingref. This repo cuts the steps required, but to create a similar repo, refer to Reproduction Full.
  2. Run dotnet tool restore
  3. Do NOT run dotnet paket restore. Pretend this is a mistake
  4. Run dotnet format MissingRef.sln. The command fails(output is below) to format anything, but returns a zero exit code indicating it succeeded. Therefore the mistake from earlier was not caught and this will lead to CI checks succeeding despite format errors.

Reproduction Full

  1. Create a new folder
  2. Create a new project(put sln and csproj in same folder)
  3. Install Paket and dotnet-format
  4. Run dotnet paket init
  5. Register Microsoft.Extensions.DependencyInjection to Paket, this can be done by adding "Microsoft.Extensions.DependencyInjection" into paket.references and "nuget Microsoft.Extensions.DependencyInjection" paket.dependencies.
  6. Run dotnet paket install
  7. Delete .paket. Simulate a mistake
  8. Run dotnet format [PROJECT].sln

Output

  Formatting code files in workspace 'C:\Users\[USER]\[REPO_PATH]\MissingRef.sln'.
Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.
  Format complete in 2134ms.

Diagnostic Output

  The dotnet format version is '5.1.250801+4a851ea9707f87d381166c2fc2b2d4b58a10a222'.
  The dotnet runtime version is '5.0.7'.
  The dotnet CLI version is '5.0.301'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\5.0.301\'.
  Formatting code files in workspace 'C:\Users\[USER]\[REPO_PATH]\MissingRef.sln'.
  Loading workspace.
Msbuild failed when processing the file 'C:\Users\[USER]\[REPO_PATH]\MissingRef.csproj' with message: The imported project "C:\Users\[USER]\[REPO_PATH]\.paket\Paket.Restore.targets" was not found. Confirm that the expression in the Import declaration ".paket\Paket.Restore.targets" is correct, and that the file exists on disk.  C:\Users\[USER]\[REPO_PATH]\MissingRef.csproj
  Complete in 1374ms.
  Determining formattable files.
  Complete in 7ms.
  Running formatters.
  Complete in 7ms.
  Formatted 0 of 0 files.
  Format complete in 1392ms.

Both outputs, despite failing to load then format the project, returned an unexpected zero exit code. I noticed this on a CI where it would continue "succeed" despite my local copy telling me there's format errors.

parched commented 7 months ago

This is pretty bad. Our dotnet format --verify-no-changes had started silently passing the the CI run even though there were changes. It was printing

Required references did not load for ...

but still had exit code 1

Arthri commented 1 week ago

Update: this is still an issue on .NET SDK 8.0.204