Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gitbash shows wrong dir name in commandline prompt while open a python project in visual studio code #5114

Open
1 task done
jingjingan opened this issue Aug 29, 2024 · 13 comments

Comments

@jingjingan
Copy link

jingjingan commented Aug 29, 2024

  • I was not able to find an open or closed issue matching what I'm seeing

Setup

  • Which version of Git for Windows are you using? Is it 32-bit or 64-bit?
$ git --version --build-options

git version 2.37.1.windows.1
cpu: x86_64
built from commit: 323a69709944b193bb5cee81ff09fe9a4a686df5
sizeof-long: 4
sizeof-size_t: 8
shell-path: /bin/sh
feature: fsmonitor--daemon
(.venv) 
  • Which version of Windows are you running? Vista, 7, 8, 10? Is it 32-bit or 64-bit?
$ cmd.exe /c ver

Microsoft Windows [版本 10.0.19045.4780]
  • What options did you set as part of the installation? Or did you choose the
    defaults?
# One of the following:
> type "C:\Program Files\Git\etc\install-options.txt"
> type "C:\Program Files (x86)\Git\etc\install-options.txt"
> type "%USERPROFILE%\AppData\Local\Programs\Git\etc\install-options.txt"
> type "$env:USERPROFILE\AppData\Local\Programs\Git\etc\install-options.txt"
$ cat /etc/install-options.txt

Editor Option: VisualStudioCode
Custom Editor Path:
Default Branch Option: main
Path Option: Cmd
SSH Option: OpenSSH
Tortoise Option: false
CURL Option: OpenSSL
CRLF Option: CRLFAlways
Bash Terminal Option: MinTTY
Git Pull Behavior Option: Merge
Use Credential Manager: Enabled
Performance Tweaks FSCache: Enabled
Enable Symlinks: Disabled
Enable Pseudo Console Support: Disabled
Enable FSMonitor: Disabled
(.venv)
  • Any other interesting things about your environment that might be related
    to the issue you're seeing?

** insert your response here **

Details

  • Which terminal/shell are you running Git from? e.g Bash/CMD/PowerShell/other

run integrated git bash in visual studio code.
I have checked all vistual studio related setting, all good.
this issue only happens while I open a python proejct in visual studio code , and then open git bash in vs code.
visual studio code version:
版本: 1.92.2 (user setup)
提交: fee1edb8d6d72a0ddff41e5f71a671c23ed924b9
日期: 2024-08-14T17:29:30.058Z
Electron: 30.1.2
ElectronBuildId: 9870757
Chromium: 124.0.6367.243
Node.js: 20.14.0
V8: 12.4.254.20-electron.0
OS: Windows_NT x64 10.0.19045

I have checked all vistual studio related setting, all good. 
this issue only happens while I open a python proejct in visual studio code , and then open git bash in vs code.
here is the step:
1. install python(Python 3.12.4) in windows.
2. install Python extension for Visual Studio Code(ms-python.python) addon in vs code.
3. open a new python project first time in vs code 。
4. open gitbash in vscode,the command line prompt shows correct dir name(the python project home dir).
5. run Python: Select Interpreter, then  select "create new visual env", then select "Venv create .venv under current workspace", the wait the steps complete.
6. open gitbash in vscode,the command line prompt shows dir name is "C:/Users/xxx/AppData/Local/Programs/Microsoft VS Code" , and even shows the current git branch name of the python project.
7. run "pwd" command, it shows the correct dir(the python project home dir).

image

  • What did you expect to occur after running these commands?

the command line prompt shows the correct dir name.

  • What actually happened instead?

the command line prompt shows the wrong dir name.

  • If the problem was occurring with a specific repository, can you provide the
    URL to that repository to help us with testing?

no .

@dscho
Copy link
Member

dscho commented Aug 29, 2024

That prompt is set via the PS1 shell variable, specifically via \w. Here is the Bash code that handles it.

One curious thing about this is that it looks at the PWD shell variable and it only calls getcwd() instead if `PWD is not set.

Can you verify via env | grep -i pwd that you have only one such variable? (I could imagine that there is a PWD one and a pwd one.)

If that is not the case, could you see whether there is another shell variable that contains that path, e.g. via `env | grep '=C:.*Microsoft.VS.Code$'?

@jingjingan
Copy link
Author

Please see my reply below.
About this issue ,no matter how I set terminal cwd in vs code, git bash behaves same.

