Build/Test Issues

Build fails if SourceGenerator project has a package reference.

Started by yusuf on 4,779 views

I have created a git repository to reproduce the issue. It uses the official way of adding dependencies to a source generator project. Visual Studio and dotnet CLI compile this solution successfuly. NCrunch gives the following error:


Program.cs (7, 4): The type or namespace name 'Test' could not be found (are you missing a using directive or an assembly reference?)
Program.cs (7, 20): The type or namespace name 'Test' could not be found (are you missing a using directive or an assembly reference?)


WARNING - CSC (0, 0): CS8785: Generator 'Generator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.'
Thanks for sharing this. When I attempt to build the sample under VS2019, I get the same failures as NCrunch is reporting. Can you confirm which version of VS you're building this with? When we added the support for source generators, we couldn't find a way make the source generator projects build with additional dependencies added, so this wasn't in scope for NCrunch at the time. I'm wondering if this is a new addition to the toolset.
I use Visual Studio 2019 16.11.0. I have tried again with a fresh clone of the repository and Visual Studio built it on first try without any errors.
I know the dependencies of source generators (especially the transient ones) are a pain in the neck at the moment, but it would be great if you can solve this. JSON parsing has a lot of use cases in source generators and it is impossible to do it without a dependency right now.
yusuf wrote:I use Visual Studio 2019 16.11.0. I have tried again with a fresh clone of the repository and Visual Studio built it on first try without any errors.


Thanks for this info. I can confirm that this is something new they pushed out with 16.11. After updating the IDE I can now build the sample project, which hopefully means we can find a way to get NCrunch to handle it. I'll let you know when a fix is available.

yusuf wrote:
I know the dependencies of source generators (especially the transient ones) are a pain in the neck at the moment, but it would be great if you can solve this. JSON parsing has a lot of use cases in source generators and it is impossible to do it without a dependency right now.


I hope they improve this setup. Right now it feels like a gruesome hack. I agree there isn't much you can write in these generators without being able to specify dependencies. The toolset in this area seems very immature, I expect further problems like this in the near future.
Could you try the build below and let me know if this solves the problem for you?

NCrunch_Console_4.10.0.2.msi
NCrunch_Console_4.10.0.2.zip
NCrunch_GridNodeServer_4.10.0.2.msi
NCrunch_GridNodeServer_4.10.0.2.zip
NCrunch_LicenseServer_4.10.0.2.zip
NCrunch_VS2010_4.10.0.2.msi
NCrunch_VS2010_4.10.0.2.zip
NCrunch_VS2012_4.10.0.2.msi
NCrunch_VS2012_4.10.0.2.zip
NCrunch_VS2013_4.10.0.2.msi
NCrunch_VS2013_4.10.0.2.zip
NCrunch_VS2015_4.10.0.2.msi
NCrunch_VS2015_4.10.0.2.msi.7z
NCrunch_VS2015_4.10.0.2.zip
NCrunch_VS2017_4.10.0.2.msi
NCrunch_VS2017_4.10.0.2.msi.7z
NCrunch_VS2017_4.10.0.2.zip
NCrunch_VS2019_4.10.0.2.msi
NCrunch_VS2019_4.10.0.2.msi.7z
NCrunch_VS2019_4.10.0.2.zip
NCrunch_VS2022_4.10.0.2.msi
NCrunch_VS2022_4.10.0.2.msi.7z
NCrunch_VS2022_4.10.0.2.zip
I have tried and it built the project successfully after disabling the instrumentation for the code generation project. Thanks for the quick solution.
Can confirm, this does make NCrunch happy in our source generator project.

External image

And we correctly get the warning about this edge case:

This project contains source generation code that makes use of MS code analyzers. Source generation executes code inside the build system without consideration for additional reference assemblies. Because NCrunch cannot control references inside the source generation environment, instrumented code will fail to execute when run in a generation context. If you experience problems with this source generator, please turn off the 'Instrument output assembly' setting for the project containing the generation code.


Thanks for fixing that!

Post a reply

Log in to reply.