hadashiA / VContainer

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.
https://vcontainer.hadashikick.jp
MIT License
1.79k stars 157 forks source link

SourceGenerator failed to generate source #536

Open Bearded-Ranga opened 11 months ago

Bearded-Ranga commented 11 months ago

warning CS8785: Generator 'VContainerSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName of the added source file must be unique within a generator. (Parameter 'hintName')'

I get this warning when I attempt to build with source generator enabled (using the setup guide). The build is successful but I am unsure if source generator has only partially or completely failed. How do I fix this warning and get source generator working?

Target platform: Android Unity 2021.3.13f1

balukin commented 4 months ago

I was getting the same error

warning CS8785: Generator 'VContainerIncrementalSourceGenerator' failed to generate source. 
It will not contribute to the output and compilation errors may occur as a result. 
Exception was of type 'ArgumentException' with message 'The hintName 'MyNamespace.MyClassNameGeneratedInjector.g.cs' of the added source file must be unique within a generator. (Parameter 'hintName')'

After some testing, it seems that having a partial class with files named MyClassName.cs and MyClassName.Anything.cs will break the source generation. In my case, I had a MyClassName.cs and MyClassName.Editor.cs (MonoBehaviours) for some debug code I used temporarily in the editor.

I don't know much about source generators but looking at the code it seems to be fixable here where probably a file name should be used instead of a type name which results in duplicates with partial classes. Fortunately for me the partial file was only a temp debug thing so I could delete it easily but you may try your luck there if you rely on partials.

EDIT: Relying on file names may not work with types that have no source data available (e.g. are a part of pre-compiled assembly). Based on this post (chapter: To collect or not to collect) it seems that the solution may require some additional changes in source generation process.