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 #1949: zipapp virtual environment creation fails if zipapp path is symlinked #2613

Closed
wants to merge 3 commits into from

Conversation

petamas
Copy link

@petamas petamas commented Jul 20, 2023

Original issue: #1949

The function in question assumes that ROOT is a prefix of full_path, and tries to remove it. However, it is not necessarily a prefix:

  • when ROOT is determined, both os.path.abspath() and os.path.realpath() gets called
  • when full_path is determined, only os.path.abspath() is called

This means that if the path contains a symlink, it will show up as resolved in ROOT, but not full_path, so simply removing the first X characters of full_path is not sufficient.

This PR adds an assertion to catch the bad situation early, then adds the os.path.realpath call to full_path.

I did not add a test because I was unable to figure out how to add it to the existing structure.

  • ran the linter to address style issues (tox -e fix)
  • wrote descriptive pull request text
  • ensured there are test(s) validating the fix
  • added news fragment in docs/changelog folder
  • updated/extended the documentation

@petamas
Copy link
Author

petamas commented Jul 20, 2023

@gaborbernat ruff is failing this PR because it considers using asserts harmful. (https://beta.ruff.rs/docs/rules/assert/) I strongly believe that an assert is the right way to address this error case, because it can only happen by a programmer error (eg. if someone changes the definition of ROOT), but naturally I'm willing to adapt to maintainers' taste.

I see the following possible solutions:

  1. Disable S101 for this line specifically
  2. Disable S101 globally (if you also happen to disagree with the rationale)
  3. Remove the assert, only fix the single line
  4. Replace the assert with throwing an exception (I have no idea what kind, possibly ValueError?)

How do you want me to proceed?

@gaborbernat
Copy link
Contributor

  1. Disable S101 for this line specifically

Copy link
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

Please add tests validating your change fixes the problem.

Comment on lines +26 to +27
full_path = os.path.realpath(os.path.abspath(str(full_path)))
prefix = ROOT + os.sep
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use here the pathlib variant instead?

Copy link
Author

Choose a reason for hiding this comment

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

Sure. I saw most of the other code uses pathlib, but I wasn't sure whether it's intentional or not, and wanted to keep the size of the diff minimal. It's way simpler to implement the whole function using PurePath.relative_to anyway.

Question: if I migrate to pathlib, the trivial implementation will throw ValueError in case ROOT and full_path diverge, i.e. when my assert fails in the current version. Is that acceptable, or should I turn it back to an assert?

Copy link
Contributor

Choose a reason for hiding this comment

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

The ValueError is fine considering we're not expecting it to happen.

@petamas
Copy link
Author

petamas commented Jul 20, 2023

Please add tests validating your change fixes the problem.

Can you point me in the direction of where it should be added? I'm not that familiar with either pytest or tox. I also don't know how I could make sure the test passes because the issue is really fixed, or because of the caching behaviour described in the original report. Or should I add a unit test for the zipapp functionality specifically? Would the place for that be in test_util.py or somewhere else?

@gaborbernat
Copy link
Contributor

@gaborbernat gaborbernat marked this pull request as draft July 29, 2023 14:46
@gaborbernat
Copy link
Contributor

Seems stalled, closing until you pick it up again.

@gaborbernat gaborbernat closed this Aug 2, 2023
@petamas
Copy link
Author

petamas commented Aug 2, 2023

Yeah, sorry, I'm swamped, and I'll be AFK next week. I hope to eventually get back to this and reopen...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants