AArnott / CodeGeneration.Roslyn

Assists in performing Roslyn-based code generation during a build.
Microsoft Public License
408 stars 59 forks source link

How to use additional dependencies #106

Closed frblondin closed 5 years ago

frblondin commented 5 years ago

In order to create some syntax nodes I'm trying to add more NuGet dependencies to my custom generator project. More specifically I want to use some methods provided by CSharpSyntaxGenerator.cs from the Microsoft.CodeAnalysis.CSharp.Workspaces NuGet package - such as TypedConstantExpression(TypedConstant value).

When I test the code generator from unit tests it works perfectly well. When I'm running the build on a project which uses the generator it fails with the following error:

2>Exception in file processing: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=2.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
2>File name: 'Microsoft.CodeAnalysis.Workspaces, Version=2.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

My understanding is that dotnet-codegen will only load the assemblies using this resolver. I don't know how to make sure that the additional dependencies I'm adding to my generator will work when executed by <DotNetCliToolReference Include="dotnet-codegen" Version="0.4.88" />.

My questions:

  1. When I'm using my custom generator as a .csproj project (ie. not as a NuGet dependency), how do I make sure that the additional NuGet references added to this project will be resolved by <DotNetCliToolReference Include="dotnet-codegen" Version="0.4.88" />?
  2. Will this problem be resolved anyways if I use my generator as a NuGet package instead? (as the additional references would then be managed properly)
  3. All I need is using Microsoft.CodeAnalysis.CSharp.Workspaces NuGet package. Would it make sense to add this dependency in CodeGeneration.Roslyn as there are already many dependencies with Roslyn? I can create a PR.
amis92 commented 5 years ago
  1. It shouldn't require further action on your side.
  2. It probably won't but you can try.
  3. No, on the contrary, the CodeGeneration.Roslyn should have as few dependencies as possible.

Overall it seems there's a problem in the dependency loading, although I thought it was already resolved.

I've created a minimal repro solution for reference and easier investigation: https://github.com/amis92/CodeGeneration.Roslyn-dependency-problem-repro

frblondin commented 5 years ago

Thanks for your help @amis92 .

While debugging CodeGeneration.Roslyn.Tool I think I've a better understanding of the cause of the problem. I set a breakpoint here and while inspecting I could see that the Microsoft.CodeAnalysis.Workspaces name does not exactly exists as a NuGet dependency. The NuGet package name is Microsoft.CodeAnalysis.Workspaces.Common. See this picture: image

The only way is apparently to check the RuntimeAssemblyGroups.AssetPaths file names. I tested it locally and it seems to work fine.

I will submit a PR.