From 6d61b0fe47bf91125c00c804cdba50698a0c52a7 Mon Sep 17 00:00:00 2001 From: jaimergp Date: Wed, 6 Dec 2023 08:49:43 +0100 Subject: [PATCH] Use index in offline mode when called from conda-build (#395) --- conda_libmamba_solver/index.py | 32 ++++++++++++++++--- conda_libmamba_solver/solver.py | 4 +-- news/395-offline-conda-build | 20 ++++++++++++ .../conda_build_recipes/stackvana/meta.yaml | 2 +- 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 news/395-offline-conda-build diff --git a/conda_libmamba_solver/index.py b/conda_libmamba_solver/index.py index 4bca3fb9..bf5418a4 100644 --- a/conda_libmamba_solver/index.py +++ b/conda_libmamba_solver/index.py @@ -81,15 +81,17 @@ from typing import Iterable import libmambapy as api +from conda import __version__ as conda_version from conda.base.constants import REPODATA_FN -from conda.base.context import context -from conda.common.io import DummyExecutor, ThreadLimitedThreadPoolExecutor +from conda.base.context import context, reset_context +from conda.common.io import DummyExecutor, ThreadLimitedThreadPoolExecutor, env_var from conda.common.serialize import json_dump, json_load from conda.common.url import percent_decode, remove_auth, split_anaconda_token from conda.core.subdir_data import SubdirData from conda.models.channel import Channel from conda.models.match_spec import MatchSpec from conda.models.records import PackageRecord +from conda.models.version import VersionOrder from .mamba_utils import set_channel_priorities from .state import IndexHelper @@ -407,5 +409,27 @@ def _process_query_result( return result -# for conda-build -_CachedLibMambaIndexHelper = lru_cache(maxsize=None)(LibMambaIndexHelper) +@lru_cache(maxsize=None) +class _LibMambaIndexForCondaBuild(LibMambaIndexHelper): + """ + See https://github.com/conda/conda-libmamba-solver/issues/386 + + conda-build needs to operate offline so the index doesn't get updated + accidentally during long build phases. However, this is only guaranteed + to work if https://github.com/conda/conda/pull/13357 is applied. Otherwise + the condarc configuration might be ignored, resulting in bad index configuration + and missing packages anyway. + """ + + def __init__(self, *args, **kwargs): + if VersionOrder(conda_version) <= VersionOrder("23.10.0"): + log.warning( + "conda-build requires conda >=23.11.0 for offline index support. " + "Falling back to online index. This might result in KeyError messages, " + "specially if the remote repodata is updated during the build phase. " + "See https://github.com/conda/conda-libmamba-solver/issues/386." + ) + super().__init__(*args, **kwargs) + else: + with env_var("CONDA_OFFLINE", "true", callback=reset_context): + super().__init__(*args, **kwargs) diff --git a/conda_libmamba_solver/solver.py b/conda_libmamba_solver/solver.py index f5919445..37b1f460 100644 --- a/conda_libmamba_solver/solver.py +++ b/conda_libmamba_solver/solver.py @@ -50,7 +50,7 @@ from . import __version__ from .exceptions import LibMambaUnsatisfiableError -from .index import LibMambaIndexHelper, _CachedLibMambaIndexHelper +from .index import LibMambaIndexHelper, _LibMambaIndexForCondaBuild from .mamba_utils import init_api_context, mamba_version from .state import SolverInputState, SolverOutputState from .utils import is_channel_available @@ -177,7 +177,7 @@ def solve_final_state( rec.channel: None for rec in self._index if rec.channel.scheme == "file" } # Cache indices for conda-build, it gets heavy otherwise - IndexHelper = _CachedLibMambaIndexHelper + IndexHelper = _LibMambaIndexForCondaBuild else: IndexHelper = LibMambaIndexHelper conda_bld_channels = () diff --git a/news/395-offline-conda-build b/news/395-offline-conda-build new file mode 100644 index 00000000..f54e5c16 --- /dev/null +++ b/news/395-offline-conda-build @@ -0,0 +1,20 @@ +### Enhancements + +* + +### Bug fixes + +* Instantiate `IndexHelper` in offline mode for compatibility with conda-build. Otherwise + the index can get out of sync during long build processes. (#386 via #395) + +### Deprecations + +* + +### Docs + +* + +### Other + +* diff --git a/tests/data/conda_build_recipes/stackvana/meta.yaml b/tests/data/conda_build_recipes/stackvana/meta.yaml index 2991ce80..b062368a 100644 --- a/tests/data/conda_build_recipes/stackvana/meta.yaml +++ b/tests/data/conda_build_recipes/stackvana/meta.yaml @@ -1,4 +1,4 @@ -{% set name = "stackvana-core" %} +{% set name = "stackvana-split" %} {% set version = "0.2021.43" %} {% set eups_product = "lsst_distrib" %}