Search code examples
roslyn

Roslyn FileNotFoundException when referencing a nuget package


I have a Roslyn code-gen project with a class that implements IIncrementalGenerator.

I have added the following package reference to the generator project

<PackageReference Include="Mono.TextTemplating" Version="2.2.1" />

But when I build the app I get the following error

System.IO.FileNotFoundException: 'Could not load file or assembly 'Mono.TextTemplating, Version=2.2.0.0, Culture=neutral, PublicKeyToken=4fa72d50da25cb30' or one of its dependencies. The system cannot find the file specified.'

I have also tried

<PackageReference Include="Mono.TextTemplating.Roslyn" Version="2.2.1" />

But get the same problem.

How do I consume this package from my code-gen project?


Solution

  • There is an example of how to include a compile-time dependency without pushing it onto the consuming app.

    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" />
        <PackageReference Include="Handlebars.Net" Version="1.10.1" GeneratePathProperty="true" PrivateAssets="all" />
        <PackageReference Include="Newtonsoft.Json" Version="12.0.1" 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" />
          <TargetPathWithTargetPlatformMoniker Include="$(PKGHandlebars_Net)\lib\netstandard2.0\Handlebars.dll" IncludeRuntimeDependency="false" />
          <TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" />
        </ItemGroup>
      </Target>