Debugger does not launch when debugging xUnit tests in C# with VS Code

1k Views Asked by At

I am trying to debug some xUnit tests in VS code but I get this error when I try to manually debug the tests one by one (instead of just running dotnet test which works fine) and I get this error:

----- Debugging test method TournamentTests.OrganizeIntoPairsIncludesAllElements -----

Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error MSB3883: Unexpected exception:  [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error : DirectoryNotFoundException: Could not find a part of the path '/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/bin/Debug/net6.0/ref/best-song.dll'. [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Microsoft.CodeAnalysis.BuildTasks.CopyRefAssembly.Copy() [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :  [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]

Build FAILED.

It appears that the debugger tries to access a directory that doesn't exist, but building and running the project works just fine.

3

There are 3 best solutions below

2
On BEST ANSWER

Add the following to the project your tests are targeting:

  <PropertyGroup>
    <ProduceReferenceAssemblyInOutDir>true</ProduceReferenceAssemblyInOutDir>
  </PropertyGroup>

For example, say you have a project MyProject.csproj and another MyProject.Tests.csproj where your tests reside. You want to add the ProduceReferenceAssemblyInOutDir element to your MyProject.csproj file. This ensures the output dll is added to the ref folder and then the built in debugger should work again.

Hope that helps!

0
On

Alright, I appear to have found a temporary solution: The debugger looks for reference libraries in bin/Debug/net6.0/ref which doesn't exist. The libraries can however be found in obj/Debug/net6.0/ref and simply copying them over to bin seems to solve the problem albeit temporarily, I guess this must be done every time you rebuild the project but at least the debugger runs now.

0
On

There is also a situation where if you are working with .NET daily builds, such as when working on the runtime itself, or the libraries, you will need to configure Visual Studio to skip validating signatures of referenced assemblies before debugging.

See, https://github.com/dotnet/runtime/blob/main/docs/workflow/debugging/coreclr/debugging-runtime.md#resolving-signature-validation-errors-in-visual-studio