question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Build failure when referencing a source generator project

See original GitHub issue

Version Used: 5.0.100-preview.8.20417.9

Steps to Reproduce:

  1. Add a new project to the source generator sample
  2. 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:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
devskocommented, Sep 2, 2020

@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>
0reactions
devskocommented, Aug 31, 2020

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 😉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found