microsoft / vstest

Visual Studio Test Platform is the runner and engine that powers test explorer and vstest.console.
MIT License
898 stars 323 forks source link

Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader - 16.0.2-preview #2008

Closed tim-arheit closed 5 years ago

tim-arheit commented 5 years ago

Description

When running the unit tests in an Azure devops build pipeline "(https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/vstest?view=azure-devops) using 16.0.2-preview Many of the unit tests throw the error:

System.AggregateException : One or more errors occurred. (The type initializer for 'Velocity.Common.Processors.Base.Common.ProcessorBaseFactory' threw an exception.) ----> System.TypeInitializationException : The type initializer for 'Velocity.Common.Processors.Base.Common.ProcessorBaseFactory' threw an exception. ----> System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. ----> System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Reverting back to 16.0.1 solves the issue.

Steps to reproduce

Run unit tests in Azure Devops (Visual Studio Test), after running 'Visual Studio Test Platform Installer' set to Latest (Includes-Pre-release) which installs and causes tests to run on 16.0.2-preview

Expected behavior

No error

Actual behavior

Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader

Environment

Azure DevOps, Visual Studio 2017 Pro, Windows Server 2016

singhsarab commented 5 years ago

@tim-arheit Is that the complete stack trace? Please run the build with System.Debug= true, share the logs with us. Is it possible for you share some a repro project/link to pipeline with us?

tim-arheit commented 5 years ago

I have found the offending code which is suppose to load a list of types that implement a particular interface: `Type type = typeof(ICommand);

        IEnumerable<Type> types = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(s => s.GetTypes())
            .Where(p => type.IsAssignableFrom(p) && p.IsClass);`

It works in production, but when running unit tests on 16.0.2 or 16.1.0 it is failing when calling GetTypes on 'Microsoft.VisualStudio.ArchitectureTools.PEReader'. throwing the error: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. File name: 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

I don't know what changed with the test runner to cause this, but the simple fix is to filter out the assemblies we are calling GetTypes on first.

Thanks

singhsarab commented 5 years ago

@tim-arheit I cannot think of anything that seems related to this, with respect to changes in the test platform.

From your comment I am inferring that this was something in your code that you have fixed now. Is that correct?

valentinajemuovic commented 5 years ago

@singhsarab, today I got the same error.

My tests were working in the past, but today after I updated my Visual Studio to the latest version (I have VS 2019 and updated it today to Version 16.1, and after that update, my tests started failing with the same error.

In my code I also have the line of code, which I used to tell AutoMapper to scan all asemblies (in the past it worked):

AppDomain.CurrentDomain.GetAssemblies().

As can be seen below, error is same as posted by @tim-arheit above:

System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Thanks, Valentina

Result StackTrace:  
----- Inner Stack Trace #1 (System.Reflection.ReflectionTypeLoadException) -----
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeAssembly.get_DefinedTypes()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action`2 configAction, IEnumerable`1 assembliesToScan, ServiceLifetime serviceLifetime) in C:\projects\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line 82
   at Optivem.NorthwindLite.Web.Startup.ConfigureServices(IServiceCollection services) in D:\GitHub\optivem\framework-dotnetcore\samples\NorthwindLite\src\Optivem.NorthwindLite.Web\Startup.cs:line 82
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at Optivem.Test.Xunit.AspNetCore.BaseTestClient`1..ctor() in D:\GitHub\optivem\framework-dotnetcore\test\Base\Xunit.AspNetCore\BaseTestClient.cs:line 18
   at Optivem.Test.Xunit.AspNetCore.BaseTestJsonClient`1..ctor()
   at Optivem.NorthwindLite.Web.Test.Fixture.TestClient..ctor() in D:\GitHub\optivem\framework-dotnetcore\samples\NorthwindLite\test\Optivem.NorthwindLite.Web.Test\Fixture\TestClient.cs:line 19
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----
Result Message: 
System.AggregateException : One or more errors occurred. (Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.) (The following constructor parameters did not have matching fixture data: TestClient client)
---- System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
---- The following constructor parameters did not have matching fixture data: TestClient client
dukeofharen commented 5 years ago

@singhsarab, today I got the same error.

My tests were working in the past, but today after I updated my Visual Studio to the latest version (I have VS 2019 and updated it today to Version 16.1, and after that update, my tests started failing with the same error.

In my code I also have the line of code, which I used to tell AutoMapper to scan all asemblies (in the past it worked):

AppDomain.CurrentDomain.GetAssemblies().

As can be seen below, error is same as posted by @tim-arheit above:

System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Thanks, Valentina

Result StackTrace:    
----- Inner Stack Trace #1 (System.Reflection.ReflectionTypeLoadException) -----
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeAssembly.get_DefinedTypes()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action`2 configAction, IEnumerable`1 assembliesToScan, ServiceLifetime serviceLifetime) in C:\projects\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line 82
   at Optivem.NorthwindLite.Web.Startup.ConfigureServices(IServiceCollection services) in D:\GitHub\optivem\framework-dotnetcore\samples\NorthwindLite\src\Optivem.NorthwindLite.Web\Startup.cs:line 82
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at Optivem.Test.Xunit.AspNetCore.BaseTestClient`1..ctor() in D:\GitHub\optivem\framework-dotnetcore\test\Base\Xunit.AspNetCore\BaseTestClient.cs:line 18
   at Optivem.Test.Xunit.AspNetCore.BaseTestJsonClient`1..ctor()
   at Optivem.NorthwindLite.Web.Test.Fixture.TestClient..ctor() in D:\GitHub\optivem\framework-dotnetcore\samples\NorthwindLite\test\Optivem.NorthwindLite.Web.Test\Fixture\TestClient.cs:line 19
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----
Result Message:   
System.AggregateException : One or more errors occurred. (Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.) (The following constructor parameters did not have matching fixture data: TestClient client)
---- System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
---- The following constructor parameters did not have matching fixture data: TestClient client

I've also just updated Visuals Studio 2019 and also got this exception when running my unit tests. Most of them ran fine, but I have some integration tests where I query the assemblies in the AppDomain for adding them to my IServiceCollection. For some reason, when running the unit tests from within Visual Studio, my code tries to get the types within the namespace Microsoft.VisualStudio.ArchitectureTools.PEReader, but the file can't be loaded. I've circumvented this issue by explicitly filtering the assemblies on a substring. Now, it works fine. (still, a dirty fix for something that shouldn't be a problem at all, but I'm happy it works.

My broken code:

public static IEnumerable<Type> GetImplementations<TInterface>()
{
    var types = AppDomain.CurrentDomain
        .GetAssemblies()
        .SelectMany(s => s.GetTypes())
        .Where(p => typeof(TInterface).IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract)
        .ToArray();

    return types;
}

My working code:

public static IEnumerable<Type> GetImplementations<TInterface>(string assemblyFilter = "")
{
    var assemblies = AppDomain.CurrentDomain
        .GetAssemblies();
    if (!string.IsNullOrWhiteSpace(assemblyFilter))
    {
        assemblies = assemblies.Where(a => a.FullName.Contains(assemblyFilter)).ToArray();
    }

    var types = assemblies
        .SelectMany(s => s.GetTypes())
        .Where(p => typeof(TInterface).IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract)
        .ToArray();

    return types;
}
valentinajemuovic commented 5 years ago

Based on all the above, it seems that the quick fix in all 3 cases seems to be additional filtering of assemblies to prevent scanning the assemblies that are referencing Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0

The interesting question is why it's searching for Microsoft.VisualStudio.ArchitectureTools.PEReader Version=16.0.0.0.... Perhaps that's why the tests work with VS 16.0 and tests fail with VS 16.1...

kasnti commented 5 years ago

I also got the same exception,AutoMapper's dependency injection test can work in VS16.0, but there is a problem in VS16.1. I have the line of code, which I used to tell AutoMapper to scan all asemblies (in the past it worked): services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); so Instead of scanning all asemblies, I specified to provide the assembly containing the 'Profile' to AutoMapper: services.AddAutoMapper(Assembly.Load("Models")); The "Models" project contains AutoMapper Profile configuration files. it works fine.

StevenRasmussen commented 5 years ago

Just wanted to add that I'm now getting this exception as well. Same scenario where I am scanning all assemblies and their types in a unit test.

benizri-ofir commented 5 years ago

the same issue for me, the unit-test used to pass before moving to visual studio 2019 16.1.1

my code: m_fieldValidatorTypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(t => type.IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface) .ToDictionary(ft => ft.FullName, ft => ft);

getting this exception: System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types. Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'

EskilSaatvedt commented 5 years ago

We have the same problem. It was mentioned in the release notes for 16.1.1 but is persisting. Rollback to visual studio 2017 works for now.

StevenRasmussen commented 5 years ago

@EskilSaatvedt - I'm not seeing it anywhere in the release notes for 16.1.1. I am hoping that a fix makes it into 16.1.2 or 16.2 though.

olivermue commented 5 years ago

Just ran into the same error after update of Visual Studio to the latest version. After some search I found, that all the failing tests are doing a scan on the loaded assemblies of the application domain. After digging a little deeper, the following code produces the error:

[Fact]
public void TryToAccessTypesOfTraceCollectorShouldNotFail()
{
    var failingAssembly = AppDomain.CurrentDomain.GetAssemblies()
        .FirstOrDefault(assembly => assembly.FullName.StartsWith("Microsoft.VisualStudio.TraceDataCollector"));

    if (failingAssembly != null)
    {
        var typeInfos = failingAssembly.DefinedTypes;
    }
}

Be aware that this error really only occurs if you run the test within Visual Studio 16.1.1 through the Test Explorer.

olivermue commented 5 years ago

And just for the people searching for an easy to use solution for their tests. Replace

AppDomain.CurrentDomain.GetAssemblies()

with

AppDomain.CurrentDomain.GetAssemblies().Where(assembly => !assembly.FullName.StartsWith("Microsoft.VisualStudio.TraceDataCollector"))

