Skip to content

fix: Create directories from envs_dirs if they do not exist #3796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,18 @@ namespace mamba
{
for (const auto& dir : dirs)
{
if (!fs::exists(dir))
{
try
{
fs::create_directories(dir);
}
catch (const fs::filesystem_error& e)
{
LOG_WARNING << "Error creating directory " << dir << ": " << e.what()
<< std::endl;
}
}
const auto candidate = dir / name;
if (mamba::path::is_writable(candidate))
{
Expand Down
23 changes: 19 additions & 4 deletions micromamba/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ def test_target_prefix(
else:
root_prefix = Path(os.environ["MAMBA_ROOT_PREFIX"])

# TODO: Remove this call to `os.makedirs` once
# https://github.com/mamba-org/mamba/issues/3790 is fixed
if root_prefix_env_exists:
os.makedirs(Path(os.environ["MAMBA_ROOT_PREFIX"]) / "envs", exist_ok=True)

Expand Down Expand Up @@ -670,6 +668,25 @@ def test_create_envs_dirs(tmp_root_prefix: Path, tmp_path: Path):
assert (tmp_path / env_name / "conda-meta" / "history").exists()


@pytest.mark.parametrize("envs_dirs_source", ("condarc", "env_var"))
def test_mkdir_envs_dirs(tmp_path, tmp_home, monkeypatch, 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))
else:
f.write(f"envs_dirs: [{str(envs_dir)}]")

assert not envs_dir.exists() # directory doesn't exist yet

helpers.create("-n", "bar", "--rc-file", tmp_home / ".condarc", no_rc=False)

assert envs_dir.exists()


@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))
Expand Down Expand Up @@ -713,8 +730,6 @@ def test_root_prefix_precedence(
if set_in_condarc:
f.write(f"envs_dirs: [{str(condarc_envs_dirs)}]")

# TODO: Remove this call to `os.makedirs` once
# https://github.com/mamba-org/mamba/issues/3790 is fixed
for envs_folder in (condarc_envs_dirs, conda_envs_dirs, cli_provided_root, mamba_root_prefix):
os.makedirs(envs_folder, exist_ok=True)

Expand Down
Loading