Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Cannot load generator dependencies when packed as nuget package #166

Closed
cezarypiatek opened this issue Sep 22, 2019 · 7 comments
Closed
Labels
Milestone

Comments

@cezarypiatek
Copy link

cezarypiatek commented Sep 22, 2019

Hi,
I create a generator using CodeGeneration.Roslyn. Everything works when I'm using it in the same solution where the generator is defined. However, when I pack everything into a NuGet package and I try to use it in another project it fails. I'm getting the following exceptions:

1>------ Rebuild All started: Project: ConsoleApp4, Configuration: Debug Any CPU ------
1>Exception in file processing: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Nie można odnaleźć określonego pliku.
1>File name: 'Microsoft.CodeAnalysis.Workspaces, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
1>   at OnBuildGenerator.OnBuildMappingGenerator.GenerateRichAsync(TransformationContext context, IProgress`1 progress, CancellationToken cancellationToken)
1>   at CodeGeneration.Roslyn.Engine.DocumentTransform.TransformAsync(CSharpCompilation compilation, SyntaxTree inputDocument, String projectDirectory, Func`2 assemblyLoader, IProgress`1 progress)
1>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.Generate(IProgress`1 progress, CancellationToken cancellationToken)
1>
1>
1>AggregateException: One or more errors occurred. (Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Nie można odnaleźć określonego pliku.)
1>System.AggregateException: One or more errors occurred. (Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Nie można odnaleźć określonego pliku.) ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Nie można odnaleźć określonego pliku.
1>   at OnBuildGenerator.OnBuildMappingGenerator.GenerateRichAsync(TransformationContext context, IProgress`1 progress, CancellationToken cancellationToken)
1>   at CodeGeneration.Roslyn.Engine.DocumentTransform.TransformAsync(CSharpCompilation compilation, SyntaxTree inputDocument, String projectDirectory, Func`2 assemblyLoader, IProgress`1 progress)
1>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.Generate(IProgress`1 progress, CancellationToken cancellationToken)
1>   --- End of inner exception stack trace ---
1>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.Generate(IProgress`1 progress, CancellationToken cancellationToken)
1>   at CodeGeneration.Roslyn.Generate.Program.Main(String[] args)
1>---> (Inner Exception #0) System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Nie można odnaleźć określonego pliku.
1>File name: 'Microsoft.CodeAnalysis.Workspaces, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
1>   at OnBuildGenerator.OnBuildMappingGenerator.GenerateRichAsync(TransformationContext context, IProgress`1 progress, CancellationToken cancellationToken)
1>   at CodeGeneration.Roslyn.Engine.DocumentTransform.TransformAsync(CSharpCompilation compilation, SyntaxTree inputDocument, String projectDirectory, Func`2 assemblyLoader, IProgress`1 progress)
1>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.Generate(IProgress`1 progress, CancellationToken cancellationToken)
1>

I verified and the Microsoft.CodeAnalysis.Workspaces assembly with the expected version is present in the tools folder inside the nuget package. It looks like a problem with loading generator dependencies.

Update: My project is available here https://github.com/cezarypiatek/MappingGenerator/tree/feature/on_build_code_generation

The generator is defined in OnBuildGenerator project. Nuget should be generated automatically on build.

@amis92
Copy link
Collaborator

amis92 commented Sep 23, 2019

Indeed, loading dependencies is currently an issue. See #114 for tracking an attempt at fixing.

@amis92 amis92 added the bug label Sep 23, 2019
@cezarypiatek
Copy link
Author

I've discovered that my generator assembly is loaded from the lib directory instead of the tools. I have defined GeneratorAssemblySearchPaths` in the following way:

<GeneratorAssemblySearchPaths Include="$(MSBuildThisFileDirectory)..\tools" />

How to force CodeGeneration.Roslyn to load generators from the tools directory?

@cezarypiatek
Copy link
Author

cezarypiatek commented Sep 23, 2019

Next observation: I think the problem is caused by this method

private Assembly LoadAssembly(AssemblyName assemblyName)

Because the generator assembly is searched in the first place in ReferencePath instead of GeneratorAssemblySearchPaths.

The problem occurs when the Attribute and Generator are in the same assembly.

@amis92
Copy link
Collaborator

amis92 commented Sep 23, 2019

Yes, so that'll be why for p2p (same solution) this works - that's because then you're referencing this project (and in turn assembly), so it becomes included in ReferencePath item.

I don't think there's an easy solution here, aside from separating attribute into another assembly.

PS Good investigation BTW.

@cezarypiatek
Copy link
Author

For p2p approach, the GeneratorAssemblySearchPaths variable is not set so changing the order of searching: first GeneratorAssemblySearchPaths then ReferencePath should fix the issue without any negative side effects.

@chinwobble
Copy link

I'm running into similar issues.
I'm writing a generator that depends on Microsoft.Data.SqlClient.
I've combined the attributes and generators into the same assembly for simplicity.
If my lib is written against .net standard then I get these load exceptions.

Also for some reason the generator able to connect to local databases.

@amis92
Copy link
Collaborator

amis92 commented Mar 25, 2020

This will be resolved via Plugin.Sdk. Tracked in #113

@amis92 amis92 closed this as completed Mar 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants