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

System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1 when running a Test Adapter Visual Studio Extension on VS 17.7.x or above. #4775

Open
felipegodias opened this issue Nov 28, 2023 · 3 comments
Assignees

Comments

@felipegodias
Copy link

Description

System.Runtime.CompilerServices.Unsafe assembly fails to load the correct version when creating an test adapter extension and this only happens on projects running on VS version 17.7.x and beyond. Works fine when running on 17.6.x or previous versions.

Note that adding a listener to AppDomain.CurrentDomain.AssemblyResolve and manually handle which Assembly to load kinda solves the issue but it seems to be an ugly workaround. Also, Visual Studio extensions can not leverage App.config for assembly version rebindings.

This issue seems similar to #4673 but GenerateBindingRedirectsOutputType does not work in this case.

The zipped solution is attached to this issue.
BindingIssue.zip

Steps to reproduce

1 - Set the VSTEST_HOST_DEBUG environment variable to 1. This is needed so the testhost.exe can be attached to the debugger.
2 - Run the BindingIssue extension project in a visual studio version 17.7.x or greater.
3 - When running the BindingIssue via VS, it should create a new instance of the VS so the extension can be tested.
4 - On the new instance, open a solution that outputs an .dll library.
5 - Open the VS test explorer window.
6 - Attach the testhost.exe process in the debugging BindingIssue project.
7 - Notice that when calling Task.Wait or Task.WaitAll or yield return the process tries to load the assembly System.Runtime.CompilerServices.Unsafe Version=4.0.4.1 instead of the one installed in the nuget package.

Note: There's a menu item button in VS under "Extensions/BindingIssue/Run Binding Issue Command" this command tries to emulate how the test adapter is called, and seems to work fine without trying to load the wrong version of the Unsafe assembly.

Expected behavior

The correct assembly should be loaded

Actual behavior

It should not throw:
System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

The correct version of the Unsafe assembly must be loaded.

Environment

Windows 10 and 11
Visual Studio version 17.7.x or above.

@josesimoes
Copy link

josesimoes commented Apr 8, 2024

I'm seeing this too.
Ended up having to revert the library (CLIWrap) to v3.6.1. Because it has a nested reference to System.Runtime.CompilerServices.Unsafe. Anything beyond that breaks the adapter completly. Not even test discovery works.
See nanoframework/nanoFramework.TestFramework#248

@nohwnd
Copy link
Member

nohwnd commented Jul 11, 2024

I don't think I follow the original repro. @josesimoes do you please have a simple repro for this? Do you still experience this problem on some of the later releases?

@josesimoes
Copy link

Following a migration from MVVM Light to CommunityToolkit.Mvvm nanoframework/nf-debugger#379 (using v8.3.2) .NET nanoFramework debugger which is used in our test framework, caused a failure to run test because of this.

Follows the log with the fusion log messages (apologies for part of it being in Portuguese):

[2024-11-26 6:29:30.995 ]  [nanoTestAdapter]: Exception raised in the process: System.AggregateException: One or more errors occurred. ---> System.IO.FileNotFoundException: Não foi possível carregar o ficheiro ou assemblagem 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ou uma das respectivas dependências. O sistema não conseguiu localizar o ficheiro especificado.
   at System.Threading.Tasks.ValueTask.get_IsCompleted()
   at CliWrap.Command.<ExecuteAsync>d__49.MoveNext() in /_/CliWrap/Command.Execution.cs:line 294
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at CliWrap.Buffered.BufferedCommandExtensions.<>c__DisplayClass0_0.<<ExecuteBufferedAsync>b__0>d.MoveNext() in /_/CliWrap/Buffered/BufferedCommandExtensions.cs:line 53
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at nanoFramework.TestAdapter.NanoCLRHelper.InstallNanoClr(LogMessenger logger) in E:\GitHub\nf-nanoFramework.TestFramework\source\TestAdapter\NanoCLRHelper.cs:line 41
   at nanoFramework.TestPlatform.TestAdapter.Executor.<RunTestOnEmulatorAsync>d__19.MoveNext() in E:\GitHub\nf-nanoFramework.TestFramework\source\TestAdapter\Executor.cs:line 659
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at nanoFramework.TestPlatform.TestAdapter.Executor.RunTests(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle) in E:\GitHub\nf-nanoFramework.TestFramework\source\TestAdapter\Executor.cs:line 128
---> (Inner Exception #0) System.IO.FileNotFoundException: Não foi possível carregar o ficheiro ou assemblagem 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ou uma das respectivas dependências. O sistema não conseguiu localizar o ficheiro especificado.
File name: 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at System.Threading.Tasks.ValueTask.get_IsCompleted()
   at CliWrap.Command.<ExecuteAsync>d__49.MoveNext() in /_/CliWrap/Command.Execution.cs:line 294
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at CliWrap.Buffered.BufferedCommandExtensions.<>c__DisplayClass0_0.<<ExecuteBufferedAsync>b__0>d.MoveNext() in /_/CliWrap/Buffered/BufferedCommandExtensions.cs:line 53

=== Informações de estado anteriores ao enlace ===
REG: DisplayName = System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
REG: Appbase = file:///C:/Program Files/Microsoft Visual Studio/2022/Enterprise/Common7/IDE/Extensions/TestPlatform/
REG: PrivatePath inicial = NULL
A chamar assemblagem: System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51.
===
REG: Este enlace tem início no contexto de carregamento LoadFrom.
AVI: A imagem nativa não será pesquisada no contexto LoadFrom. A imagem nativa só será pesquisada no contexto de carregamento predefinido, tal como Assembly.Load().
REG: A utilizar ficheiro de configuração da aplicação: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\TestPlatform\testhost.net48.exe.Config
REG: A utilizar ficheiro de configuração de anfitrião: 
REG: A utilizar ficheiro de configuração do computador a partir de C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
REG: Referência pós-política: System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
REG: O mesmo enlace foi verificado antes e falhou com hr = 0x80070002.
<---

Only after moving CommunityToolkit.Mvvm to v7.1.2, this issue is gone.

I've tried all the possible options that I could think of related with this: app.config, , . Nothing worked.

This is kind of easy to reproduce.

  1. Clone https://github.com/nanoframework/nf-debugger
  2. Clone https://github.com/nanoframework/nanoFramework.TestFramework/tree/vstest-4775
  3. Load nanoFramework.TestFramework\poc\NFUnit Test DemoByReference.sln
  4. Adjust path to nf-debugger project in nanoFramework.TestAdapter.csproj
  5. Adjust path of test adapters in nanoFramework.TestFramework\poc\TestOfTestFrameworkByReference\nano.runsettings
  6. Build, run tests in test explorer, all good.
  7. Go to nd-debugger project update CommunityToolkit.Mvvm to v8.n.n
  8. Clean, rebuild solution
  9. Run tests again -> error message -> look into test output pane and the error is there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants