Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NuGets referenced from analyzer/source-generator should automatically include DLL's #7640

Open
Eli-Black-Work opened this issue May 20, 2022 · 3 comments
Labels

Comments

@Eli-Black-Work
Copy link

Eli-Black-Work commented May 20, 2022

Let's say that my analyzer or source generator depends on the GraphQL NuGet:

<ItemGroup>
    <PackageReference Include="GraphQL" Version="4.7.1" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>

This will fail, because MSBuild won't automatically include GraphQL's DLL's, so I need to do this:

<!-- See https://github.com/dotnet/roslyn-sdk/blob/0313c80ed950ac4f4eef11bb2e1c6d1009b328c4/samples/CSharp/SourceGenerators/SourceGeneratorSamples/SourceGeneratorSamples.csproj#L13-L30
and https://github.com/dotnet/roslyn/discussions/47517#discussioncomment-64145 -->
<PropertyGroup>
    <GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>

<Target Name="GetDependencyTargetPaths">
    <!-- Manually include the DLL of each NuGet package that this analyzer uses. -->
    <ItemGroup>
        <TargetPathWithTargetPlatformMoniker Include="$(PKGGraphQL)\lib\netstandard2.0\GraphQL.dll" IncludeRuntimeDependency="false" />
    </ItemGroup>
</Target>

I'd like to request that when an analyzer/source-generator references a NuGet, MSBuild automatically hooks up the NuGet's DLL's 🙂

This seems to have been a source of confusion for many! See all of the comments at dotnet/roslyn#47517 🙂

P.S.
Not sure if MSBuild is the correct place to post this?

@Eli-Black-Work Eli-Black-Work added the needs-triage Have yet to determine what bucket this goes in. label May 20, 2022
@Eli-Black-Work Eli-Black-Work changed the title Improve NuGet package referencing from source generators Improve NuGet package referencing from analyzers/source-generators May 25, 2022
@Eli-Black-Work Eli-Black-Work changed the title Improve NuGet package referencing from analyzers/source-generators NuGets referenced from analyzer/source-generator should automatically include DLL's May 25, 2022
@benvillalobos
Copy link
Member

MSBuild Team Triage: It looks like there's a built in mechanism to do this. If you set property CopyLocalLockFileAssemblies to true in your analyzer project, it will copy over the graphql dependencies you need to its bin/ folder.

@benvillalobos benvillalobos removed the needs-triage Have yet to determine what bucket this goes in. label May 26, 2022
@Eli-Black-Work
Copy link
Author

@benvillalobos Thanks for the reply! 🙂

I just gave CopyLocalLockFileAssemblies a shot. When building from the command-line, everything works fine, but when I open my project in Visual Studio, the generator crashes with this error:

Generator 'ControllerGenerator' 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 'Microsoft.OpenApi.Readers, Version=1.2.3.0, Culture=neutral, PublicKeyToken=3f5743946376f042' or one of its dependencies. The system cannot find the file specified.'

Here's an example project that reproduces the issue: https://github.com/Bosch-Eli-Black/source-generator-example

I'm using VS 17.3.0 Preview 1.1

Is this something for the MSBuild team to look into, or should I file it somewhere else?

@Eli-Black-Work
Copy link
Author

Opened a Roslyn issue for supporting CopyLocalLockFileAssemblies in VS: dotnet/roslyn#61940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants