fix: Adapt root prefix' precedence for CONDA_ENVS_PATH (#3852)

This commit is contained in:
Burt Holzman 2025-03-07 03:57:11 -06:00 committed by GitHub
parent b6b1143b1b
commit 902fd1990f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 13 deletions

View File

@ -1005,24 +1005,16 @@ namespace mamba
void envs_dirs_hook(const Context& context, std::vector<fs::u8path>& dirs)
{
// Check that "root_prefix/envs" is already in the dirs,
// and append if not - to match `conda`
fs::u8path default_env_dir = context.prefix_params.root_prefix / "envs";
if (std::find(dirs.begin(), dirs.end(), default_env_dir) == dirs.end())
{
dirs.push_back(default_env_dir);
}
// Also add all the directories in the environment variable `CONDA_ENVS_PATH`.
// Prepend all the directories in the environment variable `CONDA_ENVS_PATH`.
auto conda_envs_path = util::get_env("CONDA_ENVS_PATH");
if (conda_envs_path)
{
auto paths_separator = util::pathsep();
auto paths = util::split(conda_envs_path.value(), paths_separator);
for (auto& p : paths)
{
dirs.push_back(fs::u8path(p));
for (auto it = paths.rbegin(); it != paths.rend(); ++it)
{ // prepend conda_envs_path to dirs while maintaining order
dirs.insert(dirs.begin(), fs::u8path(*it));
}
}
@ -1036,6 +1028,14 @@ namespace mamba
throw std::runtime_error("Aborting.");
}
}
// Check that "root_prefix/envs" is already in the dirs,
// and append if not - to match `conda`
fs::u8path default_env_dir = context.prefix_params.root_prefix / "envs";
if (std::find(dirs.begin(), dirs.end(), default_env_dir) == dirs.end())
{
dirs.push_back(default_env_dir);
}
}
std::vector<fs::u8path> fallback_pkgs_dirs_hook(const Context& context)

View File

@ -757,6 +757,7 @@ def test_env_dir_idempotence(tmp_path, tmp_home, tmp_root_prefix):
assert Path(condarc_envs_dirs / env_name).exists()
@pytest.mark.parametrize("conda_envs_x", ("CONDA_ENVS_DIRS", "CONDA_ENVS_PATH"))
@pytest.mark.parametrize("set_in_conda_envs_dirs", (False, True))
@pytest.mark.parametrize("set_in_condarc", (False, True))
@pytest.mark.parametrize("cli_root_prefix", (False, True))
@ -765,6 +766,7 @@ def test_root_prefix_precedence(
tmp_path,
tmp_home,
monkeypatch,
conda_envs_x,
set_in_condarc,
set_in_conda_envs_dirs,
cli_root_prefix,
@ -794,7 +796,7 @@ def test_root_prefix_precedence(
env_name = "foo"
monkeypatch.setenv("MAMBA_ROOT_PREFIX", str(mamba_root_prefix))
if set_in_conda_envs_dirs:
monkeypatch.setenv("CONDA_ENVS_DIRS", str(conda_envs_dirs))
monkeypatch.setenv(conda_envs_x, str(conda_envs_dirs))
with open(tmp_home / ".condarc", "w+") as f:
if set_in_condarc: