'Unable to debug WSL2 with VS 2022 - invalid 'cwd' value

I am trying to debug from WSL2 using my VS 2022 IDE in Windows, but I get the following error:

Invalid cwd value

The cwd value does indeed look wrong, but how do I fix it?

I am using .netcore 6.0.101, Ubuntu 20.04.3 and Windows 11.

launchSettings.json:

{
  "profiles": {
    "WSL": {
      "commandName": "WSL2",
      "launchBrowser": false,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "local"
      },
      "applicationUrl": "https://localhost:56962;http://localhost:56963", 
      "distributionName": "Ubuntu"
    }
  }
}
PS C:\Users\me> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu                 Running         2
  docker-desktop         Running         2
  docker-desktop-data    Running         2


Solution 1:[1]

I tried to search some useful information for you, somebody said this is permisson issue. But I this below description is correct.

Currently the extension only support opening projects that are stored in the windows disks (it leverages WSLs automount feature).

Related Post: Debug Your .NET Core Apps in WSL 2 with Visual Studio

Solution 2:[2]

In Visual Studio 2022:

Go to the project properties (right click on the project name in the Solution Explorer, then Properties on the pop up menu). Then, write in the search bar "working directory" and click on "Open debug launch profiles UI" here you'll find the "Working Directory" setting where you should set the wsl2 path you need.

Solution 3:[3]

You have this error because Windows doesn't know about linux path used on WSL. But you can access your WSL2 directories on Windows using \\wsl$ path.

If your WSL2 machine name is Ubuntu, you need to define cwd value with following path:

\\wsl$\Ubuntu\home\<the_rest_of_the_path_you_need>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason
Solution 2 Kvble
Solution 3 dgp