How to publish this C# Source Generator with dependencies on Nuget?
Asked Answered
M

0

7

I've created a C# source generator, and would like to publish it to Nuget.

But I run into warnings/errors such as:

  • Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below: - Add lib or ref assemblies for the netstandard2.0 target framework
  • An instance of analyzer Cosmogenesis.Generator.CosmosGenerator cannot be created from [..]\Cosmogenesis.Generator.dll : Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.. (or Version=2.0.0.0 depending on what I'm trying)

There are a few resources explaining how, but none seem to work in my exact scenario, which is:

  • Cosmogenesis.Core
    • .NET Standard 2.1
    • Not depended on by the generator, but the generated source code depends on it
  • Cosmogenesis.Generator (published on Nuget as just Cosmogenesis)
    • .NET Standard 2.0
    • This is the source generator

Ideally, I'd like to be able to just install Cosmogenesis from Nuget and it would automatically bring in Cosmogenesis.Core along with the generator.

Use functionality from NuGet packages <-- explains my scenario (except for differing .net standard requirements). ....And as a side question: Why does it have code to check if an assembly is referenced if it's explaining how to make it be automatically referenced?....

Anyway...

Here is Cosmogenesis on Github

Cosmogenesis.Core is published without any trouble.

Cosmogenesis.Generator is published using this .csproj as well

It works. However, the consumer needs to reference BOTH packages. I'd like them to only need to reference one package.

When I add <IncludeBuildOutput>false</IncludeBuildOutput> (as per tutorial above), on build I get this warning: Some target frameworks declared... [see above]. If I ignore it and publish it anyway, when I consume the package in a project and build it, the generator does not run and I get this warning: An instance of analyzer... [see above].

When I add <PackageReference Include="Cosmogenesis.Core" Version="0.0.1" /> (as per tutorial above), I get an error. The generator is .NET Standard 2.0 while the .Core package is 2.1. The error is: error NU1202: Package Cosmogenesis.Core 0.0.1 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package Cosmogenesis.Core 0.0.1 supports: netstandard2.1 (.NETStandard,Version=v2.1). Understandable.

When I set the generator project to 2.1 to fix the mismatch, no warnings appear while building, however the consumer again gets the An instance of analyzer... error. C# Source Generators - Write code that writes code - David Wengier says they have to be .net standard 2.0.

This is my first nuget package. And my first source generator. Can anyone advise?

EDIT: I'm 75% sure a solution to this roslyn github issue would solve my problem here.

Molehill answered 20/3, 2021 at 23:49 Comment(2)
Did you ever find a solution to this?Schoolmistress
There are lots of open issues on this ATM and no solution in sight. See github.com/dotnet/sdk/issues/17775Natty

© 2022 - 2024 — McMap. All rights reserved.