Unify CONDA_ENVS_PATH, CONDA_ENVS_DIRS (#3855)

Co-authored-by: Julien Jerphanion <git@jjerphan.xyz>
This commit is contained in:
Burt Holzman 2025-05-14 05:28:49 -05:00 committed by GitHub
parent 3111778478
commit da0607b039
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 15 deletions

View File

@ -1007,18 +1007,20 @@ namespace mamba
{
// Prepend all the directories in the environment variable `CONDA_ENVS_PATH`.
auto conda_envs_path = util::get_env("CONDA_ENVS_PATH");
auto conda_envs_dirs = util::get_env("CONDA_ENVS_DIRS");
if (conda_envs_path)
if (conda_envs_path && conda_envs_dirs)
{
if (util::get_env("CONDA_ENVS_DIRS"))
{
const auto message = "The `CONDA_ENVS_DIRS` and `CONDA_ENVS_PATH` environment variables are both set, but only one must be declared. We recommend setting `CONDA_ENVS_DIRS` only. Aborting.";
throw mamba_error(message, mamba_error_code::incorrect_usage);
}
const auto message = "The `CONDA_ENVS_DIRS` and `CONDA_ENVS_PATH` environment variables are both set, but only one must be declared. We recommend setting `CONDA_ENVS_DIRS` only. Aborting.";
throw mamba_error(message, mamba_error_code::incorrect_usage);
}
auto envs_path = conda_envs_dirs.value_or(conda_envs_path.value_or(""));
if (!envs_path.empty())
{
auto paths_separator = util::pathsep();
auto paths = util::split(conda_envs_path.value(), paths_separator);
auto paths = util::split(envs_path, paths_separator);
dirs.reserve(dirs.size() + paths.size());
dirs.insert(dirs.begin(), paths.rbegin(), paths.rend());
@ -1353,10 +1355,11 @@ namespace mamba
{ return detail::env_name_hook(*this, value); })
.description("Name of the target prefix"));
// don't use set_env_var_names for CONDA_ENVS_DIRS, since it is a path-sep delimited
// list rather than a YAML list
insert(Configurable("envs_dirs", &m_context.envs_dirs)
.group("Basic")
.set_rc_configurable(RCConfigLevel::kHomeDir)
.set_env_var_names({ "CONDA_ENVS_DIRS" })
.needs({ "root_prefix" })
.set_post_merge_hook<decltype(m_context.envs_dirs)>(
[this](decltype(m_context.envs_dirs)& value)

View File

@ -879,12 +879,15 @@ def test_activate_path(tmp_empty_env, tmp_env_name, interpreter, tmp_path):
assert any([str(tmp_empty_env) in p for p in dict_res.values()])
@pytest.mark.parametrize("conda_envs_x", ["CONDA_ENVS_DIRS", "CONDA_ENVS_PATH"])
@pytest.mark.parametrize("interpreter", get_interpreters())
def test_activate_envs_dirs(tmp_root_prefix: Path, interpreter, tmp_path: Path):
def test_activate_envs_dirs(
tmp_root_prefix: Path, interpreter, tmp_path: Path, conda_envs_x, monkeypatch
):
"""Activate an environment as the non leading entry in ``envs_dirs``."""
env_name = "myenv"
helpers.create("-p", tmp_path / env_name, "--offline", "--no-rc", no_dry_run=True)
os.environ["CONDA_ENVS_DIRS"] = f"{Path('/noperm')},{tmp_path}"
monkeypatch.setenv(conda_envs_x, f"{Path('/noperm')}{os.pathsep}{tmp_path}")
res = helpers.shell("activate", env_name, "-s", interpreter)
dict_res = env_to_dict(res, interpreter)
assert any([env_name in p for p in dict_res.values()])

View File

@ -698,13 +698,14 @@ def test_create_conda_envs_dirs_and_path(tmp_root_prefix, monkeypatch):
)
def test_create_envs_dirs(tmp_root_prefix: Path, tmp_path: Path, monkeypatch):
@pytest.mark.parametrize("conda_envs_x", ("CONDA_ENVS_DIRS", "CONDA_ENVS_PATH"))
def test_create_envs_dirs(tmp_root_prefix: Path, tmp_path: Path, conda_envs_x, monkeypatch):
"""Create an environment when the first env dir is not writable."""
noperm_root_dir = Path(tmp_path / "noperm")
noperm_envs_dir = noperm_root_dir / "envs"
monkeypatch.setenv("CONDA_ENVS_DIRS", f"{noperm_envs_dir},{tmp_path}")
monkeypatch.setenv(conda_envs_x, f"{noperm_envs_dir}{os.pathsep}{tmp_path}")
env_name = "myenv"
os.makedirs(noperm_root_dir, exist_ok=True)
@ -750,15 +751,16 @@ def test_create_envs_dirs(tmp_root_prefix: Path, tmp_path: Path, monkeypatch):
assert (tmp_path / env_name / "conda-meta" / "history").exists()
@pytest.mark.parametrize("conda_envs_x", ["CONDA_ENVS_DIRS", "CONDA_ENVS_PATH"])
@pytest.mark.parametrize("envs_dirs_source", ("condarc", "env_var"))
def test_mkdir_envs_dirs(tmp_home, tmp_root_prefix, tmp_path, monkeypatch, envs_dirs_source):
def test_mkdir_envs_dirs(tmp_path, tmp_home, monkeypatch, conda_envs_x, envs_dirs_source):
"""Test that an env dir is created if it does not exist already"""
envs_dir = tmp_path / "user_provided_envdir" / "envs"
with open(tmp_home / ".condarc", "w+") as f:
if envs_dirs_source == "env_var":
monkeypatch.setenv("CONDA_ENVS_DIRS", str(envs_dir))
monkeypatch.setenv(conda_envs_x, str(envs_dir))
else:
f.write(f"envs_dirs: [{str(envs_dir)}]")