Looking for assistance clearing the cache for updated source generators (Roslyn/ISourceGenerator)

Jeffrey Sylvan 20 Reputation points
2023-02-24T17:06:45.6366667+00:00

Issue:
I use Roslyn's source generators to generate some of my code.

 

For policy reasons, I need both my analyzer projects and the projects that consume my analyzers to be in the same solution.

 

When I compile with dotnet build, call dotnet clean, then change the behavior of the source generator, the updated behavior is not reflected in the consuming projects.

 

Backstory:

This was not a problem initially as I used Visual Studio. To make this work with Visual Studio, I simply needed to compile my analyzer project, shut down visual studio, and re-open it for the changes to be reflected in the consuming projects. This is because Visual Studio understandably loads analyzers on load.

 

Unfortunately, Visual Studio is no longer an option for reasons outside the scope of this post.

 
Current workaround

The only usable workaround I have seen is to push the project via git, delete it, then restore it from origin, then rebuild from there.

 

This is time consuming and I'm looking for a more effective manner.

 

Question:
How do i clear Roslyn's code generator cache via the command line?

At the very least I'm wondering where the cached source generators are stored when one goes through donet build. If they are stored in $env:TEMP, that may explain the issue, because due to additional policies, I do not have sufficient access to remove all files from my temp folder.

Solution from Suchiman from a chat channel:

fix:

dotnet build-server shutdown

reason:

"the way it works is, that csc.exe, the C# compiler, usually spins up a "compilation server" by the name of VBCSCompiler.exe (to avoid the JIToverhead of starting the process over and over again). csc.exe in that case just takes the parameters and forwards them via IPC to VBCSCompiler.exe to do the work. That compiler process will shut down if VS shuts down and if it's being used outside of VS, it has an idle timeout. A source generator is a compiler plugin so VBCSCompiler would load that source generator dll and execute it but it will not pick up changes to the dll once it has been loaded"
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,208 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jeffrey Sylvan 20 Reputation points
    2023-02-24T20:35:51.97+00:00

    Resolution: From "Suchiman" on a chat server from elsewhere. All credit goes to her/him.

    ``

    dotnet build-server shutdown
    

    Why this works:

    "the way it works is, that csc.exe, the C# compiler, usually spins up a "compilation server" by the name of VBCSCompiler.exe (to avoid the JIToverhead of starting the process over and over again). csc.exe in that case just takes the parameters and forwards them via IPC to VBCSCompiler.exe to do the work. That compiler process will shut down if VS shuts down and if it's being used outside of VS, it has an idle timeout. A source generator is a compiler plugin so VBCSCompiler would load that source generator dll and execute it but it will not pick up changes to the dll once it has been loaded"
    
    3 people found this answer helpful.
    0 comments No comments