dotnet / format

Home for the dotnet-format command
MIT License
1.92k stars 171 forks source link

Fails with CsWin32 / C# Source Generators #1191

Open espenrl opened 3 years ago

espenrl commented 3 years ago

I'm using CsWin32 to generate PInvoke calls. CsWin32 is based upon C# Source Generators and it doesn't seem that the code is being generated when running dotnet format. See the output below.

See https://github.com/microsoft/CsWin32

NOTE: I've tried using --exclude on GrantWindowsPrivilegeCmdlet.cs with no luck.

 Formatting code files in workspace '/src/Solution.sln'.
Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(6,7): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(7,7): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(8,7): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(9,7): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(59,17): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(59,74): error CS0246: The type or namespace name 'PSID' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(62,31): error CS0246: The type or namespace name 'PSID' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(65,52): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(65,127): error CS0246: The type or namespace name 'PWSTR' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(69,51): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(70,25): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(76,44): error CS0246: The type or namespace name 'OBJECT_ATTRIBUTES' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(79,41): error CS0246: The type or namespace name 'HANDLE' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(88,43): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(88,69): error CS0246: The type or namespace name 'UNICODE_STRING' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(89,49): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(93,47): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(94,21): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(101,42): error CS0246: The type or namespace name 'UNICODE_STRING' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(102,41): error CS0246: The type or namespace name 'UNICODE_STRING' could not be found (are you missing a using directive or an assembly reference?) [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(110,53): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(111,59): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(115,51): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(116,25): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(117,25): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(122,17): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
/src/Automation/GrantWindowsPrivilegeCmdlet.cs(123,17): error CS0103: The name 'PInvoke' does not exist in the current context [/src/Automation/Automation.csproj]
  Formatted code file '/src/Automation/GrantWindowsPrivilegeCmdlet.cs'.
  Writing formatting report to: '/report/DotnetFormatReport.json'.
  Format complete in 11521ms.
The command '/bin/sh -c dotnet format /src/Solution.sln --no-restore --check --fix-style --fix-analyzers --report /report/DotnetFormatReport.json' returned a non-zero code: 2
espenrl commented 3 years ago

I tried emitting the generated code to disk in the obj folder. I expected that to work, but it doesn't change anything and the result is the same.

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
jmarolf commented 3 years ago

@JoeRobich and @chsienki hmm, what is the best way to run source generators from a workspace?

chsienki commented 3 years ago

@jmarolf Kittitas uses workspaces and it all just works: https://www.github.com/chsienki/Kittitas/tree/master/Kittitas%2FProgram.cs I guess because it's using MsSBuildWorkspace it correctly picks up the analyzers.

I guess dotnet-format is creating a workspace manually, so will need to read in the analyzer references itself or switch to use the MSBuildWorkspace

JoeRobich commented 3 years ago

@chsienki We use MSBuildWorkspace. So I imagine it would just run when we request a Compilation.

jmarolf commented 3 years ago

alright, we will need to look into it. I would expect that the design-time build that is run on solution would call the compiler and run the source generators. @espenrl can you give me the exact commandline options that you passed into dotnet format?

JoeRobich commented 3 years ago

I've had issues just getting a very basic cswin32 project building.

Steps:

  1. New project setup

    dotnet new console -o cswin32
    cd cswin32
    dotnet add package Microsoft.Windows.CsWin32 --prerelease
    echo CreateFile > NativeMethods.txt
    code .
  2. Update cswin32.csproj by setting the TFM to net5.0 and adding <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to the property group

  3. Update Program.cs to use PInvoke.CreateFile()

    
    using System.Windows.Sdk;

namespace cswin32 { class Program { static void Main(string[] args) { PInvoke.CreateFile(); } } }


4. Save all files and try to build from command line - `dotnet build`

Expected Output:
> error CS1501: No overload for method 'CreateFile' takes 0 arguments

Actual Output:
> error CS0234: The type or namespace name 'Sdk' does not exist in the namespace 'System.Windows'

## Running dotnet format on the cswin32.csproj
<details>
<summary>Expand to see log</summary>

```console
~/Source/cswin32> dotnet restore
  Determining projects to restore...
  Restored /Users/joeyrobichaud/Source/cswin32/cswin32.csproj (in 408 ms).
~/Source/cswin32> dotnet format ./cswin32.csproj  -wsa -v diag --binarylog --no-restore
  The dotnet format version is '5.1.225507+756d5a1c121be3e57e924788af64aa5607dc24e1'.
  The dotnet runtime version is '5.0.6'.
  The dotnet CLI version is '6.0.100-preview.4.21255.9'.
  Using MSBuild.exe located in '/usr/local/share/dotnet/sdk/5.0.203/'.
  Formatting code files in workspace '/Users/joeyrobichaud/Source/cswin32/cswin32.csproj'.
  Loading workspace.
  Project cswin32 is using configuration from '/Users/joeyrobichaud/Source/cswin32/obj/Debug/net5.0/cswin32.GeneratedMSBuildEditorConfig.editorconfig'.
  Project cswin32 is using configuration from '/usr/local/share/dotnet/sdk/5.0.203/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
  Complete in 3428ms.
  Determining formattable files.
  Complete in 369ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 2 analyzers on cswin32.
  Complete in 2032ms.
  Fixing diagnostics...
Unable to fix CS0234. Code fix GenerateTypeCodeFixProvider doesn't support Fix All in Solution.
Unable to fix CS0103. Code fix CSharpSpellCheckCodeFixProvider doesn't support Fix All in Solution.
Unable to fix CS0103. Code fix CSharpGenerateVariableCodeFixProvider doesn't support Fix All in Solution.
Unable to fix CS0103. Code fix CSharpFullyQualifyCodeFixProvider doesn't support Fix All in Solution.
Unable to fix CS0103. Code fix GenerateTypeCodeFixProvider doesn't support Fix All in Solution.
Unable to fix CS0103. Code fix GenerateMethodCodeFixProvider doesn't support Fix All in Solution.
Unable to fix CS0103. Code fix CSharpAddImportCodeFixProvider doesn't support Fix All in Solution.
  Complete in 21ms.
  Analysis complete in 2054ms.
  Running Analyzer Reference analysis.
  Determining diagnostics...
  Complete in 183ms.
  Fixing diagnostics...
  Complete in 1ms.
  Analysis complete in 184ms.
  Complete in 2980ms.
  Formatted code file '/Users/joeyrobichaud/Source/cswin32/Program.cs'.
  Formatted 1 of 3 files.
  Format complete in 6780ms.

msbuild.binlog.zip

espenrl commented 3 years ago

I discovered that the combination of --check and --fix-style triggers the issue.

This fails

dotnet format /src/Solution.sln --check --fix-style

Original command line

dotnet format /src/Solution.sln --no-restore --verbosity diagnostic --check --fix-style --fix-analyzers --report /report/DotnetFormatReport.json
jmarolf commented 3 years ago

to make sure I understand this commandline produces no warnings:

dotnet format /src/Solution.sln --no-restore --verbosity diagnostic --fix-style --fix-analyzers --report /report/DotnetFormatReport.json

but this one does?

dotnet format /src/Solution.sln --no-restore --verbosity diagnostic --check --fix-style --fix-analyzers --report /report/DotnetFormatReport.json

and you are running this against Microsoft.Windows.CsWin32.sln?

espenrl commented 3 years ago

The code is my own code. You can skip my original command line. The following reproduces the issue.

dotnet format /src/Solution.sln --check --fix-style

Those two arguments together make the compilation go bad. If I take --check away or use --fix-analyzers instead then everything works like a charm, and there is no problem with the CsWin32 source generator.

foriequal0 commented 2 years ago

I'm generating .cs files before "BeforeCompile" target, and it causes similar errors.