✅ Roslyn Emit API and Source Generators
Hello guys, I'm looking for someone who has some experience with Roslyn API.
I want to track file changes inside a project and emit a new assembly each time.
The problem here is that just changing the syntax trees of the source code (
ReplaceSyntaxTree
) does not change the dependent syntax trees generated by source generators.
So, the compilation uses outdated generated syntax trees and fails.
For now, I believe that I can regenerate missing/outdated files by implicitly using GeneratorDriver
, but I have not find a good way to access the driver of the project/solution.
I hope you will have some ideas.18 Replies
First, #roslyn is the better place to ask questions
Second, you're not using the returned compilation from RunGenerators
You're using whatever _currentCompilation is
It is just an example
Yeah, it should be
compilation
, of course
But for now I have no idea how to access this driver, so this code will not work anywayWhat are you actually trying to do? If you're using the
Project
API, you shouldn't need to interact with the driver at allI have a file system watcher that reacts on file changes and I want to emit a new assembly on each file change
It is needed in my project for something like "hotreload"
Is there a reason you're not using
dotnet watch
?Yep
I do not want to run this code, I want to build it and push to the remote server which will load it, analyze and use to handle some requests
I am building something like cloud functions in firebase btw
Then I would completely avoid using roslyn directly
Rather, I'd invoke
dotnet build
There are lots of things that a pure roslyn solution is not going to pick up on, or will not be easy to make it pick up onFor this reason I use both of them
At first I programmaticly invoke msbuild using BuildManager API and it restores all packages and do other preparation tasks
And then I load it using
MSBuildWorkspace
and manually update Compilation
on file change events to achieve instant updates on code changes
It works perfectly except the one thing: source generators does not automatically run on changes like ReplaceSyntaxTree
I would seriously avoid using roslyn directly here
Like, yes, you can make this work. You need to update the solution with the new compilation, you can't get access to the GeneratorDriver that the solution was using
But it is seriously not worth the effort
That would be really cool, though
Running a full build after every change doesn't sound so cool
I just feel like I've already got it working, all that's left is to solve the issue with source generators
Running a full build is the only way to be sure that everything is actually working correctly
Okay, thanks anyway.
I only wanted to do that because instant updates (hundreds of milliseconds instead of a few seconds) sounds like a pretty good killer feature
Maybe it's really not worth it.
I could see if you were talking about multiple minutes, but a few seconds? You're going to see longer variable latency on deploying to your cloud service
The longest latency now is a network latency anyway
I have a real experimental results where the whole process takes less than a second (on this screenshot the "real" remote server is used, not localhost)
Roslyn has a very cool infrastructure, I just wanted to make the most of it
I'm just a perfectionist😎
The problem is that the perfectionist approach is to realize that you may need to account for people having incrementallity bugs in their source generators such that running the pipeline incrementally will produce different results than running the whole thing from the start
Yeah, well, that makes sense
I'll stick with BuildManager, thanks for the warning
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.