简体   繁体   中英

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

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.

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.

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>

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM