dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.86k stars 672 forks source link

Debug test from VS code does not work #5533

Open sirdawidd opened 1 year ago

sirdawidd commented 1 year ago

Environment data

.NET SDK (reflecting any global.json): Version: 6.0.403
Commit: 2bc18bf292

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19042 OS Platform: Windows
RID: win10-x64 Base Path: C:\Programs\dotnet-sdk-6.0.403-win-x64\sdk\6.0.403\

global.json file: Not found

Host: Version: 6.0.11 Architecture: x64 Commit: 943474ca16

.NET SDKs installed: 6.0.403 [C:\Programs\dotnet-sdk-6.0.403-win-x64\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.11 [C:\Programs\dotnet-sdk-6.0.403-win-x64\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.11 [C:\Programs\dotnet-sdk-6.0.403-win-x64\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.11 [C:\Programs\dotnet-sdk-6.0.403-win-x64\shared\Microsoft.WindowsDesktop.App]

Download .NET: https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs: https://aka.ms/dotnet/runtimes-sdk-info VS Code version: C# Extension version:

OmniSharp log

OmniSharp server started with .NET 6.0.403 . Path: c:\Users\xxx.vscode\extensions\ms-dotnettools.csharp-1.25.2-win32-x64.omnisharp\1.39.2-net6.0\OmniSharp.dll PID: 24196

    Starting OmniSharp on Windows 10.0.19042.0 (x64)
    Checking the 'DOTNET_ROOT' environment variable to find a .NET SDK
    DotNetPath set to C:\Programs\dotnet-sdk-6.0.403-win-x64\dotnet
    Located 1 MSBuild instance(s)
        1: .NET Core SDK 6.0.403 17.3.2 - "C:\Programs\dotnet-sdk-6.0.403-win-x64\sdk\6.0.403\"
    Registered MSBuild instance: .NET Core SDK 6.0.403 17.3.2 - "C:\Programs\dotnet-sdk-6.0.403-win-x64\sdk\6.0.403\"
    Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
    Detecting Cake files in 'c:\api'.
    Did not find any Cake files
    Skip loading projects listed in solution file or under target directory because MsBuild:LoadProjectsOnDemand is true.
    Detecting CSX files in 'c:\api'.
    Did not find any CSX files
    Configuration finished.
    Omnisharp server running using Stdio at location 'c:\api' on host 32912.

When I try to debug test I see

    Update project: DistributionApi.Tests
    read: {"MessageType":"TestSession.Connected","Payload":null}
    read: {"MessageType":"ProtocolVersion","Payload":1}
    Debug session started.
    Debug session ended.

Steps to reproduce

  1. Try to click on popup of selected test option: Debug Test
  2. You will see

Failed to read environment variable [DOTNET_ROOT], HRESULT: 0x80070002 A fatal error occurred. The required library hostfxr.dll could not be found. If this is a self-contained application, that library should exist in [c:\Users\Api.Tests\bin\Debug\net6.0]. If this is a framework-dependent application, install the runtime in the global location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x64\InstallLocation].

The .NET Core runtime can be found at:

Option Run Test, works fine , I get test results, also debugging is working, if I run it manually: Create test process and attach debugger. Also o .net 5 I didn't faced similar issue.

Expected behavior

Click Debug Test option in Vs Code should run debugger without error.

Actual behavior

Failed to read environment variable [DOTNET_ROOT], HRESULT: 0x80070002 A fatal error occurred. The required library hostfxr.dll could not be found. If this is a self-contained application, that library should exist in [c:\Users\Tests\bin\Debug\net6.0]. If this is a framework-dependent application, install the runtime in the global location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x64\InstallLocation].

The .NET Core runtime can be found at:

sirdawidd commented 1 year ago

After some investigation, it work when I removed from user variables DOTNET_ROOT. However the path was correctly setup, and even error mentioned that I need to define base directory.... Now I only have issues, that some plugins in VS code, does not work, as they cant find installation of .net ....

crimsonduelist commented 1 year ago

I ve got the same problem for some time now. I m using 6.0.406-win-x64 zip version. I ve setup DOTNET_ROOT = path/to/sdk-6.0.406-win-x64 and added %DOTNET_ROOT% to Path It s only the test debugging that s not working. That s my setup on other machines as well and it s working properly.

moly commented 1 year ago

I'm seeing the same problem for both "Run Test" and "Debug Test". I have dotnet 7.0.400 extracted to c:\users\x\AppData\Local\dotnet. DOTNET_ROOT is set and added to PATH. Running dotnet test in the vscode terminal works fine.

moly commented 1 year ago

After playing with this a bit I think I've found a workaround. Setting the environment variable VSTEST_WINAPPHOST_DOTNET_ROOT to my dotnet location fixes it for me.