Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use MSBuildWorkspace in .NET Core net46 mstest test assembly #595

Closed
fubar-coder opened this issue Mar 10, 2017 · 1 comment
Closed
Assignees

Comments

@fubar-coder
Copy link

fubar-coder commented Mar 10, 2017

Description

When I try to use MSBuildWorkspace in a .NET Core csproj targeting net46, I get assembly load exceptions.

Steps to reproduce

  1. Create a component test project (.NET Core)
  2. Change TargetFramework from netcoreapp1.1 to net46
  3. Add Microsoft.CodeAnalysis version 2.0.0 to the unit test project
  4. Create the MSBuildWorkspace in the test method using MSBuildWorkspace.Create();
  5. Run the unit tests

Expected behavior

No error

Actual behavior

A System.Reflection.ReflectionTypeLoadException is thrown. The LoaderExceptions contains the following fusion log:

=== Zustandsinformationen vor Bindung ===
LOG: DisplayName = System.Collections.Immutable, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///c:/users/mark/documents/visual studio 2017/Projects/TestRoslyn20/TestRoslyn20Tests/bin/Debug/net46
LOG: Ursprünglicher PrivatePath = NULL
Aufruf von Assembly : System.Reflection.Metadata, Version=1.4.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
===
LOG: Diese Bindung startet im default-Load-Kontext.
LOG: Die Anwendungskonfigurationsdatei wird verwendet: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2017\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe.Config
LOG: Die Hostkonfigurationsdatei wird verwendet: 
LOG: Die Computerkonfigurationsdatei von C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config wird verwendet.
LOG: Die gleiche Bindung ist bereits aufgetreten und hat den Fehler hr = 0x80131040 verursacht.

This log shows that System.Reflection.Metadata version 1.4.1.0 tried to load System.Collections.Immutable version 1.2.0.0, but I cannot downgrade to this version, because Roslyn requires 1.2.1 (NuGet package version 1.3.1).

Stack trace:

   bei System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   bei System.Reflection.RuntimeAssembly.get_DefinedTypes()
   bei System.Composition.Hosting.ContainerConfiguration.<WithAssemblies>b__0(Assembly a)
   bei System.Linq.Enumerable.<SelectManyIterator>d__16`2.MoveNext()
   bei System.Composition.TypedParts.TypedPartExportDescriptorProvider..ctor(IEnumerable`1 types, AttributedModelProvider attributeContext)
   bei System.Composition.Hosting.ContainerConfiguration.CreateContainer()
   bei Microsoft.CodeAnalysis.Host.Mef.MefHostServices.Create(IEnumerable`1 assemblies)
   bei Microsoft.CodeAnalysis.Host.Mef.DesktopMefHostServices.get_DefaultServices()
   bei Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create()
   bei TestRoslyn20Tests.UnitTest1.Test1() in c:\users\mark\documents\visual studio 2017\Projects\TestRoslyn20\TestRoslyn20Tests\UnitTest1.cs:Zeile 14.

Upgrading the package Microsoft.Composition doesn't work, because it complains with a FileLoadException that the assembly System.Composition.TypedParts version 1.0.27.0 couldn't be loaded.

This is the stack trace:

   bei Microsoft.CodeAnalysis.Host.Mef.MefHostServices.Create(IEnumerable`1 assemblies)
   bei Microsoft.CodeAnalysis.Host.Mef.DesktopMefHostServices.get_DefaultServices()
   bei Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create()
   bei TestRoslyn20Tests.UnitTest1.Test1() in c:\users\mark\documents\visual studio 2017\Projects\TestRoslyn20\TestRoslyn20Tests\UnitTest1.cs: Zeile14

Using an app.config for the test project doesn't work. The .dll.config gets copied to the output folder, but the binding redirect isn't picked up.

I think that the problem is two-fold:

  1. Microsoft/vstest requires auto generated binding redirects (similar to Test dll's taking dependency on Newtonsoft.Json fail #391)
  2. Roslyn is delivered with incompatible package dependencies (Cannot use MSBuildWorkspace in net46 xunit/mstest test assembly dotnet/roslyn#17705)

Fun fact: Using a .NET Framework console application works without error.

Environment

  • Roslyn NuGet packages 2.0.0
  • Visual Studio 2017 RTM

Workaround

Use a .NET Framework unit test project and add the following lines in the PropertyGroup:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
@codito
Copy link
Contributor

codito commented Mar 21, 2017

Related to #428.

Faizan2304 added a commit to Faizan2304/vstest that referenced this issue Mar 21, 2017
1) microsoft#613
2) microsoft#428
3) microsoft#391
4) microsoft#595

Fix:
Generate config file for test project targeting .NET Framework. This config file has have binding redirect which is needed at time of running tests.
Faizan2304 added a commit that referenced this issue Mar 22, 2017
* Issue:
1) #428
2) #595

Fix:
Generate config file for test project targeting .NET Framework. This config file has binding redirect which is needed at time of running tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants