Search code examples
c#csharp-source-generator

Can Source Generator created files be physically reached?


Is there any standard method/option, how to arrange the files generated via Source Generators and added to the build process are also physically visible in the project structure (so developer can review the result code e.g. via VS solution explorer window)?

I mean other than I save the code directly to a file in the generator's Execute(), which I would expect might bring other complications (not tried yet).

EDIT:

  • According to this: https://github.com/dotnet/roslyn/issues/49249#issuecomment-809807528, no I/O is allowed within generator execution.
  • According to debug session (entering the generated code function), the generated source is at least available in the temp folder, like C:\Users\user\AppData\Local\Temp\.vsdbgsrc\some-guid\FileNameHint.cs. Unfortunately standard VS navigation (F12) does not work (regardless of debug session activity).

Solution

  • Add following to the project using the generator:

    <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>DesiredPathForGeneratedStuff</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>