Proper way to configure Incremental Source Generators?

Hey everyone. With all the regressions to ECS generics, I’m hoping to turn to source generators to fix my problems and stop running into broken edge cases. Unfortunately, Unity makes me build source generators separately, and I have been having the roughest time just getting them to work at all (not even in Unity, though I’m trying to set them up in a way that would be compatible in Unity).

Some background, I’m using VS Studio 19 and Windows 10 as I’ve been procrastinating updates. I hope these aren’t the root of my issues.

I create a new class library project targeting .NET Standard 2.0. I then install Microsoft.Codeanalaysis.CSharp version 4.0.1 from Nuget (I’ve also tried 4.2.0 based on a rumor this was supported better, but same issue). I wrote an IIncrementalGenerator and it compiles fine. I then created a second project in the solution to test. However, when compiling the second project in the solution, I get the following error:

If I try to copy the DLL to a Unity project, I get a similar error about not being able to load the plugin due to missing dependencies. When using version 4.2.0, the version in the error message changes, but otherwise its the same issue.

I’m wondering if anyone can provide any insight or provide the steps they use for building and testing source generators?

2 Likes

I haven’t used vs in a while but I was under assumption incremental source generators require .net 6 which required vs22 to have proper support.

I could very much be wrong though as apart from loading unitys to play around with I have not looked into this myself

To add to that, as far as I know Unity shipped code analysis dll doesn’t support incremental source generator yet.

Edit: might be wrong, see my reply below

@DreamingImLatios You need to use Codeanalysis version 3.8:

UPDATE: looks like this is completely wrong! sorry, the docs are not correct here

Starting from 2021.3, you can use 3.9.0, not sure if it got updated again in recent Unity versions.

Edit: seems like in 2022.2.5 it is using 4.1.0, but I haven’t tested it neither all the new features it has

1 Like

Unity uses 4.0 and incremental source generators for IJobEntity in their latest releases. I checked that before going down the route of incremental source generators. I did find this link, which does explain the issue I’m having. Versions of source generator dependencies | Damir's Corner Although I don’t get why the source generator were able to compile if I don’t have working libraries on my system. And it also doesn’t really explain why the error also occurred in Unity, but I’m hoping a newer Visual Studio will solve the problem. That’s what I am doing right now. I’ll post again if I either run into more issues or get it working.

2 Likes

SUCCESS!

8 Likes

Would you please tell me more about how you made this to work?
What version of Roslyn / Visual Studio / Unity you used, so you could use Incremental Generator successfully?
I want to use Roslyn generators in my Unity project, but I want to use incremental generator instead of that old classic one. Any help would be greatly appreciated :slight_smile:

Sometimes I write blog-style articles on tech I am still learning, and post it on GitHub. This is one of those instances: https://github.com/Dreaming381/Latios-Framework-Documentation/tree/main/Tech%20Adventures

1 Like

wow nice, thanks soo much for this help, I will look at it and I will try to make it work in my project :slight_smile:

I went through your post quickly and will read it carefully in the evening.
But can you please tell me one crucial thing? You used Unity version 2022.2.11 and it worked there?
And you finally used Visual Studio 2022, not the Visual Studio 2019, is that right?
Did you try to run the very latest stable build of VS 2022?

PS: From you documentation about generators, it doesn’t look simple at all, to make it work :smile:

Since 2022.3 has been out for a while, you should use it. It lets you access Microsoft.CodeAnalysis.CSharp 4.3.1. I’ve been using VS 2022 to build source generators for Unity for a while, no problem at all.

Source generator is not a simple topic. But would you mind elaborate on this?

It is still working in 2022.3 LTS.

Yeah. I had to switch to 2022 to make it work.

No. I don’t update VS all that frequently.

Rarely is something “simple” when you are new to the tech. But once you have something working the first time, it gets a lot easier.