PowerShell doesn't respect the current directory when invoked from Visual Studio Code

Created on 5 Nov 2017  路  12Comments  路  Source: PowerShell/PowerShell

Note: While I haven't looked at what's going on under the hood, the fact that other shells (cmd.exe, WSL bash, bash on Unix, Git bash) do _not_ exhibit this problem leads me to suspect that PowerShell is the culprit.

Steps to reproduce

  • Open Visual Studio Code (tested with v1.17.2)
  • Make PowerShell the default shell by adding the following to the user preferences:

    • Windows: "terminal.integrated.shell.windows": "pwsh"
    • macOS: "terminal.integrated.shell.osx": "pwsh", "terminal.integrated.shellArgs.osx": []
    • Linux: "terminal.integrated.shell.linux": "pwsh"
  • Open a folder that has subfolders.

  • In the Explorer view in the side bar, right-click on a subfolder and choose Open in Terminal.

Expected behavior

The PowerShell instance that is created in the integrated terminal should have the right-clicked subfolder as the current location.

Actual behavior

The PowerShell instance that is created in the integrated terminal _invariably_ has the folder that was originally opened in Visual Studio Code as the current location.

Environment data

PowerShell Core v6.0.0-beta.9 on macOS 10.13
PowerShell Core v6.0.0-beta.9 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.9 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Issue-Question Resolution-Answered

Most helpful comment

Had the same problem. Managed to solve it by adding if statement into my PowerShell $PROFILE script:

if ($env:VSCODE_CWD)
{
    # VS Code environment
}
else
{
    Set-Location -Path $env:USERPROFILE
}

All 12 comments

VS Code sent current path as parameter?

No; from what I can tell, it just invokes the configured shell _from the target directory_.

I have a _vague_ hunch that this may be related to PowerShell maintaining its own current location, independently of the underlying .NET working directory.

If it just invokes the configured shell from the target directory any from the shells should get the same directory as current. I wonder :-)

Yes, it is mysterious. As stated, bash and cmd.exe behave as expected.

If anyone is looking for a workaround: see this SO answer.

The workaround uses bash / cmd.exe to invoke PowerShell _with an explicit Set-Location call_.

I want to see VS Code code that call the shell.

After looking at this briefly, my guess is that the code is in terminalInstance.ts.

(I don't have time to look into this; doing so would take me a while.)

We could a right thing - open Issue-Question in VS Code repo. If they conclude that they can not fix and it is PowerShell issue we'll continue here.

This is working for me on my MacBook Pro. Started vscode from the root of my PowerShell repo folder. Right click on a subfolder and using Open in Terminal (after configuring default to pwsh and clearing out the osx args), it opens to the subfolder.

It is working for me as well on Windows 10 FCU - VSCode 1.17.2.

My (embarrassingly) bad, guys:

I had custom code in my profile that changed the current location.
The fact that the OP of the linked SO question seemed to experience the same - nonexistent - problem made me see a pattern that wasn't there.

I'm closing this - sorry for the noise.

Thanks @mklement0 for this issue, it helped me a lot because I had the same stupid brain fail 馃槃

Had the same problem. Managed to solve it by adding if statement into my PowerShell $PROFILE script:

if ($env:VSCODE_CWD)
{
    # VS Code environment
}
else
{
    Set-Location -Path $env:USERPROFILE
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

JohnLBevan picture JohnLBevan  路  3Comments

SteveL-MSFT picture SteveL-MSFT  路  3Comments

andschwa picture andschwa  路  3Comments

aragula12 picture aragula12  路  3Comments

pcgeek86 picture pcgeek86  路  3Comments