roslyn: Could not load System.Collections.Immutable
Version Used: Microsoft.CodeAnalysis.CSharp 4.3.0 for .NET 6
Steps to Reproduce:
- Get compilation
var c = (CSharpCompilation)context.Compilation;
- Get globalnamespace and cast him as
INamespaceOrTypeSymbol
var n = (INamespaceOrTypeSymbol)c.Assembly.GlobalNamespace;
- Get members
var members = n.GetMembers();
Expected Behavior: No error.
Actual Behavior: Failed to generate source.
The error appeared only in version 4.3.0. Connecting nuget System.Collections.Immutable directly does not help. If you comment out this line, then there will be no error:
//var members = n.GetMembers();
Example code for reproduce error Generator code:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace SourceGenerator
{
[Generator]
public class HelloSourceGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
// Find the main method
var mainMethod = context.Compilation.GetEntryPoint(context.CancellationToken);
var c = (CSharpCompilation)context.Compilation;
var n = (INamespaceOrTypeSymbol)c.Assembly.GlobalNamespace;
var members = n.GetMembers();
// Build up the source code
string source = $@"// <auto-generated/>
using System;
namespace {mainMethod.ContainingNamespace.ToDisplayString()}
{{
public static partial class {mainMethod.ContainingType.Name}
{{
static partial void HelloFrom(string name) =>
Console.WriteLine($""Generator says: Hi from '{{name}}'"");
}}
}}
";
var typeName = mainMethod.ContainingType.Name;
// Add the source code to the compilation
context.AddSource($"{typeName}.g.cs", source);
}
public void Initialize(GeneratorInitializationContext context)
{
// No initialization required for this one
}
}
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 9
- Comments: 21 (9 by maintainers)
Commits related to this issue
- temp build fix https://github.com/dotnet/roslyn/issues/63780#issuecomment-1241187595 — committed to StockSharp/StockSharp by mikasoukhov 2 years ago
- Downgrade `Microsoft.CodeAnalysis.CSharp` https://github.com/dotnet/roslyn/issues/63780#issuecomment-1241187595 — committed to CommunityToolkit/Maui.Markup by brminnick 2 years ago
- Downgrade to 4.2.0 to avoid bugs in 4.3 and 4.4 Note: Needed to upgrade >3.11 to avoid https://github.com/dotnet/msbuild/issues/7873 Needed to downgrade < 4.3 to avoid https://github.com/dotnet/rosly... — committed to icsharpcode/CodeConverter by GrahamTheCoder 2 years ago
- Update to VS2022 17.2 only Needed codeanalysis >3.11 to avoid dotnet/msbuild#7873 Needed codeanalysis< 4.3 to avoid dotnet/roslyn#63780 There may have been other workarounds — committed to icsharpcode/CodeConverter by GrahamTheCoder 2 years ago
- VS2022 17.2+ only Needed codeanalysis >3.11 to avoid dotnet/msbuild#7873 Needed codeanalysis< 4.3 to avoid dotnet/roslyn#63780 — committed to icsharpcode/CodeConverter by GrahamTheCoder 2 years ago
- VS2022 17.2+ only Needed codeanalysis >3.11 to avoid dotnet/msbuild#7873 Needed codeanalysis< 4.3 to avoid dotnet/roslyn#63780 — committed to icsharpcode/CodeConverter by GrahamTheCoder 2 years ago
- VS2022 17.2+ only Needed codeanalysis >3.11 to avoid dotnet/msbuild#7873 Needed codeanalysis< 4.3 to avoid dotnet/roslyn#63780 — committed to icsharpcode/CodeConverter by GrahamTheCoder 2 years ago
- VS2022 17.2+ only Needed codeanalysis >3.11 to avoid dotnet/msbuild#7873 Needed codeanalysis< 4.3 to avoid dotnet/roslyn#63780 — committed to icsharpcode/CodeConverter by GrahamTheCoder 2 years ago
@Bosch-Eli-Black
That is a different bug. Yes both bugs are issues building due to a bad version of
System.Collections.Immutable
but the circumstances are different:My suspicion would be that installation was corrupted in (2). But it’s not my area so I’d defer to MSBuild team.
For now we think the workaround for users who want to adopt APIs added in 4.3 is to reference version 4.3.0-3.final, which was released to match 17.3 preview 3. Apologies for the inconvenience.
There is a new release of VS 2022, V17.3.5, it solved the issue for me.
Ah, thanks, @jaredpar! Sorry for the confusion 🙂
I see that in PR #63599 we updated the reference to System.Collections.Immutable to 6.0.0. But in the main branch it is still at 5.0.0, and last modified 3 months ago, which means the PR in 17.3 never flowed forward. Also, a package containing PR #63599 has not yet been inserted to VS, but it looks like the version on NuGet does include PR #63599.
When we load the analyzer in the VS 17.4 preview (and probably in VS 17.3 as well, but I haven’t tested), the compiler ends up trying to load this version, which fails. TBH, I don’t have a lot of insight into why this is happening except that our Resolve event is returning null via this code path.
cc @chsienki @JoeRobich @jaredpar in case you can provide any insight.
@RikkiGibson can you take a look here. Suspect it’s a case where the NuPkg is pushing a new version of S.C.I that doesn’t match what we ship with our binaries.