Build failure when referencing a source generator project
See original GitHub issueVersion Used: 5.0.100-preview.8.20417.9
Steps to Reproduce:
- Add a new project to the source generator sample
- Add
<ProjectReference Include="..\SourceGeneratorSamples\SourceGeneratorSamples.csproj" />
Expected Behavior: Build of the added project succeeds and the source generator is available for testing.
Actual Behavior:
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: Unerwarteter Fehler bei der GenerateDepsFile-Aufgabe. [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: System.ArgumentException: An item with the same key has already been added. Key: C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\SourceGeneratorSamples\SourceGeneratorSamples.csproj [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: at Microsoft.NET.Build.Tasks.SingleProjectInfo.CreateProjectReferenceInfos(IEnumerable`1 referencePaths, IEnumerable`1 referenceSatellitePaths, Func`2 isRuntimeAssembly) [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateDepsFile.WriteDepsFile(String depsFilePath) [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateDepsFile.ExecuteCore() [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute() [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
..\Microsoft.NET.Sdk.targets(195,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [C:\Users\stefa\source\repos\roslyn-sdk\samples\CSharp\SourceGenerators\TestProject1\TestProject1.csproj]
After removing the part below from SourceGeneratorSamples.csproj
building the new project succeeds but of course now building GeneratedDemo
fails because the referenced assemblies are not found by the generator.
<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>
<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PKGCsvTextFieldParser)\lib\netstandard2.0\CsvTextFieldParser.dll" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGHandlebars_Net)\lib\netstandard2.0\Handlebars.dll" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" />
</ItemGroup>
</Target>
/cc: @sharwell
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Deploying a C# source generator project that includes ...
Generator BenchmarkSourceGenerator failed to generate source. It will not contribute to the output and compilation errors may occur as a result.
Read more >Build fails if SourceGenerator project has a package ...
I have created a git repository to reproduce the issue. It uses the official way of adding dependencies to a source generator project....
Read more >Solving the source generator 'marker attribute' problem
I described 4 main approaches: Directly referencing the source generator dll in the consuming project; creating two independent NuGet packages; ...
Read more >Bug: Solution with source generators does not compile ...
Actual Result. The build fails because none of the generated code consumed by the integration test project can be found: C:\source\repos\github ...
Read more >Let's Build an Incremental Source Generator With Roslyn, by ...
This talk was part of the JetBrains .NET Days Online 2022 conference. Details: https://pages.jetbrains.com/dotnet-days-2022 Description: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sharwell Adding
IncludeRuntimeDependency="false"
fixes the issue. TheGetDependencyTargetPaths
target inSourceGeneratorSamples.csproj
should look like this. Everything works as expected and the generator project can be referenced by other projects like any other project.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 😉