6

i'm currently having a really annoying problem since i ran windows repair on my PC. My wsl remote vscode for some reason that i couldn't find out in the net, isn't able to open the current folder in the terminal. Instead, it opens vscode appdata windows folder(/mnt/c/Users/Jonathan/AppData/Local/Programs/Microsoft VS Code), like in the images linked below:

my current workspace

open in integrated terminal result

My remote settings.json file:

{
  "go.autocompleteUnimportedPackages": true,
  "terminal.integrated.profiles.linux": {
    "bash": {
      "path": "bash",
      "icon": "terminal-bash",
      "cwd": "/home/jonathanr/Documents/workspace"
    },
    "zsh": {
      "path": "zsh"
    },
    "fish": {
      "path": "fish"
    },
    "tmux": {
      "path": "tmux",
      "icon": "terminal-tmux"
    },
    "pwsh": {
      "path": "pwsh",
      "icon": "terminal-powershell"
    }
  }
}

Thanks a lot for any help!

4 Answers 4

13

So, turns out I found the solution. To anyone who's struggling with this problem, the problem was (in my case at least) in one of the environment variables that vscode was running WSL with. It is called PRE_NAMESPACE_PWD. If you run WSL with debug enabled (to do this just go to the WSL remote extension settings and turn on the Remote WSL:Debug option). You'll notice the "env" options in WSL command with all environment variables listed there, and if you keep scrolling you'll notice two variables: PRE_NAMESPACE_PWD and PWD. In this problem, the PRE_NAMESPACE_PWD was pointing to the windows vscode folder instead of the current workspace folder in WSL and the PWD variable was using this folder. To summarize, I just exported this PRE_NAMESPACE_PWD variable with the value ${cwd} which is a command that gets the current workspace folder in my vscode. To do this, simply add this line to your remote Settings.json file:

"terminal.integrated.env.linux": {
    "PRE_NAMESPACE_PWD": "${cwd}"
  }

And that's it, now every time you click on "Open with integrated terminal" your terminal is going to open in the right workspace directory.

Sign up to request clarification or add additional context in comments.

1 Comment

It'd be nice to know why this happens in the first place. Just had this happen, but yesterday everything was fine. It just took the wrong directory all of the sudden. BUT I also had issues with code on WSL before dealing with this (Something about a missing proxy). Could be rough edges since I'm on the insider program, but no idea if I can even reproduce it to report it. Thank for the solution, nevertheless.
2

Previously, you could set this in a single line in your settings.json file using terminal.integrated.shell.windows, but this has since been deprecated in a later version of Visual Studio Code.

  1. Add the following snippet of JSON to your settings.json file in Visual Studio Code

  2. Close any integrated terminals you had open in Visual Studio Code, and then open a new integrated terminal - WSL should now open by default, rather than PowerShell.

         "terminal.integrated.defaultProfile.windows": "WSL",
         "terminal.integrated.profiles.windows": {
           "PowerShell": {
             "source": "PowerShell",
             "icon": "terminal-powershell"
           },
           "Command Prompt": {
             "path": [
               "${env:windir}\\Sysnative\\cmd.exe",
               "${env:windir}\\System32\\cmd.exe"
             ],
             "args": [],
             "icon": "terminal-cmd"
           },
           "Git Bash": {
             "source": "Git Bash"
           },
           "WSL": {
             "path": [
               "C:\\WINDOWS\\System32\\wsl.exe"
             ],
             "args": [],
             "icon": "terminal-ubuntu-wsl"
           }
         }
    

Comments

1

For me:

I have added to the bashrc: cd $OLDPWD and all worked. Also, the solution above works for me.

Comments

1

For me was a mixed solution: I've used Matan's solution but with eatsfood & johnathanrib3 variable PRE_NAMESPACE_PWD, and ONLY if that variable exists, as I use WSL2 for more stuff.

In my case, this worked flawlessly for me by adding it to the end of the .bashrc file:

# ...your existing code in .basrhc

if [ ! -z "$PRE_NAMESPACE_PWD" ]; then
    cd $PRE_NAMESPACE_PWD
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.