dotnet / roslyn-sdk

Roslyn-SDK templates and Syntax Visualizer
MIT License
498 stars 254 forks source link

how to use `System.Collections.Immutable` v`8.0.0` with `Microsoft.CodeAnalysis.Testing.ReferenceAssemblies.Net.Net60` #1155

Open LukasGelke opened 2 months ago

LukasGelke commented 2 months ago

our code-analyzer-tests use Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest. We now had to upgrade System.Collections.Immutable to 8.0.0 for all Assemblies. Whenever we now want to use these assemblies to run within tests (because we have tests specifically for Classes inside our assemblies) we get an Exception:

// error CS1705: Assembly 'Komsa' with identity 'Komsa, Version=2.23.1.0, Culture=neutral, PublicKeyToken=null' uses 'System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Collections.Immutable' with identity 'System.Collections.Immutable, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
DiagnosticResult.CompilerError("CS1705").WithArguments("Komsa", "Komsa, Version=2.23.1.0, Culture=neutral, PublicKeyToken=null", "System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Collections.Immutable", "System.Collections.Immutable, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),

adding

new Verifier.Test()
{
  TestState =
  {
    AdditionalReferences = { typeof(ImmutableArray).Assembly  },
  }
}

results in the following:

Actual diagnostic:
    // /0/Test0.cs(13,5): error CS0433: The type 'ImmutableArray<T>' exists in both 'System.Collections.Immutable, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
DiagnosticResult.CompilerError("CS0433").WithSpan(13, 5, 13, 27).WithArguments("System.Collections.Immutable, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Collections.Immutable.ImmutableArray<T>", "System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),

adding the NugetPackage like this does not change anything (in either above case):

new Verifier.Test()
{
  ReferenceAssemblies = ReferenceAssemblies.Net.Net60 // used always
    .AddPackages([new PackageIdentity("System.Collections.Immutable", "8.0.0")]) // new
}

Our verifiers are essentially a "thin wrapper" around CodeFixVerifier<TAnalyzer, TCodeFix, TTest, TVerifier> and Verifier.Test around CSharpCodeFixTest<TAnalyzer, TCodeFix, TVerifier>, to allow for more testing and better migrating from our old tests, and as a workaround for other bugs in the past (ie tests not being able to handle 2-space indents correctly). We do not interfere with Roslyn in this subclass, we "only extend/append" (editorconfig-spacing) on what the base implementation provides. Thus I don't see us being the reason for "something not working"

sharwell commented 2 months ago

This is expected to work:

  ReferenceAssemblies = ReferenceAssemblies.Net.Net60 // used always
    .AddPackages([new PackageIdentity("System.Collections.Immutable", "8.0.0")]) // new

Someone would need to add a unit test to MetadataReferenceTests and debug through why it's not working.