Skip to content

Source generator dependency unable to be resolved #52017

Closed
@Turnerj

Description

@Turnerj

Version Used:
MSBuild: 16.9.0.11203
.NET: 5.0.201
CSC: 3.900.21.12328 (comes with .NET 5.0.201 and used by dotnet build), 3.900.21.16010 (comes with Visual Studio and used at least by msbuild but probably VS too) - All the commits between these two versions
Visual Studio: 16.9.2

Steps to Reproduce / Actual Behaviour:
Sample project to recreate issue: https://github.com/Turnerj/SourceGeneratorDependencyTest

Using .NET CLI (like we use on a CI)

  1. Clean solution
  2. Build solution with command dotnet build
  3. Solution builds 🎉

Using MSBuild

  1. Clean solution
  2. Build solution with command msbuild
  3. Solution fails (see generator warning) 😕

Using Visual Studio

  1. Clean solution
  2. Build solution via Visual Studio
  3. Solution fails (see generator warning) 😞

Generator Warning

CSC : warning CS8784: Generator 'CustomSourceGenerator' failed to initialize. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly
'System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.'

Expected Behavior:

Each method builds the project.

Notes:

The sample project is a minimum example of what we use in a full project: https://github.com/RehanSaeed/Schema.NET/
I'm following the guidance listed in this discussion about how to mark the package reference: #47517 (comment)
This is also seen on the Roslyn SDK C# samples: https://github.com/dotnet/roslyn-sdk/blob/0313c80ed950ac4f4eef11bb2e1c6d1009b328c4/samples/CSharp/SourceGenerators/SourceGeneratorSamples/SourceGeneratorSamples.csproj#L13-L30
I've added the transient dependency (in my case System.Text.Encodings.Web) as per this comment in the same discussion: #47517 (reply in thread)

If I use the version 5.0.0 of System.Text.Encodings.Web, it does build with all methods. I've tried to debug binlogs of this but I haven't found anything explaining why it resolves correctly via dotnet build and not via Visual Studio (or msbuild).

I don't see why I can't specifically use version 5.0.1 of System.Text.Encodings.Web, hopefully you can work out what is going on!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions