I’m working on a source generator in Unity 2022.3.12f1, and I’d like my source generator to generate a series of source files based on the contents of a json file. In order to include this json file so that it can be read in my source generator, my understanding is that I have two options:
Use a csc.rsp file (these seem to be scope-able?) for every asmdef that references my source generator. Since I’m planning on distributing my source generator along with a package, I would need to generate this automatically, which seems likely to break in the future. It also seems very fragile to just add random csc.rsp files to referencing projects and their related asmdef’s.
Use the .additionalfile feature in Unity. I’ve tried this, but it seems to be adding my additional file to every single package in my solution. This would generate the same types over and over again in all of my packages. Since these are intended to be public types, this doesn’t seem like it would work.
I’m alright with the .additionalfile approach, but I’d like it to only be included as an additional file for the particular asmdef that the additional file is scoped under. Is this possible? Are there other options that I’m not listing above? Is there something in a newer version that would help?
My only idea for a workaround would be to include something in the additional file itself that silences the output in all assemblies except for the one it’s actually scoped under. But that doesn’t seem like a good option, since the unused files would still be included in many other places.
I have been wondering about the generator being called for each and every assembly myself. Why does it run for every assembly? Why does no example even mention that it runs for every assembly?
The simple examples I found simply generate the code every time the generator runs. I’ve added a check to generate my script only if it matches a specific assembly name and that did not break the code generation. However I haven’t checked what happens if you need to refer to that code in another asmdef.
So I’m sort of thinking that the tutorials - once again - simply throw efficiency over board in order to remove a single if statement. I still need to dig deeper into this but my assumption is that you are supposed to be specific with when you generate your code inside the generator callback, since the callback runs for each and every assembly.
The fact that the .additionalfile appear in ever call of the generator method is another reason for me to believe that my hypothesis is correct.
Would be nice if you could report with what you went along with.
I agree that scoping analyzers is a bit strange, but it’s not a core part of my original question. Ultimately, you can have two asmdefs/packages that reference the source generation asmdef/package. This is a totally normal state of affairs and you’d expect the source generator to run twice. And if you’re source-generating based on analyzed C# files, it works just as you’d expect - the only source files available in the source generator as part of each assembly build are the source files from that assembly.
However, if you are trying to source generate based on some *.[AnalyzerName].additionalfile, then regardless of where you put it, that additional file is available in both packages and both source generators. You can even see it in Visual Studio and the csproj’s. That seems to happen regardless of the .additionalfile’s actual location in the folder structure. So, naturally, if you add a different additionalfile to each of the two packages, both packages would have both additional files, and the only way to tell them apart would be if you dropped some identifier in the .additionafile so you could tell which ones were from this actual project, and which ones were not. I find this very strange. Hence the question.
What that documentation is not clear on is that it (if you use the .additionalfile extension) actually adds the additional file to every single package/asmdef in your project. There might be certain types of additional files where this might be a desirable behavior - like if the file is actually a code-style settings file or something like that. But in my case, since I take what’s in the additional file and use it to generate/add some code to the assembly (so it can be used in there), this isn’t a desirable behavior at all. If there are two assembly definitions that use the analyzer, the code will be added twice. The Unity Netcode solution to this seems to be to have the user write some code that actually “binds” the additional code at runtime, but I haven’t tried that out, and I’m not really interested in that approach, since I use reflection to locate the generated code at runtime.
I’ve been unable to resolve this, and since I’m unwilling (as a library author) to mandate the csc.rsp approach, I’ve moved over to an editor extension that generates the code instead. I’m not aware of any other non-hacky solution, but I’d certainly still be interested in suggestions!
Well, I mean the source-generator-named additional files are never per assembly, they are always per source generator.
If I have to guess, when Unity moves over to use .csproj files, it will naturally change. Until then, inside Unity the standard is the csc.rsp route.
BTW, you can generate csc.rsp files yourself or you can establish naming convention, like additionally to contain the name of the source generator you can demand the additional files’ name contain the assembly name too and just filter for the current domain inside your source generator.
Yep - I agree that the nature of additional files will most likely change when moving to csproj, which is part of the reason I posted in this subforum. I’m not really sure how the legacy .additionalfile’s will behave in that version. My impression for why they’re this way currently is just because they’re essentially added as global compiler flags, but I’m not clear on the compiler pattern in the .csproj world.
My original question was whether it was possible to scope the additional files for the containing asmdef, since that’s the way I’d personally expect it to work, and that’s how csproj’s work. That’s also how asmdef/.cs files work, of course. No one seems to have been able to answer that question, so I’m guessing not.
I did give some thoughts on auto-generating csc.rsp or filtering out the extraneous additional files in previous posts. For me, since I already need an editor plugin for some editor UI (to edit this additional file), the in-editor generation seems like a good path forwards, that’s less likely to break in the future than any of the other workarounds.
I don’t recognize these as “workarounds” since there is no original “work” supposed to work at the first place. Many loves comparing Unity to plain .Net but that comparison is flawed for many reasons. Whatever Unity supports is the canon here.
I don’t like in-editor code generation, because it has a severe negative effect on iteration time. You edit your whatever data, you save, it processes, then code gets generated and you then need to import said changed code, so it gets compiled and imported. It’s a slow process. Source Generator is faster and more importantly it doesn’t hold up the editor. It’s worth to develop a working pipeline there. But obviously, that’s maybe just me, you certainly do whatever you feel best for your own project.
IDK, if you don’t expect any more answers to your original question, off-topic conversation about a very important aspect of your problem is just slightly off-topic and IMHO it’s worth to explore.
Over time I somewhat specialized in code generation, I became the go-to guy on my projects I was contributing to with code generation problems or for solutions. If you take a look at my GitHub (link in my signature), I explored both types of code generations, and I have a hybrid package we played around with (where after an asset edit session we run the AssetPostProcessor and generate an additional file so the source generator can generate code). It’s more promising. And avoid nasty, unnecessary progress bars and hold-ups. Again, the most problematic part generating source code in the editor is that it’s needs to be imported, so the editor will fell into another compile routine. Meanwhile source generation is more seamless, since it’s invoked during compilation.
In the hybrid approach you describe here - are you saying that would avoid a domain reload? I totally agree that the domain reloads are disruptive, and I’d certainly like to avoid them as much as possible. I was under the impression that changing the additional file (a json file for me) would result in a domain reload regardless when using the UI. The extra code visibility you get from the in-editor codegen is also nice, and I do show the generated code in the UI, so it’d be a little disappointing losing that, too.
Is there a way to check what assembly the .additionalfile belongs to in the source generator? Maybe through the use of some utility in UnityEditor.dll that your source generator can reference?