Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Build fails if SourceGenerator project has a package reference.
yusuf
#1 Posted : Thursday, August 19, 2021 1:26:37 PM(UTC)
Rank: Member

Groups: Registered
Joined: 11/7/2019(UTC)
Posts: 12

Was thanked: 4 time(s) in 3 post(s)
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:

Code:

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.'
Remco
#2 Posted : Friday, August 20, 2021 5:54:05 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,510

Thanks: 852 times
Was thanked: 1155 time(s) in 1079 post(s)
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.
yusuf
#3 Posted : Friday, August 20, 2021 6:33:10 AM(UTC)
Rank: Member

Groups: Registered
Joined: 11/7/2019(UTC)
Posts: 12

Was thanked: 4 time(s) in 3 post(s)
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.
Remco
#4 Posted : Friday, August 20, 2021 1:42:40 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,510

Thanks: 852 times
Was thanked: 1155 time(s) in 1079 post(s)
yusuf;15614 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;15614 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.
Remco
#5 Posted : Saturday, August 21, 2021 1:23:21 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,510

Thanks: 852 times
Was thanked: 1155 time(s) in 1079 post(s)
1 user thanked Remco for this useful post.
UppSol on 8/22/2021(UTC)
yusuf
#6 Posted : Sunday, August 22, 2021 8:50:32 AM(UTC)
Rank: Member

Groups: Registered
Joined: 11/7/2019(UTC)
Posts: 12

Was thanked: 4 time(s) in 3 post(s)
I have tried and it built the project successfully after disabling the instrumentation for the code generation project. Thanks for the quick solution.
1 user thanked yusuf for this useful post.
Remco on 8/24/2021(UTC)
Silvenga
#7 Posted : Monday, August 23, 2021 3:09:41 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 1/24/2016(UTC)
Posts: 40
Location: United States of America

Was thanked: 3 time(s) in 3 post(s)
Can confirm, this does make NCrunch happy in our source generator project.



And we correctly get the warning about this edge case:

Quote:
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!
1 user thanked Silvenga for this useful post.
Remco on 8/24/2021(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.053 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download