How to use Git to Save your .NET Source Generator Output Files
Transcript
In this video I'm going to take you through how to save your source generator output into your Git repository. First question you might be thinking is why on Earth would I want to do this? And the answer is quite simply so that you can see any changes that you're making from your source generator and what effect those changes are having on your generated code. Now I do have to warn you that if you do use this approach then you could be generating potentially thousands of files and annoying your co-workers. So try and use it with care. You can ask me how I know this another time.
So if we take a look at our setup very quickly, we have a main project. This project has got nothing in it, it just serves for somewhere for us to put the source generator against. And we have the generator itself. The generator, all it does is it adds a new source file that says "hello CodeWithStu viewers." Nothing particularly fancy. We're not trying to do anything complex here. Generators are not the point of this video.
So let's take a look at the generator project. As you can see from the screen here there is nothing out of the ordinary that we need to set. All the magic happens in the main project. So if we go over to the main project now. If you've ever wondered how you can co-locate a generator and a project next to each other in the same Git repository then this is the section for you. If we take a look down at the project reference, we reference the project as normal. We ensure that the OutputItemType is equal to Analyzer. This basically makes sure that all the Roslyn bits and pieces work behind the scenes. And then we ensure that it's not referenced in the output assembly, i.e. we don't get a copy of the source generator DLL in our output.
So let's now look at the source generator magic itself. There's two parts to this. First of all is EmitCompilerGeneratedFiles. If we set this to true, your compiler will generate some files onto disk for us so we can see exactly what's going on. The second part is for us to tell the compiler exactly where to place our files once they've been generated. We do this by the CompilerGeneratedFilesOutputPath. I am placing this in the Generated folder. As you can see on the left hand side, I've used the MSBuild property TargetFramework which will be evaluated to net5.0 or net6.0, and a subfolder will be created as shown on the left. Next, underneath each of those folders, what the compiler does for you behind the scenes is it gives you the assembly name followed by the generator name itself and then we see our output file.
So if I go in and make changes now, say "hello viewers in 2023", and then I open up my main project and build it, what you'll see has happened on the left hand side is both frameworks have updated their output. And if I go and take a look at the generated output themselves, you can see that the generated output has changed. Now depending on how much your source generators generate, this may or may not be a good option for you. It can also be incredibly handy to do this in a debug kind of scenario and then exclude your Generated folder.
If you enjoyed this video, consider subscribing to the YouTube channel for more content like this.