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

Log a warning if the user specifies -P and the output file is present but empty #1822

Merged
merged 8 commits into from
May 15, 2023
9 changes: 9 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,15 @@ def cli(
# Proxy with a LocalRequirementsRepository if --upgrade is not specified
# (= default invocation)
if not upgrade and os.path.exists(output_file.name):
if upgrade_install_reqs and os.path.getsize(output_file.name) == 0:
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
log.warning(
f"WARNING: the output file {output_file.name} exists but is empty. "
"Pip-tools cannot upgrade only specific packages (using -P/--upgrade-package) "
"without an existing pin file to provide constraints. "
"This often occurs if you redirect standard output to your output file, "
"as any existing content is truncated."
)

# Use a temporary repository to ensure outdated(removed) options from
# existing requirements.txt wouldn't get into the current repository.
tmp_repository = PyPIRepository(pip_args, cache_dir=cache_dir)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,27 @@ def test_upgrade_packages_option_no_existing_file(pip_conf, runner):
assert out.exit_code == 0
assert "small-fake-a==0.2" in out.stderr.splitlines()
assert "small-fake-b==0.3" in out.stderr.splitlines()
assert (
"WARNING: the output file requirements.txt exists but is empty"
not in out.stderr
)


def test_upgrade_packages_empty_target_file_warning(pip_conf, runner):
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
"""
piptools warns the user if --upgrade-package/-P is specified and the
output file exists, but is empty.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a==0.2")
with open("requirements.txt", "w") as req_txt:
req_txt.write("")

out = runner.invoke(cli, ["--no-annotate", "-P", "small-fake-a"])

assert out.exit_code == 0
assert "small-fake-a==0.2" in out.stderr.splitlines()
assert "WARNING: the output file requirements.txt exists but is empty" in out.stderr


@pytest.mark.parametrize(
Expand Down