Skip to content
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

Add fallback to root prefix #3435

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions libmamba/src/api/clean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace mamba
auto& ctx = config.context();

config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.load();

bool clean_all = options & MAMBA_CLEAN_ALL;
Expand Down
3 changes: 3 additions & 0 deletions libmamba/src/api/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace mamba
void config_describe(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX
Expand All @@ -37,6 +38,7 @@ namespace mamba
void config_list(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX
Expand Down Expand Up @@ -68,6 +70,7 @@ namespace mamba
void config_sources(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX
Expand Down
18 changes: 15 additions & 3 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,17 @@ namespace mamba
}
else
{
bool use_fallback = config.at("use_target_prefix_fallback").value<bool>();
if (use_fallback)
bool use_target_prefix_fallback = config.at("use_target_prefix_fallback").value<bool>();
if (use_target_prefix_fallback)
{
prefix = util::get_env("CONDA_PREFIX").value_or("");
}

bool use_root_prefix_fallback = config.at("use_root_prefix_fallback").value<bool>();
if (use_root_prefix_fallback && prefix.empty())
{
prefix = root_prefix;
}
}

#ifdef _WIN32
Expand Down Expand Up @@ -1194,7 +1200,8 @@ namespace mamba
"envs_dirs",
"env_name",
"spec_file_env_name",
"use_target_prefix_fallback" })
"use_target_prefix_fallback",
"use_root_prefix_fallback" })
.set_single_op_lifetime()
.description("Path to the target prefix")
.set_post_merge_hook<fs::u8path>(
Expand All @@ -1215,6 +1222,11 @@ namespace mamba
.set_single_op_lifetime()
.description("Fallback to the current target prefix or not"));

insert(Configurable("use_root_prefix_fallback", true)
.group("Basic")
.set_single_op_lifetime()
.description("Fallback to the root prefix or not"));

