Closed devsko closed 4 years ago
The most likely cause of this error is an incorrectly formed <ProjectReference>
element. A reference to a source generator in the same solution needs to have this form:
<ProjectReference
Include="[relative path]"
SetTargetFramework="TargetFramework=netstandard2.0"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
The SetTargetFramework
attribute can be omitted if the project using the source generator is netstandard2.0 or newer, but it's often easiest to just include it.
Thanks for looking at this.
The kind of reference you mentioned works fine for actually generating code, but I'm trying to reference the project (not just the source generator) from a unit test. I thought a "normal" ProjectReerence
should work.
(Absolutely not blocking for me)
Is the problem fixed if you add the attribute PrivateAssets="all"
to the <ProjectReference>
form I showed above?
The build succeeds but the assembly is not referenced thus I cannot test the classes in the generator project.
PrivateAssests="all"
has no effect on this behavior.
The build succeeds but the assembly is not referenced thus I cannot test the classes in the generator project.
Ah, we don't currently have a mechanism for unit testing the source generator code itself. I filed https://github.com/dotnet/roslyn-sdk/issues/602 for this.
I understand that referencing a project with source generator in any other way than just for generating code is currently not possible - at least when it self references other packages. Right? I can live with that but think it should be mentioned somewhere ;)
@sharwell
Adding IncludeRuntimeDependency="false"
fixes the issue. The GetDependencyTargetPaths
target in SourceGeneratorSamples.csproj
should look like this. Everything works as expected and the generator project can be referenced by other projects like any other project.
<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>
Version Used: 5.0.100-preview.8.20417.9
Steps to Reproduce:
<ProjectReference Include="..\SourceGeneratorSamples\SourceGeneratorSamples.csproj" />
Expected Behavior: Build of the added project succeeds and the source generator is available for testing.
Actual Behavior:
After removing the part below from
SourceGeneratorSamples.csproj
building the new project succeeds but of course now buildingGeneratedDemo
fails because the referenced assemblies are not found by the generator./cc: @sharwell