How to fix C# Source Generators Issue of not found references
Asked Answered
E

3

8

I have this project using C# Source Generators.

https://github.com/efonsecab/PTIMicroservicesGenerators

The issue I'm having is that I get this issue when compiling the Console App

CSC : warning CS8785: Generator 'OpenApiClientServicesGenerator' 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.'

Does anybody know which is the correct way to fix this issue when using C# Source Generators? Thanks for the help.

Estray answered 4/4, 2021 at 18:29 Comment(0)
C
2

The problem is that you are not deploying all of your dependencies.

Add the following to your PTI.Microservices.Generators.csproj project file

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

To tell msbuild to copy all dependencies to the output directory.

Castillo answered 4/4, 2021 at 19:31 Comment(2)
I did exactly this, but the error (CSC : warning CS8785) still remain, the generator fails because it does not find the assembly what is reference by the source generator assembly. Both assemblies target netstandard2.0Janellajanelle
@Janellajanelle same problem hereForster
C
7

As of May 2022 I believe you need to do more than just add the package reference:

In the generators project file:

  1. Package refernce must have GeneratePathProperty="true" PrivateAssets="all"
  2. GetTargetPathDependsOn needs to be specified and configured

This piece of knowledge is based on looking at https://github.com/dotnet/roslyn-sdk/blob/main/samples/CSharp/SourceGenerators/SourceGeneratorSamples/CSharpSourceGeneratorSamples.csproj

  <ItemGroup>
    <!-- Generator dependencies -->
    <PackageReference Include="CsvTextFieldParser" Version="1.2.2-preview" GeneratePathProperty="true" PrivateAssets="all" />
  </ItemGroup>

  <PropertyGroup>
    <GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
  </PropertyGroup>

  <Target Name="GetDependencyTargetPaths">
    <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="$(PKGCsvTextFieldParser)\lib\netstandard2.0\CsvTextFieldParser.dll" IncludeRuntimeDependency="false" />
    </ItemGroup>
  </Target>
</Project>
Cassock answered 11/5, 2022 at 12:6 Comment(2)
Thank you! Been trying to figure this out for hours...Beecher
This has to be some kind of VS2k22 bug because I have two projects that use my generator, almost identical with trivial differences, and one builds and one does not.Hansel
C
2

The problem is that you are not deploying all of your dependencies.

Add the following to your PTI.Microservices.Generators.csproj project file

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

To tell msbuild to copy all dependencies to the output directory.

Castillo answered 4/4, 2021 at 19:31 Comment(2)
I did exactly this, but the error (CSC : warning CS8785) still remain, the generator fails because it does not find the assembly what is reference by the source generator assembly. Both assemblies target netstandard2.0Janellajanelle
@Janellajanelle same problem hereForster
F
0

I had to change the import directory to mirror the cache directory:

<None Include="$(PkgNewtonsoft_Json)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="/lib/netstandard2.0/" Visible="false" />
Frae answered 12/4, 2022 at 3:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.