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

Relative paths improvements for editable installs #5896

Merged
merged 5 commits into from
Aug 31, 2023
Merged
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 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()

Choose a reason for hiding this comment

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

Thanks for this!

Is it possible that Path.cwd() is not the virtual environment root path (i.e typically where the Pipfile is located?). I thought all paths were always relative to the Pipfile etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe it has to be where the Pipfile is located because pipenv changes directories to that location when its invoked, if we ever change that assumption this here will break.

Choose a reason for hiding this comment

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

So when pipenv is executed it changes the current working directory for the process to the location of the Pipfile? If so, I get it now, I just don't understand why the code path that manually constructs a relative code path is required if that's the case.



def determine_vcs_specifier(package: InstallRequirement):
Expand Down
Loading