$ env | grep '=C:.*Microsoft.VS.Code$'
PWD=C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code
(.venv) 
hola_@DESKTOP-JO4V5P1 MINGW64 C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code (main)
$ env  | grep -i pwd
PWD=C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code
PS1=\[\](.venv) \[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$ \[\]
(.venv) 
hola_@DESKTOP-JO4V5P1 MINGW64 C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code (main)
$ pwd
/d/ICCLOUDY/12客户/工具/py-stat
(.venv) 

@dscho
Copy link
Member

dscho commented Aug 29, 2024

That's bizarre. What does env | grep py-stat show?

@jingjingan
Copy link
Author

$ env | grep py-stat
VIRTUAL_ENV=D:/ICCLOUDY/12客户/工具/py-stat/.venv
VIRTUAL_ENV=D:/ICCLOUDY/12客户/工具/py-stat/.venv
PATH=c:\Users\hola_\.vscodetensions\ms-python.python-2024.12.3-win32-x64\python_files\deactivatash;D:\ICCLOUDY\12客户\工具\py-stat\.venv\Scripts;C:\Program Files\Git\mingw6in;C:\Program Files\Git\usin;C:\Users\holain;C:\Program Files\OpenSSH;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell
                        1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git/c/Users/hola_/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/hola_/bin:/c/Us      1.0;C:\WINDOW
S\System32\OpenSSH;C:\Program Files\Git/c/Users/hola_/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/hola_/bin:/c/Users/hola_/.vscode/extensions/ms-python.python-2024.12.3-win32-x64/python_files/deactivate/bash:/d/ICCLOUDY/12客户/工具/py-stat/.venv/Scripts:/mingw64/bin:/usr/bin:/c/Users/hola_/bin:/c/Program Files/OpenSSH:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/cmd:/c/Gradle/gradle-7.5.1/bin:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/Program Files/Java/jdk1.8.0_231/bin:/c/Program Files (x86)/GBESClient/GBESClient:/c/Users/hola_/AppData/Local/Programs/Python/Python312/Scripts:/c/Users/hola_/AppData/Local/Programs/Python/Python312:/c/Users/hola_/AppData/Local/Microsoft/WindowsApps:/c/Users/hola_/AppData/Local/Programs/Microsoft VS Code/bin:/c/Program Files/OpenSSH:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/cmd:/c/Gradle/gradle-7.5.1/bin:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/Program Files/Java/jdk1.8.0_231/bin:/c/Program Files (x86)/GBESClient/GBESClient:/c/Users/hola_/AppData/Local/Programs/Python/Python312/Scripts:/c/Users/hola_/AppData/Local/Programs/Python/Python312:/c/Users/hola_/AppData/Local/Microsoft/WindowsApps:/c/Users/hola_/AppData/Local/Programs/Microsoft VS Code/bin:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Users/hola_/.ssh/scripts
ORIGINAL_PATH=/mingw64/bin:/usr/bin:/c/Users/hola_/bin:/c/Users/hola_/.vscode/extensions/ms-python.python-2024.12.3-win32-x64/python_files/deactivate/bash:/d/ICCLOUDY/12客户/工具/py-stat/.venv/Scripts:/mingw64/bin:/usr/bin:/c/Users/hola_/bin:/c/Program Files/OpenSSH:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/cmd:/c/Gradle/gradle-7.5.1/bin:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/Program Files/Java/jdk1.8.0_231/bin:/c/Program Files (x86)/GBESClient/GBESClient:/c/Users/hola_/AppData/Local/Programs/Python/Python312/Scripts:/c/Users/hola_/AppData/Local/Programs/Python/Python312:/c/Users/hola_/AppData/Local/Microsoft/WindowsApps:/c/Users/hola_/AppData/Local/Programs/Microsoft VS Code/bin:/c/Program Files/OpenSSH:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/cmd:/c/Gradle/gradle-7.5.1/bin:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/Program Files/Java/jdk1.8.0_231/bin:/c/Program Files (x86)/GBESClient/GBESClient:/c/Users/hola_/AppData/Local/Programs/Python/Python312/Scripts:/c/Users/hola_/AppData/Local/Programs/Python/Python312:/c/Users/hola_/AppData/Local/Microsoft/WindowsApps:/c/Users/hola_/AppData/Local/Programs/Microsoft VS Code/bin
(.venv)

@dscho
Copy link
Member

dscho commented Aug 29, 2024

And what does realpath "$PWD" "$(pwd)" report?

@dscho
Copy link
Member

dscho commented Aug 29, 2024

VIRTUAL_ENV=D:/ICCLOUDY/12客户/工具/py-stat/.venv
VIRTUAL_ENV=D:/ICCLOUDY/12客户/工具/py-stat/.venv

This is quite strange, by the way. Does VIRTUAL_ENV contain multi-line text?

PATH=c:\Users\hola_\.vscodetensions\ms-python.python-2024.12.3-win32-x64\python_files\deactivatash;D:\ICCLOUDY\12客户\工具\py-stat\.venv\Scripts;C:\Program Files\Git\mingw6in;C:\Program Files\Git\usin;C:\Users\holain;C:\Program Files\OpenSSH;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell
                        1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git/c/Users/hola_/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/hola_/bin:/c/Us      1.0;C:\WINDOW
S\System32\OpenSSH;C:\Program Files\Git/c/Users/hola_/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/hola_/bin:/c/Users/hola_/.vscode/extensions/ms-python.python-2024.12.3-win32-x64/python_files/deactivate/bash:/d/ICCLOUDY/12客户/工具/py-stat/.venv/Scripts:/mingw64/bin:/usr/bin:/c/Users/hola_/bin:/c/Program Files/OpenSSH:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/cmd:/c/Gradle/gradle-7.5.1/bin:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/Program Files/Java/jdk1.8.0_231/bin:/c/Program Files (x86)/GBESClient/GBESClient:/c/Users/hola_/AppData/Local/Programs/Python/Python312/Scripts:/c/Users/hola_/AppData/Local/Programs/Python/Python312:/c/Users/hola_/AppData/Local/Microsoft/WindowsApps:/c/Users/hola_/AppData/Local/Programs/Microsoft VS Code/bin:/c/Program Files/OpenSSH:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/cmd:/c/Gradle/gradle-7.5.1/bin:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/Program Files/Java/jdk1.8.0_231/bin:/c/Program Files (x86)/GBESClient/GBESClient:/c/Users/hola_/AppData/Local/Programs/Python/Python312/Scripts:/c/Users/hola_/AppData/Local/Programs/Python/Python312:/c/Users/hola_/AppData/Local/Microsoft/WindowsApps:/c/Users/hola_/AppData/Local/Programs/Microsoft VS Code/bin:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Users/hola_/.ssh/scripts

And this is actively wrong. The first part of the PATH variable contains a Windows-style path list, with absolute Windows paths containing drive prefixes that are separated by ;. The second part is as expected: a Unix-style path list, with absolute Unix paths starting with / and separated by :.

I suspect that this is venv's doing, which may be lacking support for running in a Git Bash, assuming instead that it either runs on Linux/macOS/Unix in a Unix shell or on Windows in CMD/PowerShell, but not in the MSYS2 Bash that sort of emulates Unix paths but that emulation obviously cannot extend to the native Windows variant of Python.

@jingjingan
Copy link
Author

$(pwd)

hola_@DESKTOP-JO4V5P1 MINGW64 C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code (main)
$ echo $PWD
C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code
(.venv)
hola_@DESKTOP-JO4V5P1 MINGW64 C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code (main)
$ echo $(pwd)
/d/ICCLOUDY/12客户/工具/py-stat
(.venv) 

@jingjingan
Copy link
Author

Does VIRTUAL_ENV contain multi-line text?

no

@jingjingan
Copy link
Author

I checked the python add-on repo, there is a same issue reported.

I did not realize that this could be the add-on issue...

@dscho
Copy link
Member

dscho commented Aug 29, 2024

I checked the python add-on repo, there is a same issue reported.

Could you please provide the link?

And what does realpath "$PWD" "$(pwd)" report?

And could you please run this realpath command?

@jingjingan
Copy link
Author

$ realpath "$PWD" "$(pwd)"
C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code
/d/ICCLOUDY/12客户/工具/py-stat
(.venv) 

add-on issue link: microsoft/vscode-python#23382

@dscho
Copy link
Member

dscho commented Aug 30, 2024

Does $PWD still show the incorrect value after a cd "$(pwd)"?

@jingjingan
Copy link
Author

After a cd "$(pwd)", $PWD corrected.

hola_@DESKTOP-JO4V5P1 MINGW64 C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code (main)
$ realpath "$PWD" "$(pwd)"
C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code
/d/ICCLOUDY/12客户/工具/py-stat
(.venv)
hola_@DESKTOP-JO4V5P1 MINGW64 C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code (main)
$ cd "$(pwd)"
(.venv) 
hola_@DESKTOP-JO4V5P1 MINGW64 /d/ICCLOUDY/12客户/工具/py-stat (main)
$ realpath "$PWD" "$(pwd)"
/d/ICCLOUDY/12客户/工具/py-stat
/d/ICCLOUDY/12客户/工具/py-stat
(.venv) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants