{"thread":{"id":"57043","subject":"BUG: git bash / python interaction with absolute paths as environment variables in Windows","startedAt":"2021-12-07T03:01:11Z","lastAt":"2021-12-07T21:39:18Z","messageCount":4,"participants":["Leland Weathers","Eric Sunshine","Johannes Schindelin"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"443226","messageId":"CAHeq4YxktibxMBnMLwEq9Nvsgw0UqCrjW2WPcerJCM4mXKxiEw@mail.gmail.com","threadId":"57043","inReplyTo":null,"subject":"BUG: git bash / python interaction with absolute paths as environment variables in Windows","fromName":"Leland Weathers","fromEmail":"leland@lcweathers.net","sentAt":"2021-12-07T03:00:56Z","receivedAt":"2021-12-07T03:01:11Z","isPatch":false,"sender":{"key":"leland@lcweathers.net","avatar":null},"body":"Issue: Using Git Bash for Windows (2.34.1-64) and Python 3.9.9, a git\npath is incorrectly prepended to environment variables in Python code.\n\nSample Output of a simple Python script pulling an environment\nvariable - this is the expected output, where Python is given the\ncorrect environment variables for both absolute directory paths and\nnon-directory paths - OS should not matter:\n\n(gitfu-XXGUYdp7) C:\\Users\\Leland\\code\\gitfu>type foobar.py\nfrom os import environ\nfrom sys import stdout\n\nstdout.write(f'environ: {environ[\"TEST_DIR_BROKEN\"]}\\n')\nstdout.write(f'environ: {environ[\"TEST_DIR_WORKING\"]}\\n')\n(gitfu-XXGUYdp7) C:\\Users\\Leland\\code\\gitfu>echo %TEST_DIR_BROKEN%\n/foo/bar\n\n(gitfu-XXGUYdp7) C:\\Users\\Leland\\code\\gitfu>echo %TEST_DIR_WORKING%\nfoo/bar\n\n(gitfu-XXGUYdp7) C:\\Users\\Leland\\code\\gitfu>python foobar.py\nenviron: /foo/bar\nenviron: foo/bar\n\n(gitfu-XXGUYdp7) C:\\Users\\Leland\\code\\gitfu>powershell Get-Command python.exe\n\nCommandType     Name\nVersion    Source\n-----------     ----\n-------    ------\nApplication     python.exe\n3.9.915... C:\\Users\\Leland\\.virtualenvs\\gitfu-XXGUYdp7\\Scripts\\python.exe\n\n\n\n(gitfu-XXGUYdp7) C:\\Users\\Leland\\code\\gitfu>\n\n\n\n\n\nSample output when the same set of commands is run in git bash -\nabsolute path has been altered the relative path has not been:\n\n$ cat foobar.py\nfrom os import environ\nfrom sys import stdout\n\nstdout.write(f'environ: {environ[\"TEST_DIR_BROKEN\"]}\\n')\nstdout.write(f'environ: {environ[\"TEST_DIR_WORKING\"]}\\n')\nLeland@local MINGW64 ~/code/gitfu\n$ echo $TEST_DIR_BROKEN\n/foo/bar\n\nLeland@local MINGW64 ~/code/gitfu\n$ echo $TEST_DIR_WORKING\nfoo/bar\n\nLeland@local MINGW64 ~/code/gitfu\n$ python foobar.py\nenviron: C:/Users/Leland/AppData/Local/Programs/Git/foo/bar\nenviron: foo/bar\n\nLeland@local MINGW64 ~/code/gitfu\n$ which python\n/c/Users/Leland/.virtualenvs/gitfu-XXGUYdp7/Scripts/python\n\nLeland@local MINGW64 ~/code/gitfu\n$\n\n\n\n\nIs there anything else I'm missing on why the same Python script would\nread environment variables differently than what is read from Git Bash\nitself or why the exact same Python code reads the environment\nvariable correctly when run from a command prompt and not in Git Bash?\n\nIn both cases I am using the same Python virtual environment. Other\nenvironment variables (e.g. non-absolute directory paths) appear to be\nread correctly. I'm assuming that this is a git issue given the\n"},{"id":"443227","messageId":"CAPig+cQSEUguKRfCBLoDfLvH5orAX-_KZ+pZE=h4u2x3N_yNzQ@mail.gmail.com","threadId":"57043","inReplyTo":"CAHeq4YxktibxMBnMLwEq9Nvsgw0UqCrjW2WPcerJCM4mXKxiEw@mail.gmail.com","subject":"Re: BUG: git bash / python interaction with absolute paths as environment variables in Windows","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2021-12-07T03:15:59Z","receivedAt":"2021-12-07T03:16:15Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Mon, Dec 6, 2021 at 10:01 PM Leland Weathers <leland@lcweathers.net> wrote:\n> Issue: Using Git Bash for Windows (2.34.1-64) and Python 3.9.9, a git\n> path is incorrectly prepended to environment variables in Python code.\n>\n> $ echo $TEST_DIR_BROKEN\n> /foo/bar\n> $ echo $TEST_DIR_WORKING\n> foo/bar\n> $ python foobar.py\n> environ: C:/Users/Leland/AppData/Local/Programs/Git/foo/bar\n> environ: foo/bar\n>\n> Is there anything else I'm missing on why the same Python script would\n> read environment variables differently than what is read from Git Bash\n> itself or why the exact same Python code reads the environment\n> variable correctly when run from a command prompt and not in Git Bash?\n>\n> In both cases I am using the same Python virtual environment. Other\n> environment variables (e.g. non-absolute directory paths) appear to be\n> read correctly. I'm assuming that this is a git issue given the\n\nThis is probably not specific to Git, but rather a \"feature\" of MSYS2,\nwhich Git for Windows happens to employ for its Bash shell. When\ninvoking Windows commands from within MSYS2, command-line arguments\nand environment variables on the Unix side which appear to be paths\nwill be converted to Windows paths for the sake of the native Windows\nprogram (since it won't know anything about the Unix paths coming out\nof the MSYS2 environment).\n\nThis behavior is documented at [1]; in particular, see the\n\"Environment Variables\" section.\n\n[1]: https://www.msys2.org/docs/filesystem-paths/\n"},{"id":"443274","messageId":"CAHeq4YwfbcwfzxjmiWJUYxtCAn9wf9AW5kNJ8+ps8dAYOn7ZAA@mail.gmail.com","threadId":"57043","inReplyTo":"CAPig+cQSEUguKRfCBLoDfLvH5orAX-_KZ+pZE=h4u2x3N_yNzQ@mail.gmail.com","subject":"Re: BUG: git bash / python interaction with absolute paths as environment variables in Windows","fromName":"Leland Weathers","fromEmail":"leland@lcweathers.net","sentAt":"2021-12-07T13:10:35Z","receivedAt":"2021-12-07T13:10:48Z","isPatch":false,"sender":{"key":"leland@lcweathers.net","avatar":null},"body":"Eric, thanks for the pointer. Got it working as expected now.\n\nOn Mon, Dec 6, 2021 at 9:16 PM Eric Sunshine <sunshine@sunshineco.com> wrote:\n>\n> On Mon, Dec 6, 2021 at 10:01 PM Leland Weathers <leland@lcweathers.net> wrote:\n> > Issue: Using Git Bash for Windows (2.34.1-64) and Python 3.9.9, a git\n> > path is incorrectly prepended to environment variables in Python code.\n> >\n> > $ echo $TEST_DIR_BROKEN\n> > /foo/bar\n> > $ echo $TEST_DIR_WORKING\n> > foo/bar\n> > $ python foobar.py\n> > environ: C:/Users/Leland/AppData/Local/Programs/Git/foo/bar\n> > environ: foo/bar\n> >\n> > Is there anything else I'm missing on why the same Python script would\n> > read environment variables differently than what is read from Git Bash\n> > itself or why the exact same Python code reads the environment\n> > variable correctly when run from a command prompt and not in Git Bash?\n> >\n> > In both cases I am using the same Python virtual environment. Other\n> > environment variables (e.g. non-absolute directory paths) appear to be\n> > read correctly. I'm assuming that this is a git issue given the\n>\n> This is probably not specific to Git, but rather a \"feature\" of MSYS2,\n> which Git for Windows happens to employ for its Bash shell. When\n> invoking Windows commands from within MSYS2, command-line arguments\n> and environment variables on the Unix side which appear to be paths\n> will be converted to Windows paths for the sake of the native Windows\n> program (since it won't know anything about the Unix paths coming out\n> of the MSYS2 environment).\n>\n> This behavior is documented at [1]; in particular, see the\n> \"Environment Variables\" section.\n>\n> [1]: https://www.msys2.org/docs/filesystem-paths/\n"},{"id":"443374","messageId":"nycvar.QRO.7.76.6.2112072235400.90@tvgsbejvaqbjf.bet","threadId":"57043","inReplyTo":"CAHeq4YxktibxMBnMLwEq9Nvsgw0UqCrjW2WPcerJCM4mXKxiEw@mail.gmail.com","subject":"Re: BUG: git bash / python interaction with absolute paths as environment variables in Windows","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2021-12-07T21:39:12Z","receivedAt":"2021-12-07T21:39:18Z","isPatch":false,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Leland,\n\nOn Mon, 6 Dec 2021, Leland Weathers wrote:\n\n> Issue: Using Git Bash for Windows (2.34.1-64) and Python 3.9.9, a git\n> path is incorrectly prepended to environment variables in Python code.\n\nFrom Git for Windows' [Release Notes' \"Known Issues\"\nsection](https://github.com/git-for-windows/build-extra/blob/master/ReleaseNotes.md#known-issues):\n\n\t* If you specify command-line options starting with a slash,\n\t  POSIX-to-Windows path conversion will kick in converting e.g.\n\t  \"`/usr/bin/bash.exe`\" to \"`C:\\Program Files\\Git\\usr\\bin\\bash.exe`\".\n\t When that is not desired -- e.g.\n\t \"`--upload-pack=/opt/git/bin/git-upload-pack`\" or \"`-L/regex/`\" --\n\t you need to set the environment variable `MSYS_NO_PATHCONV`\n\t temporarily, like so:\n\n\t > `MSYS_NO_PATHCONV=1 git blame -L/pathconv/ msys2_path_conv.cc`\n\n\t Alternatively, you can double the first slash to avoid\n\t POSIX-to-Windows path conversion, e.g. \"`//usr/bin/bash.exe`\".\n\nThose release notes are shown by default when you install Git for Windows,\nand they are also accessible via the Start Menu as \"Git Release Notes\".\n\n> [... snip ...]\n>\n> In both cases I am using the same Python virtual environment. Other\n> environment variables (e.g. non-absolute directory paths) appear to be\n> read correctly. I'm assuming that this is a git issue given the\n\nPremature end-of-mail?\n\nCiao,\nJohannes\n"}]}