Skip to content

SourceGenerators in shared compilation can cause MissingMethodException #66104

@hymccord

Description

@hymccord

Version Used:
Visual Studio v17.5p1
Roslyn in .NET 7.0.101 and in .NETFramework 4.7.2 (the one that ships with VS)

Steps to Reproduce:

> mkdir CTRepro
> cd CTRepro
> dotnet new console
> dotnet add package CommunityToolkit.Mvvm -v 8.1.0-preview2
> dotnet add package Microsoft.Windows.CsWin32 -v 0.2.164-beta

then add a new .cs file so the CommunityToolkit SourceGenerators will run

using CommunityToolkit.Mvvm.Input;

namespace CTRepro;

internal partial class Model
{
    [RelayCommand]
    private void Execute() { }
}

Then you can either build in VS or use msbuild -restore /t:Rebuild to trigger the warning

CSC : warning CS8785: Generator 'RelayCommandGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'MissingMethodException' with message 'Method not found: 'System.ReadOnlySpan`1<!0> System.Collections.Immutable.ImmutableArray`1.AsSpan()'.' [C:\Users\hank.mccord\source\repos\CTRepro\CTRepro.csproj]

Expected Behavior:

The generated code should include the ICommand that is generated

Actual Behavior:

Because the source generator encountered an error, the associated IL is missing entirely.
If you build with dotnet build, it works as expected.

Investigation/Cause

A couple things collide to cause this:

  • Using msbuild or Visual Studio uses the full framework MSBuild (i.e. no ALCs) under a shared compilation context
  • CsWin32 is shipping System.Memory in their analyzers directory
  • VBCSCompiler eventually loads System.Memory in the LoadFrom context (even though System.Memory is in the probing path)

I hope this enough information. Someone who is very familiar with assembly loading process should get the gist of what's going on.
The .NET version of roslyn has the names of simple assemblies that will be provided in by the compiler but the .NETFramework has a completely different loading process since there are no ALCs.

Is this a concern to the Roslyn team or should the fix be CsWin32 package not shipping Roslyn provided dlls in their analyzer folder?

[Update(jcouv):] This issue is referenced by some skipped tests.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions