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 config file search and inclusion in header #2048

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 piptools/scripts/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def _get_default_option(option_name: str) -> Any:
src_files = click.argument(
"src_files",
nargs=-1,
is_eager=True,
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #1902 (comment). Making only src_files eager doesn't work though, as it won't pick up the config if not specified, then.

type=click.Path(exists=True, allow_dash=True),
)

Expand Down
2 changes: 1 addition & 1 deletion piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def select_config_file(src_files: tuple[str, ...]) -> Path | None:
# NOTE: input.
working_directory = Path.cwd()
src_files_as_paths = (
(working_directory / src_file).resolve() for src_file in src_files or (".",)
(working_directory / src_file).resolve() for src_file in src_files + (".",)
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain this? Obviously, there's some unspecified side effect in external logic, but I don't get it.

Copy link
Contributor Author

@chrysle chrysle Feb 15, 2024

Choose a reason for hiding this comment

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

This adds the current working directory to the search path for source files as well, as proposed in the linked issue.

)
candidate_dirs = (src if src.is_dir() else src.parent for src in src_files_as_paths)
config_file_path = next(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3635,3 +3635,13 @@ def test_stdout_should_not_be_read_when_stdin_is_not_a_plain_file(
out = runner.invoke(cli, [req_in.as_posix(), "--output-file", fifo.as_posix()])

assert out.exit_code == 0, out


def test_config_option_not_unnecessarily_added_in_output_header(runner, tmpdir_cwd):
req_in = tmpdir_cwd / "requirements.in"
req_in.touch()

out = runner.invoke(cli)

assert out.exit_code == 0, out
assert "--config" not in out.stderr
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,3 +808,16 @@ def test_select_config_file_prefers_pip_tools_toml_over_pyproject_toml(tmpdir_cw
)

assert select_config_file(()) == pip_tools_file


def test_select_config_file_with_config_file_in_different_directory(
tmpdir_cwd, make_config_file
):
config_file = make_config_file("dry-run", True, ".pip-tools.toml")

(tmpdir_cwd / "subdir").mkdir()

requirement_file = Path("subdir/requirements.in")
requirement_file.touch()

assert select_config_file((requirement_file.as_posix(),)) == config_file