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

Refine MatchSpec validation to avoid incompatibilities with libmamba's parser #421

Merged
merged 5 commits into from
Jan 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
24 changes: 21 additions & 3 deletions conda_libmamba_solver/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,9 @@ def _specs_to_tasks_add(self, in_state: SolverInputState, out_state: SolverOutpu
continue # ignore virtual packages
installed: PackageRecord = in_state.installed.get(name)
if installed:
installed_spec_str = self._spec_to_str(installed.to_match_spec())
installed_spec_str = self._spec_to_str(
self._check_spec_compat(installed.to_match_spec())
)
else:
installed_spec_str = None
requested: MatchSpec = self._check_spec_compat(in_state.requested.get(name))
Expand Down Expand Up @@ -925,18 +927,34 @@ def _check_spec_compat(self, match_spec: Union[MatchSpec, None]) -> Union[MatchS
if match_spec is None:
return None
supported = "name", "version", "build", "channel", "subdir"
droppable = ("url",)
unsupported_but_set = []
to_drop = set()
to_keep = {}
for field in match_spec.FIELD_NAMES:
value = match_spec.get_raw_value(field)
if value and field not in supported:
unsupported_but_set.append(field)
if value:
if (
(field == "channel" and str(value) == "<unknown>")
or (field == "subdir" and "channel" in to_drop)
or field in droppable
):
# These make libmamba segfault but don't add useful info
to_drop.add(field)
elif field not in supported:
unsupported_but_set.append(field)
else:
to_keep[field] = value
if unsupported_but_set:
raise InvalidMatchSpec(
match_spec,
"Libmamba only supports a subset of the MatchSpec interface for now. "
f"You can only use {supported}, but you tried to use "
f"{tuple(unsupported_but_set)}.",
)
if to_drop:
log.debug("Dropping unsupported fields from %s: %s", match_spec, sorted(to_drop))
match_spec = MatchSpec(**to_keep)
if (
match_spec.get_raw_value("channel") == "defaults"
and context.default_channels == DEFAULT_CHANNELS
Expand Down
19 changes: 19 additions & 0 deletions news/421-matchspec-validation
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Do not raise error if an unsupported `MatchSpec` field can be safely dropped instead. (#418 via #421)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
21 changes: 20 additions & 1 deletion tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from conda.common.io import env_vars
from conda.core.prefix_data import PrefixData
from conda.models.channel import Channel
from conda.testing.integration import _get_temp_prefix, make_temp_env
from conda.testing.integration import (
_get_temp_prefix,
make_temp_env,
package_is_installed,
)
from conda.testing.integration import run_command as conda_inprocess

from .channel_testing.helpers import http_server_auth_basic # noqa: F401
Expand Down Expand Up @@ -298,3 +302,18 @@ def test_http_server_auth_token_in_defaults(http_server_auth_token):
condarc.write_text(condarc_contents)
else:
condarc.unlink()


def test_unknown_channels_do_not_crash(tmp_path):
"https://github.com/conda/conda-libmamba-solver/issues/418"
DATA = Path(__file__).parent / "data"
test_pkg = DATA / "mamba_repo" / "noarch" / "test-package-0.1-0.tar.bz2"
with make_temp_env("ca-certificates") as prefix:
# copy pkg to a new non-channel-like location without repodata around to obtain
# '<unknown>' channel and reproduce the issue
temp_pkg = Path(prefix, "test-package-0.1-0.tar.bz2")
shutil.copy(test_pkg, temp_pkg)
conda_inprocess("install", prefix, str(temp_pkg))
assert package_is_installed(prefix, "test-package")
conda_inprocess("install", prefix, "zlib")
assert package_is_installed(prefix, "zlib")
Loading