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

Fix handling of default sources #232

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
35 changes: 20 additions & 15 deletions conda_lock/conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,8 @@ def run_lock(
update: Optional[List[str]] = None,
filter_categories: bool = False,
) -> None:
if environment_files == DEFAULT_FILES:
# no environment files specified => from lockfile or defaults
if len(environment_files) == 0:
if lockfile_path.exists():
lock_content = parse_conda_lock_file(lockfile_path)
# reconstruct native paths
Expand All @@ -935,13 +936,23 @@ def run_lock(
print(
f"{lockfile_path} was created from {[str(p) for p in locked_environment_files]},"
f" but some files ({[str(p) for p in missing]}) do not exist. Falling back to"
f" {[str(p) for p in environment_files]}.",
f" {[str(p) for p in DEFAULT_FILES]}.",
file=sys.stderr,
)
else:
long_ext_file = pathlib.Path("environment.yaml")
if long_ext_file.exists() and not environment_files[0].exists():
environment_files = [long_ext_file]
# always re-check in case no files from the lockfile were assigned
if len(environment_files) == 0:
# bail out if we do not encounter any default .y(a)ml files
candidates = [
p if p.exists() else p.with_suffix(".yaml") for p in DEFAULT_FILES
]
environment_files = [p for p in candidates if p.exists()]
if len(environment_files) == 0:
print(
f"No files exist matching the defaults"
f" ({[str(p.with_suffix('.y(a)ml')) for p in DEFAULT_FILES]}).",
file=sys.stderr,
)
sys.exit(1)

_conda_exe = determine_conda_executable(
conda_exe, mamba=mamba, micromamba=micromamba
Expand Down Expand Up @@ -1131,16 +1142,10 @@ def lock(
if pypi_to_conda_lookup_file:
set_lookup_location(pypi_to_conda_lookup_file)

# bail out if we do not encounter the default file if no files were passed
# we need to distinguish between unspecified files and specified but same as default
# (while keeping the CLI default to have it in the --help)
if ctx.get_parameter_source("files") == click.core.ParameterSource.DEFAULT:
candidates = list(files)
candidates += [f.with_name(f.name.replace(".yml", ".yaml")) for f in candidates]
for f in candidates:
if f.exists():
break
else:
print(ctx.get_help())
sys.exit(1)
files = []

if pdb:
sys.excepthook = _handle_exception_post_mortem
Expand Down
3 changes: 1 addition & 2 deletions tests/test_conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from flaky import flaky

from conda_lock.conda_lock import (
DEFAULT_FILES,
DEFAULT_LOCKFILE_NAME,
_add_auth_to_line,
_add_auth_to_lockfile,
Expand Down Expand Up @@ -532,7 +531,7 @@ def test_run_lock_with_locked_environment_files(
run_lock([pre_environment], conda_exe="mamba")
make_lock_files = MagicMock()
monkeypatch.setattr("conda_lock.conda_lock.make_lock_files", make_lock_files)
run_lock(DEFAULT_FILES, conda_exe=conda_exe, update=["pydantic"])
run_lock([], conda_exe=conda_exe, update=["pydantic"])
if sys.version_info < (3, 8):
# backwards compat
src_files = make_lock_files.call_args_list[0][1]["src_files"]
Expand Down