insert(Configurable("target_prefix_checks", MAMBA_NO_PREFIX_CHECK)
.group("Basic")
.needs({ "target_prefix", "rc_files" })
Expand Down
1 change: 1 addition & 0 deletions libmamba/src/api/create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace mamba
auto& ctx = config.context();

config.at("use_target_prefix_fallback").set_value(false);
config.at("use_root_prefix_fallback").set_value(false);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
Expand Down
1 change: 1 addition & 0 deletions libmamba/src/api/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace mamba
void info(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
Expand Down
1 change: 1 addition & 0 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ namespace mamba

config.at("create_base").set_value(true);
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_NOT_ALLOW_MISSING_PREFIX
Expand Down
1 change: 1 addition & 0 deletions libmamba/src/api/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace mamba
void list(Configuration& config, const std::string& regex)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX
Expand Down
1 change: 1 addition & 0 deletions libmamba/src/api/remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace mamba
bool remove_all = flags & MAMBA_REMOVE_ALL;

config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set the flag to false here since it's related to environment removal and could be dangerous (so no fallback in this case).

config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_NOT_ALLOW_MISSING_PREFIX
Expand Down
1 change: 1 addition & 0 deletions libmamba/src/api/repoquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace mamba
repoquery_init(Context& ctx, Configuration& config, QueryResultFormat format, bool use_local)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX);
config.load();
Expand Down
1 change: 1 addition & 0 deletions libmamba/src/api/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ namespace mamba
auto& ctx = config.context();

config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_NOT_ALLOW_MISSING_PREFIX
Expand Down
5 changes: 5 additions & 0 deletions micromamba/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ void
set_sequence_to_rc(mamba::Configuration& config, const SequenceAddType& opt)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
Expand Down Expand Up @@ -356,6 +357,7 @@ set_config_remove_key_command(CLI::App* subcom, mamba::Configuration& config)
[&]()
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX
Expand Down Expand Up @@ -419,6 +421,7 @@ set_config_remove_command(CLI::App* subcom, mamba::Configuration& config)
[&]
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX
Expand Down Expand Up @@ -496,6 +499,7 @@ set_config_set_command(CLI::App* subcom, mamba::Configuration& config)
[&]
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX
Expand Down Expand Up @@ -541,6 +545,7 @@ set_config_get_command(CLI::App* subcom, mamba::Configuration& config)
[&]
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX
Expand Down
1 change: 1 addition & 0 deletions micromamba/src/constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void
construct(Configuration& config, const fs::u8path& prefix, bool extract_conda_pkgs, bool extract_tarball)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
Expand Down
1 change: 1 addition & 0 deletions micromamba/src/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ namespace
void set_default_config_options(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(false);
config.at("use_root_prefix_fallback").set_value(false);
config.at("target_prefix_checks").set_value(MAMBA_NO_PREFIX_CHECK);
}

Expand Down
7 changes: 4 additions & 3 deletions micromamba/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def check_create_result(res, root_prefix, target_prefix):
assert res["root_prefix"] == str(root_prefix)
assert res["target_prefix"] == str(target_prefix)
assert not res["use_target_prefix_fallback"]
assert not res["use_root_prefix_fallback"]
checks = (
helpers.MAMBA_ALLOW_EXISTING_PREFIX
| helpers.MAMBA_NOT_ALLOW_MISSING_PREFIX
Expand Down Expand Up @@ -168,7 +169,7 @@ def test_env_logging_overhead_regression(tmp_home, tmp_root_prefix, tmp_path):
@pytest.mark.parametrize("cli_env_name", (False, True))
@pytest.mark.parametrize("yaml_name", (False, True, "prefix"))
@pytest.mark.parametrize("env_var", (False, True))
@pytest.mark.parametrize("fallback", (False, True))
@pytest.mark.parametrize("current_target_prefix_fallback", (False, True))
@pytest.mark.parametrize(
"similar_non_canonical,non_canonical_position",
((False, None), (True, "append"), (True, "prepend")),
Expand All @@ -183,7 +184,7 @@ def test_target_prefix(
cli_env_name,
yaml_name,
env_var,
fallback,
current_target_prefix_fallback,
similar_non_canonical,
non_canonical_position,
):
Expand Down Expand Up @@ -243,7 +244,7 @@ def test_target_prefix(
if env_var:
os.environ["MAMBA_TARGET_PREFIX"] = str(p)

if not fallback:
if not current_target_prefix_fallback:
os.environ.pop("CONDA_PREFIX", None)
else:
os.environ["CONDA_PREFIX"] = str(p)
Expand Down
5 changes: 3 additions & 2 deletions micromamba/tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix):
infos = helpers.info()

if prefix_selection is None:
expected_name = "None"
location = "-"
# Fallback on root prefix
expected_name = "base"
location = tmp_root_prefix
elif prefix_selection == "env_var":
expected_name = name + " (active)"
location = prefix
Expand Down
64 changes: 55 additions & 9 deletions micromamba/tests/test_install.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import platform
import shutil
import subprocess
import sys
import platform
from pathlib import Path

import pytest
Expand Down Expand Up @@ -51,6 +51,7 @@ def config_tests(cls, res, root_prefix=root_prefix, target_prefix=prefix):
assert res["root_prefix"] == root_prefix
assert res["target_prefix"] == target_prefix
assert res["use_target_prefix_fallback"]
assert res["use_root_prefix_fallback"]
checks = (
helpers.MAMBA_ALLOW_EXISTING_PREFIX
| helpers.MAMBA_NOT_ALLOW_MISSING_PREFIX
Expand Down Expand Up @@ -116,7 +117,7 @@ def test_specs(self, source, file_type, existing_cache):
@pytest.mark.parametrize("cli_env_name", (False, True))
@pytest.mark.parametrize("yaml_name", (False, True, "prefix"))
@pytest.mark.parametrize("env_var", (False, True))
@pytest.mark.parametrize("fallback", (False, True))
@pytest.mark.parametrize("current_target_prefix_fallback", (False, True))
def test_target_prefix(
self,
root_prefix,
Expand All @@ -125,7 +126,7 @@ def test_target_prefix(
cli_env_name,
yaml_name,
env_var,
fallback,
current_target_prefix_fallback,
existing_cache,
):
cmd = []
Expand Down Expand Up @@ -176,22 +177,67 @@ def test_target_prefix(
if env_var:
os.environ["MAMBA_TARGET_PREFIX"] = p

if not fallback:
if not current_target_prefix_fallback:
os.environ.pop("CONDA_PREFIX")
else:
os.environ["CONDA_PREFIX"] = p

if (
(cli_prefix and cli_env_name)
or (yaml_name == "prefix")
or not (cli_prefix or cli_env_name or yaml_name or env_var or fallback)
):
if (cli_prefix and cli_env_name) or (yaml_name == "prefix"):
with pytest.raises(subprocess.CalledProcessError):
helpers.install(*cmd, "--print-config-only")
elif not (
cli_prefix or cli_env_name or yaml_name or env_var or current_target_prefix_fallback
):
# Fallback on root prefix
res = helpers.install(*cmd, "--print-config-only")
TestInstall.config_tests(res, root_prefix=r, target_prefix=r)
else:
res = helpers.install(*cmd, "--print-config-only")
TestInstall.config_tests(res, root_prefix=r, target_prefix=expected_p)

def test_target_prefix_with_no_settings(
self,
existing_cache,
):
# Specify no arg
cmd = []

# Get the actual set MAMBA_ROOT_PREFIX when setting up `TestInstall` class
os.environ["MAMBA_DEFAULT_ROOT_PREFIX"] = os.environ.pop("MAMBA_ROOT_PREFIX")
os.environ.pop("CONDA_PREFIX")

# Fallback on root prefix
res = helpers.install(*cmd, "--print-config-only")

TestInstall.config_tests(
res,
root_prefix=TestInstall.root_prefix,
target_prefix=TestInstall.root_prefix,
)

@pytest.mark.skipif(
sys.platform == "win32",
reason="MAMBA_ROOT_PREFIX is set in windows GH workflow",
)
def test_target_prefix_with_no_settings_and_no_env_var(
self,
existing_cache,
):
# Specify no arg
cmd = []

os.environ.pop("MAMBA_ROOT_PREFIX")
os.environ.pop("CONDA_PREFIX")

# Fallback on root prefix
res = helpers.install(*cmd, "--print-config-only")

TestInstall.config_tests(
res,
root_prefix=TestInstall.current_root_prefix,
target_prefix=TestInstall.current_root_prefix,
)

@pytest.mark.parametrize("cli", (False, True))
@pytest.mark.parametrize("yaml", (False, True))
@pytest.mark.parametrize("env_var", (False, True))
Expand Down
11 changes: 7 additions & 4 deletions micromamba/tests/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def remove_config_common_assertions(res, root_prefix, target_prefix):
assert res["root_prefix"] == str(root_prefix)
assert res["target_prefix"] == str(target_prefix)
assert res["use_target_prefix_fallback"]
assert not res["use_root_prefix_fallback"]
checks = (
helpers.MAMBA_ALLOW_EXISTING_PREFIX
| helpers.MAMBA_NOT_ALLOW_MISSING_PREFIX
Expand All @@ -205,7 +206,7 @@ def test_remove_config_specs(tmp_home, tmp_root_prefix, tmp_prefix):
@pytest.mark.parametrize("cli_prefix", (False, True))
@pytest.mark.parametrize("cli_env_name", (False, True))
@pytest.mark.parametrize("env_var", (False, True))
@pytest.mark.parametrize("fallback", (False, True))
@pytest.mark.parametrize("current_target_prefix_fallback", (False, True))
def test_remove_config_target_prefix(
tmp_home,
tmp_root_prefix,
Expand All @@ -216,7 +217,7 @@ def test_remove_config_target_prefix(
cli_prefix,
cli_env_name,
env_var,
fallback,
current_target_prefix_fallback,
):
(tmp_root_prefix / "conda-meta").mkdir(parents=True, exist_ok=True)

Expand Down Expand Up @@ -246,12 +247,14 @@ def test_remove_config_target_prefix(
if env_var:
os.environ["MAMBA_TARGET_PREFIX"] = p

if not fallback:
if not current_target_prefix_fallback:
os.environ.pop("CONDA_PREFIX")
else:
os.environ["CONDA_PREFIX"] = p

if (cli_prefix and cli_env_name) or not (cli_prefix or cli_env_name or env_var or fallback):
if (cli_prefix and cli_env_name) or not (
cli_prefix or cli_env_name or env_var or current_target_prefix_fallback
):
with pytest.raises(subprocess.CalledProcessError):
helpers.remove(*cmd, "--print-config-only")
else:
Expand Down
1 change: 1 addition & 0 deletions micromamba/tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def test_activate_target_prefix_checks(tmp_home, tmp_root_prefix):
res = helpers.shell("activate", "-p", tmp_root_prefix, "--print-config-only")
assert res["target_prefix_checks"] == helpers.MAMBA_NO_PREFIX_CHECK
assert not res["use_target_prefix_fallback"]
assert not res["use_root_prefix_fallback"]


@pytest.mark.parametrize("shell_type", ["bash", "powershell", "cmd.exe"])
Expand Down
Loading
Loading