Maybe you get an error, cause the return value of this call is changed to IEnumerable<Assembly> instead of being Assembly[]. Perhaps a good day to make the corresponding functions less demanding and changing their signatures accordingly. If you really need that array then add .ToArray().

xygon commented 5 years ago

I am also getting this error when running tests after updating to Visual Studio 16.1.1

The code under test is using mediatr and that is scanning assemblies looking for classes that implement specific interfaces an that is where the error gets thrown.

The tests run fine from the command line.

olivermue commented 5 years ago

@xygon When initializing MediatR in asp core you use the extension method services.AddMediatR(). This will scan the whole application domain. Instead call services.AddMediatR(assemblies) and for the assemblies variable you provide the list of assemblies excluding the TraceDataCollector like shown in my answer.

olivermue commented 5 years ago

Just updated to 16.1.2 and tried again. Unfortunately the error is NOT fixed. So hoping for the next version.

TheMadStrater commented 5 years ago

Just updated to 16.1.2 and tried again. Unfortunately the error is NOT fixed. So hoping for the next version.

I agree, this issue persists!

vagisha-nidhi commented 5 years ago

@olivermue Can you please check if the issue still persists with 16.2.0-preview-20190606-02?

olivermue commented 5 years ago

Took my unit test from above and created a new xUnit test project with VS Community 16.2.0 Preview 1.0. The error is still:

System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
    Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Das System kann die angegebene Datei nicht finden.
vagisha-nidhi commented 5 years ago

@olivermue The fix will be available in VS 16.2.0 Preview 3. Meanwhile, if you are running the unit tests in an Azure devops build pipeline, you can simply select 16.2.0-preview-20190606-02 version in the tools installer.

divinci commented 5 years ago

Perhaps a good day to make the corresponding functions less demanding and changing their signatures accordingly.

Yes @olivermue !

vipetrul commented 5 years ago

Getting the same exception when Debugging NUnit tests that utilize TestCaseSource attribute.

olivermue commented 5 years ago

Tested with VS Community 16.2.0 Preview 3.0 and the error is FIXED.

singhsarab commented 5 years ago

@olivermue Thanks for confirming. Thanks everyone for your patience.

timmsch commented 5 years ago

The issue still seems to exist, even when using the Visual Studio Test Platform Installer. Version 16.2.0 (stable) Error details: Could not load file or assembly 'Microsoft.TeamFoundation.TestManagement.Controller, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

jjxtra commented 5 years ago

Why is this closed? I get this error in latest visual studio 2019 when running unit tests, calling assembly.GetTypes()... No such issue in VS 2017.

Should not be closed until a non-preview release is out that fixes the issue.

jjxtra commented 5 years ago
/// <summary>
/// Get all types from all assemblies
/// </summary>
/// <returns>List of all types</returns>
public static List<Type> GetAllTypes()
{
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    List<Type> allTypes = new List<Type>();
    foreach (Assembly assembly in assemblies)
    {
        try
        {
            // some assemblys throw in unit tests in VS 2019, bug in MSFT...
            allTypes.AddRange(assembly.GetTypes());
        }
        catch
        {
        }
    }
    return allTypes;
}
jannikbuschke commented 5 years ago

I ran into this issue with the following code

services.AddAutoMapper(cfg => {  
  cfg.AddCollectionMappers();
});

Explicitly adding the assembly fixed it for me:

services.AddAutoMapper(cfg => {
  cfg.AddCollectionMappers();
}, typeof(Startup).Assembly);

VS 16.1.5 dotnetcore 2.2

bencollinsuk commented 5 years ago

I still get this problem - it prevents my unit tests from running.

Microsoft Visual Studio Enterprise 2019 Version 16.1.6 VisualStudio.16.Release/16.1.6+29102.190

olivermue commented 5 years ago

Yes, in Enterprise this error still exists (just tested it again, also with 16.1.6). Due to the fact, that it is solved in VS Community 16.2.0 Preview 3.0 I would guess we have to wait till the Enterprise Version 16.2.x arrives.

And in case you use Azure DevOps, you have to wait till Microsoft releases a new hosted agent image. Depending on how Microsoft names this agent, you then have also to configure your build pipeline to use this new version, but after taking a look at the Microsoft docs, changes are high, that they make an in-place update but you never know (and especially you don't know when).

So my current advice would be to implement my suggested workaround above and re-try it at the end of the year to check if the error still persists. If not revert the workaround and if yes make the next try two month later.

pwen090 commented 5 years ago

I am also not clear on what version fixes this and agree this should not have been closed under a preview release. Is there any confirmation that Visual Studio Professional 2019 version 16.2.2 fixes this issue?

TheMadStrater commented 5 years ago

This isn't fixed on any of my versions of VS...not sure why this isn't a trivial fix?!?

xygon commented 5 years ago

I have Visual Studio Professional 16.2.2 and the issue is fixed.

olivermue commented 5 years ago

Just installed Visual Studio Enterprise 2019 Version 16.2.3 and I can confirm the error is fixed. So the only bogus version is within Azure DevOps the agent pool image Hosted Windows 2019 with VS2019 image, cause it contains VisualStudio/16.1.6+29102.190