Skip to content

Commit

Permalink
Relative paths improvements for editable installs (#5896)
Browse files Browse the repository at this point in the history
* Relative paths improvements for editable installs
  • Loading branch information
matteius authored Aug 31, 2023
1 parent 16334c9 commit 90714e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/5896.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Relative paths improvements for editable installs.
23 changes: 21 additions & 2 deletions pipenv/utils/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,29 @@ def determine_path_specifier(package: InstallRequirement):
if package.link.scheme in ["http", "https"]:
return package.link.url_without_fragment
if package.link.scheme == "file":
abs_path = Path(package.link.file_path).resolve()
current_dir = Path.cwd()

try:
return Path(package.link.file_path).relative_to(Path.cwd()).as_posix()
relative_path = abs_path.relative_to(current_dir)
return relative_path.as_posix()
except ValueError:
return Path(package.link.file_path).as_posix()
# If the direct relative_to fails, manually compute the relative path
common_parts = 0
for part_a, part_b in zip(abs_path.parts, current_dir.parts):
if part_a == part_b:
common_parts += 1
else:
break

# Number of ".." needed are the extra parts in the current directory
# beyond the common parts
up_levels = [".."] * (len(current_dir.parts) - common_parts)
# The relative path is constructed by going up as needed and then
# appending the non-common parts of the absolute path
rel_parts = up_levels + list(abs_path.parts[common_parts:])
relative_path = Path(*rel_parts)
return relative_path.as_posix()


def determine_vcs_specifier(package: InstallRequirement):
Expand Down

0 comments on commit 90714e2

Please sign in to comment.