C
C#14mo ago
Exeteres

✅ 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.
No description
18 Replies
333fred
333fred14mo ago
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
Exeteres
ExeteresOP14mo ago
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 anyway
333fred
333fred14mo ago
What are you actually trying to do? If you're using the Project API, you shouldn't need to interact with the driver at all
Exeteres
ExeteresOP14mo ago
I 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"
No description
333fred
333fred14mo ago
Is there a reason you're not using dotnet watch?
Exeteres
ExeteresOP14mo ago
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
333fred
333fred14mo ago
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 on
Exeteres
ExeteresOP14mo ago
For 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 MSBuildWorkspaceand 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
Exeteres
ExeteresOP14mo ago
No description
333fred
333fred14mo ago
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
Exeteres
ExeteresOP14mo ago
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
333fred
333fred14mo ago
Running a full build is the only way to be sure that everything is actually working correctly
Exeteres
ExeteresOP14mo ago
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.
333fred
333fred14mo ago
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
Exeteres
ExeteresOP14mo ago
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😎
333fred
333fred14mo ago
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
Exeteres
ExeteresOP14mo ago
Yeah, well, that makes sense I'll stick with BuildManager, thanks for the warning
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server