无法 运行 或在 VSCode 中使用 CodeLens 调试 C# 测试

Cannot Run or Debug C# tests with CodeLens in VSCode

我正在尝试 运行 使用 运行 CodeLens 在 VSCode 内进行我的 xUnit 测试 .

我在带有 C# VSCode 扩展 v1.24.1 的 2019 MacBook Pro(英特尔)上使用 VSCode 1.65.0 和 .Net 6 (6.0.200)。

我 运行 使用 dotnet test 命令、.NET Core Test Explore 插件和 VS2022,它们按预期工作。

这是我得到的输出日志

----- Running test method "net6TestTest.TestControllerTest.Get_ShouldReturn_String" -----

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

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

Build FAILED.

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

Time Elapsed 00:00:01.26


要使用 .net 6,您需要更新 Omnisharp 设置以使用 'modern' 方式

您需要切换的设置是 omnisharp.useModernNet。它在 C# 扩展的读取中有描述。