Skip to content

Commit

Permalink
Construct relative req lines to match what pip install understands
Browse files Browse the repository at this point in the history
Based on findings at #1329 (comment)
  • Loading branch information
AndydeCleyre committed Oct 3, 2022
1 parent 6468e37 commit aa3ee0f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@ def format_requirement(
else:
fragment = fragment_string(ireq)
extras = f"[{','.join(sorted(ireq.extras))}]" if ireq.extras else ""
delimiter = "#" if extras and not fragment else ""
# pip install needs different relpath formats, depending on extras and fragments:
# https://github.com/jazzband/pip-tools/pull/1329#issuecomment-1056409415
if fragment or not extras:
prefix = "file:"
delimiter = ""
else:
prefix = ""
delimiter = "#"
if not from_dir:
line = (
f"-e {path_to_url(ireq.local_file_path)}{fragment}{delimiter}{extras}"
Expand All @@ -163,9 +170,9 @@ def format_requirement(
)
else:
try:
path_url = "file:" + os.path.relpath(
ireq.local_file_path, from_dir
).replace(os.path.sep, "/")
relpath = os.path.relpath(ireq.local_file_path, from_dir).replace(
os.path.sep, "/"
)
except ValueError:
# On Windows, a relative path is not always possible (no common ancestor)
line = (
Expand All @@ -174,7 +181,9 @@ def format_requirement(
else _build_direct_reference_best_efforts(ireq)
)
else:
line = f"{'-e ' if ireq.editable else ''}{path_url}{fragment}{delimiter}{extras}"
if not prefix and not relpath.startswith("."):
prefix = "./"
line = f"{'-e ' if ireq.editable else ''}{prefix}{relpath}{fragment}{extras}"

if marker:
line = f"{line} ; {marker}"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def test_local_file_uri_package(
),
pytest.param(
"./small_fake_with_extras[dev,test]",
"file:small_fake_with_extras#[dev,test]",
"./small_fake_with_extras[dev,test]",
id="relative path with extras",
),
pytest.param(
Expand Down

0 comments on commit aa3ee0f

Please sign in to comment.