单元测试在 Windows 的 VSCode 中有效,但在 Linux 中无效

Unit Testing Works in VSCode on Windows but not Linux

我在 VSCode (1.66.2) 的 Ubuntu 20.04 上调试 .NET6 运行ning 中的 Xunit 单元测试时遇到问题。这些 运行 在 VisualStudio 中和 VSCode 在 Windows 中。我可以 运行(不调试)来自 CLI 或 .NET Core Test Explorer 扩展的 Linux 中的测试。

如果我尝试使用上下文菜单或放置在单元测试方法声明上方的小魔法命令属性来调试单元测试,我会收到错误消息:

Failed to start debugger: 
"System.InvalidOperationException: The debugger could not be started
at OmniSharp.DotNetTest.Services.DebugTestService.Handle 
(OmniSharp.DotNetTest.Models.DebugTestGetStartInfoRequest request) [0x00050] in <9d87b466e5a04dffb95becc0b4dfd560>:0
at OmniSharp.Endpoint.Exports.RequestHandlerExportHandler`2[TRequest,TResponse].Handle (TRequest request) [0x00000] in <3d93fabea6954f4ba147450b0cf4332a>:0
at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].GetFirstNotEmptyResponseFromHandlers (OmniSharp.Endpoint.Exports.ExportHandler`2[TRequest,TResponse][] handlers, TRequest request) [0x00022] in <3d93fabea6954f4ba147450b0cf4332a>:0
at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].HandleRequestForLanguage (System.String language, TRequest request, OmniSharp.Protocol.RequestPacket packet) [0x00163] in <3d93fabea6954f4ba147450b0cf4332a>:0
at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].Process (OmniSharp.Protocol.RequestPacket packet, OmniSharp.Endpoint.LanguageModel model, Newtonsoft.Json.Linq.JToken requestObject) [0x0024b] in <3d93fabea6954f4ba147450b0cf4332a>:0
at OmniSharp.Stdio.Host.HandleRequest (System.String json, Microsoft.Extensions.Logging.ILogger logger) [0x000f3] in <2584067dfcea42a69c19a025cfbc4799>:0 "

我在“输出”窗格中也看到了这个:

/usr/share/dotnet/sdk/6.0.202/Microsoft.Common.CurrentVersion.targets(4650,5): error MSB3883: Unexpected exception:  [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]
/usr/share/dotnet/sdk/6.0.202/Microsoft.Common.CurrentVersion.targets(4650,5): error : DirectoryNotFoundException: Could not find a part of the path '/home/danielp/Source/Experiments/LibAndTest/Library/bin/Debug/net6.0/ref/Library.dll'. [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]
/usr/share/dotnet/sdk/6.0.202/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter) [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]
/usr/share/dotnet/sdk/6.0.202/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]
/usr/share/dotnet/sdk/6.0.202/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) [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]
/usr/share/dotnet/sdk/6.0.202/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]
/usr/share/dotnet/sdk/6.0.202/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]
/usr/share/dotnet/sdk/6.0.202/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Microsoft.CodeAnalysis.BuildTasks.CopyRefAssembly.Copy() [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]
/usr/share/dotnet/sdk/6.0.202/Microsoft.Common.CurrentVersion.targets(4650,5): error :  [/home/danielp/Source/Experiments/LibAndTest/Library/Library.csproj]

我在这里发现有趣的是,它一直在 ...Debug/net6.0/ref/Library.dll' 中寻找引用的 dll,并且那里没有构建输出二进制文件,更不用说一个名为 ref.

的文件夹了

现在,我可以使用控制台应用程序进行调试,但它在 Windows 而不是 Linux 中运行得很好,这确实看起来很奇怪。不过,既然说出来了:好像也没什么好惊讶的。

最后,我确实尝试调试 mstest 单元测试,结果相同。

我想通了!但只是因为我发现了另一个已回答的问题:

因为我是 运行 .NET 6,Omnisharp 扩展需要更改设置:

  • 打开带有左侧图标的 VSCode 扩展程序
  • 在文本框中搜索 Omnisharp
  • 单击齿轮图标并从出现的上下文菜单中选择 Extension Settings
  • 在设置页面显示的搜索栏顶部,附加(带 space)modern 以便搜索栏包含 @ext:ms-dotnettools.csharp modern
  • 您会看到一个标记为 Omnisharp: Use Modern Net. 的复选框
    • 确保选中此项。
  • 我不得不重新启动 VSCode 并允许扩展进行下载

之后就可以了。