git-for-windows / git

A fork of Git containing Windows-specific patches.
http://gitforwindows.org/
Other
8.28k stars 2.51k forks source link

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

Open jingjingan opened 2 weeks ago

jingjingan commented 2 weeks ago

Setup

$ 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) 
$ cmd.exe /c ver

Microsoft Windows [版本 10.0.19045.4780]
# 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)

insert your response here

Details

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

the command line prompt shows the correct dir name.

the command line prompt shows the wrong dir name.

no .

dscho commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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

jingjingan commented 2 weeks ago
$ 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 commented 2 weeks ago

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

dscho commented 2 weeks ago
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 commented 2 weeks ago

$(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 commented 2 weeks ago

Does VIRTUAL_ENV contain multi-line text?

no

jingjingan commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago
$ realpath "$PWD" "$(pwd)"
C:/Users/hola_/AppData/Local/Programs/Microsoft VS Code
/d/ICCLOUDY/12客户/工具/py-stat
(.venv) 

add-on issue link: https://github.com/microsoft/vscode-python/issues/23382

dscho commented 2 weeks ago

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

jingjingan commented 2 weeks ago

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)