From 8ffd12820f97c003f0f418e7b5d5eac6e82aee9b Mon Sep 17 00:00:00 2001 From: Hind Montassif Date: Mon, 7 Oct 2024 10:52:35 +0200 Subject: [PATCH 1/5] Add dir to path only if it exists --- libmamba/data/Mamba.psm1 | 5 ++++- libmamba/data/mamba.csh | 8 ++------ libmamba/data/mamba.fish | 5 ++++- libmamba/data/mamba.sh | 5 ++++- libmamba/data/mamba.xsh | 5 ++++- libmamba/src/core/shell_init.cpp | 4 +++- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/libmamba/data/Mamba.psm1 b/libmamba/data/Mamba.psm1 index 7dbe5f6e18..36dfa5ca46 100644 --- a/libmamba/data/Mamba.psm1 +++ b/libmamba/data/Mamba.psm1 @@ -178,7 +178,10 @@ Register-ArgumentCompleter -Native -CommandName $__mamba_name -ScriptBlock $Mamb ## EXPORTS ################################################################### if ($null -eq $Env:CONDA_SHLVL) { - $Env:PATH = "$Env:MAMBA_ROOT_PREFIX\condabin;" + $Env:PATH + $condabinPath = "$Env:MAMBA_ROOT_PREFIX\condabin" + if (Test-Path -Path $condabinPath) { + $Env:PATH = "$condabinPath;" + $Env:PATH + } } Export-ModuleMember ` diff --git a/libmamba/data/mamba.csh b/libmamba/data/mamba.csh index 04fd856625..472a13c1a8 100644 --- a/libmamba/data/mamba.csh +++ b/libmamba/data/mamba.csh @@ -48,16 +48,12 @@ if ("$__exe_name" == "micromamba") then else if ("$__exe_name" == "mamba") then alias micromamba __mamba_wrap else - echo "Error unknow MAMBA_EXE: \"$MAMBA_EXE\", filename must be mamba or micromamba" >&2 + echo "Error unknown MAMBA_EXE: \"$MAMBA_EXE\", filename must be mamba or micromamba" >&2 endif if (! $?CONDA_SHLVL) then setenv CONDA_SHLVL 0 - # In dev-mode MAMBA_EXE is python.exe and on Windows - # it is in a different relative location to condabin. - if ($?_CE_CONDA && $?WINDIR) then - setenv PATH "${MAMBA_ROOT_PREFIX}/condabin:${PATH}" - else + if ( -d "$MAMBA_ROOT_PREFIX/condabin" ) then setenv PATH "${MAMBA_ROOT_PREFIX}/condabin:${PATH}" endif diff --git a/libmamba/data/mamba.fish b/libmamba/data/mamba.fish index d6a8861c5c..3c720e1103 100644 --- a/libmamba/data/mamba.fish +++ b/libmamba/data/mamba.fish @@ -1,6 +1,9 @@ if not set -q MAMBA_SHLVL set -gx MAMBA_SHLVL "0" - fish_add_path --move $MAMBA_ROOT_PREFIX/condabin + set MAMBA_PATH "$MAMBA_ROOT_PREFIX/condabin" + if test -d $MAMBA_PATH + fish_add_path --move $MAMBA_PATH + end end if not set -q MAMBA_NO_PROMPT diff --git a/libmamba/data/mamba.sh b/libmamba/data/mamba.sh index 502a2e857c..9ea21ddff6 100644 --- a/libmamba/data/mamba.sh +++ b/libmamba/data/mamba.sh @@ -62,7 +62,10 @@ fi if [ -z "${CONDA_SHLVL+x}" ]; then \export CONDA_SHLVL=0 - \export PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}" + # Check if the directory exists before adding it to PATH + if [ -d "${MAMBA_ROOT_PREFIX}/condabin" ]; then + \export PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}" + fi # We're not allowing PS1 to be unbound. It must at least be set. # However, we're not exporting it, which can cause problems when starting a second shell diff --git a/libmamba/data/mamba.xsh b/libmamba/data/mamba.xsh index 31775802f6..410a6836ce 100644 --- a/libmamba/data/mamba.xsh +++ b/libmamba/data/mamba.xsh @@ -86,7 +86,10 @@ if 'CONDA_SHLVL' not in ${...}: $CONDA_SHLVL = '0' import os as _os import sys as _sys - _sys.path.insert(0, _os.path.join($MAMBA_ROOT_PREFIX, "condabin")) + + condabin_path = _os.path.join($MAMBA_ROOT_PREFIX, "condabin") + if _os.path.exists(condabin_path): + _sys.path.insert(0, condabin_path) del _os, _sys diff --git a/libmamba/src/core/shell_init.cpp b/libmamba/src/core/shell_init.cpp index d4d6744d92..cf98b5bf6a 100644 --- a/libmamba/src/core/shell_init.cpp +++ b/libmamba/src/core/shell_init.cpp @@ -430,7 +430,9 @@ namespace mamba #add condabin when base env if $env.MAMBA_SHLVL? == null { $env.MAMBA_SHLVL = 0 - $env.PATH = ($env.PATH | prepend $"($env.MAMBA_ROOT_PREFIX)/condabin") + if (test -d "$env.MAMBA_ROOT_PREFIX/condabin") { + $env.PATH = ($env.PATH | prepend $"($env.MAMBA_ROOT_PREFIX)/condabin") + } } #ask mamba how to setup the environment and set the environment (^($env.MAMBA_EXE) shell activate --shell nu $name From f2d96c1ff1403c8e7df9cc568bc558cf65d93693 Mon Sep 17 00:00:00 2001 From: Hind Montassif Date: Mon, 7 Oct 2024 15:02:25 +0200 Subject: [PATCH 2/5] Adapt tests --- micromamba/tests/test_activation.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/micromamba/tests/test_activation.py b/micromamba/tests/test_activation.py index bbf5b8c9b8..2fbedeab49 100644 --- a/micromamba/tests/test_activation.py +++ b/micromamba/tests/test_activation.py @@ -585,7 +585,8 @@ def call(s): ] + evars stdout, stderr = call(s) res = env_to_dict(stdout) - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + if (tmp_root_prefix / "condabin").is_dir(): + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "bin", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "xyz", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "envs" / "abc", res["PATH"]) @@ -597,7 +598,8 @@ def call(s): ] + evars stdout, stderr = call(s) res = env_to_dict(stdout) - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + if (tmp_root_prefix / "condabin").is_dir(): + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "bin", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "xyz", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "abc", res["PATH"]) @@ -609,18 +611,21 @@ def call(s): ] + evars stdout, stderr = call(s) res = env_to_dict(stdout) - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + if (tmp_root_prefix / "condabin").is_dir(): + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "bin", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "xyz", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "abc", res["PATH"]) stdout, stderr = call(evars) res = env_to_dict(stdout) - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + if (tmp_root_prefix / "condabin").is_dir(): + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) stdout, stderr = call([f"{mamba_name} deactivate"] + evars) res = env_to_dict(stdout) - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + if (tmp_root_prefix / "condabin").is_dir(): + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "bin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "envs" / "xyz", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "envs" / "abc", res["PATH"]) From a5ccad18ded9bf7dc0a8f3ce40ab0f3974994ede Mon Sep 17 00:00:00 2001 From: Hind Montassif Date: Mon, 7 Oct 2024 16:09:51 +0200 Subject: [PATCH 3/5] Revert "Add dir to path only if it exists" This reverts commit 8ffd12820f97c003f0f418e7b5d5eac6e82aee9b. --- libmamba/data/Mamba.psm1 | 5 +---- libmamba/data/mamba.csh | 8 ++++++-- libmamba/data/mamba.fish | 5 +---- libmamba/data/mamba.sh | 5 +---- libmamba/data/mamba.xsh | 5 +---- libmamba/src/core/shell_init.cpp | 4 +--- 6 files changed, 11 insertions(+), 21 deletions(-) diff --git a/libmamba/data/Mamba.psm1 b/libmamba/data/Mamba.psm1 index 36dfa5ca46..7dbe5f6e18 100644 --- a/libmamba/data/Mamba.psm1 +++ b/libmamba/data/Mamba.psm1 @@ -178,10 +178,7 @@ Register-ArgumentCompleter -Native -CommandName $__mamba_name -ScriptBlock $Mamb ## EXPORTS ################################################################### if ($null -eq $Env:CONDA_SHLVL) { - $condabinPath = "$Env:MAMBA_ROOT_PREFIX\condabin" - if (Test-Path -Path $condabinPath) { - $Env:PATH = "$condabinPath;" + $Env:PATH - } + $Env:PATH = "$Env:MAMBA_ROOT_PREFIX\condabin;" + $Env:PATH } Export-ModuleMember ` diff --git a/libmamba/data/mamba.csh b/libmamba/data/mamba.csh index 472a13c1a8..04fd856625 100644 --- a/libmamba/data/mamba.csh +++ b/libmamba/data/mamba.csh @@ -48,12 +48,16 @@ if ("$__exe_name" == "micromamba") then else if ("$__exe_name" == "mamba") then alias micromamba __mamba_wrap else - echo "Error unknown MAMBA_EXE: \"$MAMBA_EXE\", filename must be mamba or micromamba" >&2 + echo "Error unknow MAMBA_EXE: \"$MAMBA_EXE\", filename must be mamba or micromamba" >&2 endif if (! $?CONDA_SHLVL) then setenv CONDA_SHLVL 0 - if ( -d "$MAMBA_ROOT_PREFIX/condabin" ) then + # In dev-mode MAMBA_EXE is python.exe and on Windows + # it is in a different relative location to condabin. + if ($?_CE_CONDA && $?WINDIR) then + setenv PATH "${MAMBA_ROOT_PREFIX}/condabin:${PATH}" + else setenv PATH "${MAMBA_ROOT_PREFIX}/condabin:${PATH}" endif diff --git a/libmamba/data/mamba.fish b/libmamba/data/mamba.fish index 3c720e1103..d6a8861c5c 100644 --- a/libmamba/data/mamba.fish +++ b/libmamba/data/mamba.fish @@ -1,9 +1,6 @@ if not set -q MAMBA_SHLVL set -gx MAMBA_SHLVL "0" - set MAMBA_PATH "$MAMBA_ROOT_PREFIX/condabin" - if test -d $MAMBA_PATH - fish_add_path --move $MAMBA_PATH - end + fish_add_path --move $MAMBA_ROOT_PREFIX/condabin end if not set -q MAMBA_NO_PROMPT diff --git a/libmamba/data/mamba.sh b/libmamba/data/mamba.sh index 9ea21ddff6..502a2e857c 100644 --- a/libmamba/data/mamba.sh +++ b/libmamba/data/mamba.sh @@ -62,10 +62,7 @@ fi if [ -z "${CONDA_SHLVL+x}" ]; then \export CONDA_SHLVL=0 - # Check if the directory exists before adding it to PATH - if [ -d "${MAMBA_ROOT_PREFIX}/condabin" ]; then - \export PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}" - fi + \export PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}" # We're not allowing PS1 to be unbound. It must at least be set. # However, we're not exporting it, which can cause problems when starting a second shell diff --git a/libmamba/data/mamba.xsh b/libmamba/data/mamba.xsh index 410a6836ce..31775802f6 100644 --- a/libmamba/data/mamba.xsh +++ b/libmamba/data/mamba.xsh @@ -86,10 +86,7 @@ if 'CONDA_SHLVL' not in ${...}: $CONDA_SHLVL = '0' import os as _os import sys as _sys - - condabin_path = _os.path.join($MAMBA_ROOT_PREFIX, "condabin") - if _os.path.exists(condabin_path): - _sys.path.insert(0, condabin_path) + _sys.path.insert(0, _os.path.join($MAMBA_ROOT_PREFIX, "condabin")) del _os, _sys diff --git a/libmamba/src/core/shell_init.cpp b/libmamba/src/core/shell_init.cpp index cf98b5bf6a..d4d6744d92 100644 --- a/libmamba/src/core/shell_init.cpp +++ b/libmamba/src/core/shell_init.cpp @@ -430,9 +430,7 @@ namespace mamba #add condabin when base env if $env.MAMBA_SHLVL? == null { $env.MAMBA_SHLVL = 0 - if (test -d "$env.MAMBA_ROOT_PREFIX/condabin") { - $env.PATH = ($env.PATH | prepend $"($env.MAMBA_ROOT_PREFIX)/condabin") - } + $env.PATH = ($env.PATH | prepend $"($env.MAMBA_ROOT_PREFIX)/condabin") } #ask mamba how to setup the environment and set the environment (^($env.MAMBA_EXE) shell activate --shell nu $name From 9cdb953776aa3da3081c2656df6e01d6e0c43283 Mon Sep 17 00:00:00 2001 From: Hind Montassif Date: Mon, 7 Oct 2024 16:13:18 +0200 Subject: [PATCH 4/5] Revert "Adapt tests" This reverts commit f2d96c1ff1403c8e7df9cc568bc558cf65d93693. --- micromamba/tests/test_activation.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/micromamba/tests/test_activation.py b/micromamba/tests/test_activation.py index 2fbedeab49..bbf5b8c9b8 100644 --- a/micromamba/tests/test_activation.py +++ b/micromamba/tests/test_activation.py @@ -585,8 +585,7 @@ def call(s): ] + evars stdout, stderr = call(s) res = env_to_dict(stdout) - if (tmp_root_prefix / "condabin").is_dir(): - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "bin", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "xyz", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "envs" / "abc", res["PATH"]) @@ -598,8 +597,7 @@ def call(s): ] + evars stdout, stderr = call(s) res = env_to_dict(stdout) - if (tmp_root_prefix / "condabin").is_dir(): - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "bin", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "xyz", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "abc", res["PATH"]) @@ -611,21 +609,18 @@ def call(s): ] + evars stdout, stderr = call(s) res = env_to_dict(stdout) - if (tmp_root_prefix / "condabin").is_dir(): - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "bin", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "xyz", res["PATH"]) assert find_path_in_str(tmp_root_prefix / "envs" / "abc", res["PATH"]) stdout, stderr = call(evars) res = env_to_dict(stdout) - if (tmp_root_prefix / "condabin").is_dir(): - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) stdout, stderr = call([f"{mamba_name} deactivate"] + evars) res = env_to_dict(stdout) - if (tmp_root_prefix / "condabin").is_dir(): - assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) + assert find_path_in_str(tmp_root_prefix / "condabin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "bin", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "envs" / "xyz", res["PATH"]) assert not find_path_in_str(tmp_root_prefix / "envs" / "abc", res["PATH"]) From 878c67c316d77a0b11ce39aa9bf38b6900d4a2e7 Mon Sep 17 00:00:00 2001 From: Hind Montassif Date: Mon, 7 Oct 2024 16:13:39 +0200 Subject: [PATCH 5/5] Modify shell_init only first --- libmamba/src/core/shell_init.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libmamba/src/core/shell_init.cpp b/libmamba/src/core/shell_init.cpp index d4d6744d92..cf98b5bf6a 100644 --- a/libmamba/src/core/shell_init.cpp +++ b/libmamba/src/core/shell_init.cpp @@ -430,7 +430,9 @@ namespace mamba #add condabin when base env if $env.MAMBA_SHLVL? == null { $env.MAMBA_SHLVL = 0 - $env.PATH = ($env.PATH | prepend $"($env.MAMBA_ROOT_PREFIX)/condabin") + if (test -d "$env.MAMBA_ROOT_PREFIX/condabin") { + $env.PATH = ($env.PATH | prepend $"($env.MAMBA_ROOT_PREFIX)/condabin") + } } #ask mamba how to setup the environment and set the environment (^($env.MAMBA_EXE) shell activate --shell nu $name