roslyn: Could not load System.Collections.Immutable

Version Used: Microsoft.CodeAnalysis.CSharp 4.3.0 for .NET 6

Steps to Reproduce:

  1. Get compilation var c = (CSharpCompilation)context.Compilation;
  2. Get globalnamespace and cast him as INamespaceOrTypeSymbol var n = (INamespaceOrTypeSymbol)c.Assembly.GlobalNamespace;
  3. Get members var members = n.GetMembers();

Expected Behavior: No error.

Actual Behavior: Failed to generate source.

error

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

Most upvoted comments

@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:

  1. This bug: Our 4.3.0 NuPkg advertised a dependency on 6.0.0 while the compiler actually deployed 5.0.0. That means anaylzers / generators that compiled against 4.3.0 ended up with a SCI dependency of 6.0.0. When deployed only 5.0.0 would be there , the analyzer wouldn’t load and it would result in a compilation error.
  2. The linked bug: This is the MSBuildSdkResolver failing because it can’t SCI at 5.0.0. That is essentially failing very early in the MSBuild process, way before ti gets to compilation.

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.