Skip to content

fix: Quote values in bash activation with shlex#1392

Merged
baszalmstra merged 1 commit intoconda:mainfrom
mwiebe:win-git-quote
Jun 19, 2025
Merged

fix: Quote values in bash activation with shlex#1392
baszalmstra merged 1 commit intoconda:mainfrom
mwiebe:win-git-quote

Conversation

@mwiebe
Copy link
Contributor

@mwiebe mwiebe commented Jun 17, 2025

Description

On Windows, activating an environment with Git Bash fails. I experienced this issue trying to use the pixi shell-hook command that produces output like:

$ pixi shell-hook
export Path="/c/Dev/rattler/.pixi/envs/default;/c/Dev/..."
export CONDA_PREFIX="C:\Dev\rattler\.pixi\envs\default"
export PIXI_PROJECT_ROOT="C:\Dev\rattler"
export PIXI_PROJECT_NAME="rattler"

This is incorrect, because of using the ";" instead of ":" separator in the PATH and not escaping the "\" characters in the other paths that appear.

By using shlex::try_quote, these variables can be set correctly. Because setting the PATH used the primitive for setting an environment variable, that had to change to explicitly use double-quotes, so that the included ${PATH} is evaluated instead of handled literally.

Extended the tests for ShellScript to include '\', an unexpanded ${} variable, all the modes of setting the PATH, and running a script with a character that needs escaping.

After cherry-picking this change onto an older rattler commit that works with pixi, here's example output from pixi with correct behavior:

$ ./target/debug/pixi.exe shell-hook
. "C:\\Dev\\pixi\\.pixi\\envs\\default\\etc/conda/deactivate.d\\libxml2_deactivate.sh"
. "C:\\Dev\\pixi\\.pixi\\envs\\default\\etc/conda/deactivate.d\\openssl_deactivate-win.sh"
export PATH="/c/Dev/pixi/.pixi/envs/default:/mingw-w64/bin:/usr/bin:/bin:/c/Dev/pixi/.pixi/envs/default/Scripts:/c/Dev/pixi/.pixi/envs/default/bin:..."
export CONDA_PREFIX="C:\\Dev\\pixi\\.pixi\\envs\\default"
export PIXI_PROJECT_ROOT="C:\\Dev\\pixi"
export PIXI_PROJECT_VERSION=NO_VERSION_SPECIFIED
export PIXI_EXE="C:\\Dev\\pixi\\target\\debug\\pixi.exe"
export PIXI_PROJECT_MANIFEST="C:\\Dev\\pixi\\pixi.toml"
export PIXI_IN_SHELL=1
export PIXI_PROJECT_NAME=pixi
export CONDA_DEFAULT_ENV=pixi
export PIXI_ENVIRONMENT_NAME=default
export PIXI_ENVIRONMENT_PLATFORMS='win-64,linux-64,osx-arm64,linux-aarch64,osx-64'
export PIXI_PROMPT='(pixi) '
export CARGO_TARGET_DIR=target/pixi
. "C:\\Dev\\pixi\\.pixi\\envs\\default\\etc/conda/activate.d\\libxml2_activate.sh"
. "C:\\Dev\\pixi\\.pixi\\envs\\default\\etc/conda/activate.d\\openssl_activate-win.sh"
...

@mwiebe mwiebe force-pushed the win-git-quote branch 2 times, most recently from e9b3d7c to 0c2cff3 Compare June 17, 2025 05:52
@baszalmstra
Copy link
Collaborator

thanks for contributing a fix!

@mwiebe mwiebe force-pushed the win-git-quote branch 2 times, most recently from 6f09b2b to 5319a95 Compare June 18, 2025 02:32
@mwiebe mwiebe marked this pull request as ready for review June 18, 2025 05:03
@mwiebe
Copy link
Contributor Author

mwiebe commented Jun 18, 2025

I succeeded building this with pixi as a tweaked version of this patch rebased on an older rattler commit. I found everything but one thing was working - the PATH variable was set with name "Path" instead, and so the replacement PATH did not take. I made code changes I think should have worked, but it still printed the incorrect Path. I'll pick this up again later to try fixing it.

@baszalmstra
Copy link
Collaborator

baszalmstra commented Jun 18, 2025

Thank you, I appreciate you taking the time!

On Windows, activating an environment with Git Bash fails, because paths
are being set with double-quote strings: export A="C:\Windows\Path"
By using shlex::try_quote, these variables can be set correctly.

Because setting the PATH used the primitive for setting an environment
variable, that had to change to explicitly use double-quotes, so that
the included ${PATH} is evaluated instead of handled literally.

Extended the tests for ShellScript<Bash> to include '\', an unexpanded
${} variable, all the modes of setting the PATH, and running a script
with a character that needs escaping.
@mwiebe
Copy link
Contributor Author

mwiebe commented Jun 19, 2025

Got it to work, and I think this is ready to merge now! Please double check the various unwrap/error handling code, I tried to follow the patterns I saw, but would be great to have someone make sure it's following the right patterns.

@baszalmstra baszalmstra merged commit 60133cf into conda:main Jun 19, 2025
19 checks passed
This was referenced Jun 18, 2025
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 20, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 20, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 20, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 20, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 20, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 22, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
@baszalmstra baszalmstra mentioned this pull request Jun 23, 2025
This was referenced Jun 23, 2025
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 23, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 25, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 26, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jun 29, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jul 9, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
@mwiebe mwiebe deleted the win-git-quote branch July 12, 2025 23:48
fecet pushed a commit to fecet/rattler that referenced this pull request Jul 23, 2025
mwiebe added a commit to mwiebe/pixi that referenced this pull request Jul 25, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Aug 10, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
mwiebe added a commit to mwiebe/pixi that referenced this pull request Aug 16, 2025
This depends on the PR conda/rattler#1392
for the bash shell hook to work correctly on Windows.

Create a function start_winbash for running bash on Windows
that launches bash using the command:

$ bash --init-file <temp-shell-hook> -i
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

Successfully merging this pull request may close these issues.

2 participants