Open espenrl opened 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>
@JoeRobich and @chsienki hmm, what is the best way to run source generators from a workspace?
@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
@chsienki We use MSBuildWorkspace. So I imagine it would just run when we request a Compilation.
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
?
I've had issues just getting a very basic cswin32 project building.
Steps:
New project setup
dotnet new console -o cswin32
cd cswin32
dotnet add package Microsoft.Windows.CsWin32 --prerelease
echo CreateFile > NativeMethods.txt
code .
Update cswin32.csproj by setting the TFM to net5.0 and adding <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
to the property group
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.
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
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?
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.
I'm generating .cs
files before "BeforeCompile" target, and it causes similar errors.
I'm using
CsWin32
to generate PInvoke calls. CsWin32 is based uponC# Source Generators
and it doesn't seem that the code is being generated when runningdotnet format
. See the output below.See https://github.com/microsoft/CsWin32
NOTE: I've tried using
--exclude
onGrantWindowsPrivilegeCmdlet.cs
with no luck.