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 path resolution for python executables looked up in PATH on windows #1232

Merged
merged 4 commits into from
Feb 19, 2024

Conversation

guahki
Copy link
Contributor

@guahki guahki commented Jan 25, 2024

  • I have added a news fragment under changelog.d/ (if the patch affects the end users)

Summary of changes

Only use the logic introduced in #1168 to fix a path resolution issue on windows when the given path already is absolute. This avoids prepending the cwd to relative paths.
This fixes #1205.

Test plan

Tested by running the following command on a windows machine with python3.12.exe in PATH (which errored before):

pipx run --python=python3.12.exe pycowsay moo

Copy link
Contributor

@Gitznik Gitznik left a comment

Choose a reason for hiding this comment

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

IMO we should not merge this before someone was able to replicate this still fixes the path redirection issue.

Also please fix the failing tests and add tests covering this new logic.

src/pipx/util.py Outdated Show resolved Hide resolved
src/pipx/util.py Outdated Show resolved Hide resolved
@dechamps
Copy link
Contributor

IMO we should not merge this before someone was able to replicate this still fixes the path redirection issue.

I just tested 02f5fe2 myself and can confirm there is no regression - #1164 is still fixed on that commit.

@Gitznik
Copy link
Contributor

Gitznik commented Jan 28, 2024

IMO we should not merge this before someone was able to replicate this still fixes the path redirection issue.

I just tested 02f5fe2 myself and can confirm there is no regression - #1164 is still fixed on that commit.

Thanks for verifying!

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.

You need to make the CI pass before we do any more review.

@guahki guahki force-pushed the fix_python_lookup_windows branch 2 times, most recently from 2436536 to dc7b2ee Compare February 2, 2024 20:29
@guahki
Copy link
Contributor Author

guahki commented Feb 2, 2024

I dived into the source code and had a totally new idea how/where to fix the original issue: I think it is quite logical to resolve the root path when calculating the venv paths. Small caveat is, that the shared lib paths have to be recomputed after creating the lib, as the first computation might no correctly resolve the redirected paths (I think, due to the fact, that the class is instantiated at import time and not even the pipx folder might exist at this early time).

I added comments in the interesting places.

@Gitznik what do you think about this?

@dechamps I tested this with your issue #1164 and for the tests I did, it worked. But of course I invite you to heavily test this commit and share your findings here!

PS: I know, that some tests might be failing. But I wanted to first get your feedback, before I spend time fixing the tests for nothing.

@Gitznik
Copy link
Contributor

Gitznik commented Feb 2, 2024

I think this only works on a subset of the problem. In pipx/venv.py line 157, we would still use the provided python version, not the one that is resolved via get_venv_paths. Can you test if this still works if you explicitly pass a python version that is subject to path redirection via the --python argument? @guahki

@guahki
Copy link
Contributor Author

guahki commented Feb 4, 2024

Can you test if this still works if you explicitly pass a python version that is subject to path redirection via the --python argument?

I tried to test this and did not manage to find any issue. Using a MS Store Python (via PATH lookup or direct path) in --python did work for me.

I tried at least

  • pipx run --python python3.12.exe pycowsay moo
  • pipx run --python C:\Users\<snip>\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\python.exe pycowsay moo

I think this only works on a subset of the problem. In pipx/venv.py line 157, we would still use the provided python version, not the one that is resolved via get_venv_paths.

I'm not 100% sure, how this whole path redirection magic works for MS Store Apps. But as far as I understand, the line you quote is no problem, as the python executable loaded here is aware of it's path redirection magic and handles that correctly by itself, independent of us (pipx) giving it a helping hand.

As far as I understand (and also the hypothesis by @dechamps expressed in #1164 (comment)), the issue arises only when calling a non-MS-Store executable living inside the redirected path by it's "unredirected" name. Then this executable thinks, it lives in (and searches for other files relative to) the unredirected path, when in reality it should use the redirected one. And this executable (due to not being part of a MS Store bundle) does not transparently redirect the paths on OS level.

@guahki
Copy link
Contributor Author

guahki commented Feb 4, 2024

However, as discussed in #1203 (comment), using different Python versions than the one used by pipx itself can not work at all with MS Store Python and the location of pipx's venv folder inside AppData\Local

@Gitznik
Copy link
Contributor

Gitznik commented Feb 4, 2024

As far as I understand (and also the hypothesis by @dechamps expressed in #1164 (comment)), the issue arises only when calling a non-MS-Store executable living inside the redirected path by it's "unredirected" name. Then this executable thinks, it lives in (and searches for other files relative to) the unredirected path, when in reality it should use the redirected one. And this executable (due to not being part of a MS Store bundle) does not transparently redirect the paths on OS level.

Thanks for double checking. In that case I think this is the preferable fix as it only resolves the path exactly where the issue occurs.

However, as discussed in #1203 (comment), using different Python versions than the one used by pipx itself can not work at all with MS Store Python and the location of pipx's venv folder inside AppData\Local

So this only fixes passing the python version that is already used anyways by default as it's the one used by pipx?

@guahki
Copy link
Contributor Author

guahki commented Feb 4, 2024

So this only fixes passing the python version that is already used anyways by default as it's the one used by pipx?

Yes and no.

Yes: in the "new" directory placement (version 1.3.0 and newer) it is inherently impossible to use another python version. I think I will open another issue about this. Nevertheless, I could give this version as full path as well as just --python python3.12.exe which does a PATH lookup.

And no: when a legacy (pre 1.3.0) directory is present, I could use any of my installed python versions (MS Store Python as well as non-MS Store one) by lookup in the PATH as well as giving the full path. The only thing I could not test quickly, was using the py-launcher (as I do not use it on this system), but the change here should obviously not create any issues with it (and your new pipx/interpreter:find_python_interpreter handled this quite nice).

@Gitznik
Copy link
Contributor

Gitznik commented Feb 4, 2024

In that case I think this is a good fix. Please fix the failing test and then mark ready for review 👍 I think the thing tested there is no longer relevant because you removed the realpath logic from the run_subprocess function.

Also please add a test to get_venv_path that verifies the results. I don't think we can actually fully reproduce the original error there as it has too many assumptions on how python is installed on the runner, but maybe you can just create a file in a directory that is subject to path redirection and check if it resolves correctly? (actually maybe we can just pretty much copy this example from the python docs into a test)

@dechamps
Copy link
Contributor

dechamps commented Feb 4, 2024

maybe you can just create a file in a directory that is subject to path redirection and check if it resolves correctly?

I am not aware of any way to manually set up this kind of "path redirection" mechanism on Windows outside of an MS Store app. Back when I wrote #1168 I tried to write a regression test in the form of a GitHub Actions workflow that installed Python from the MS Store, but I hit a wall in the form of Cyberboss/install-winget#1. It would sure be nice to catch MS Store Python regressions automatically, but it's not clear how that can be done.

@Gitznik
Copy link
Contributor

Gitznik commented Feb 4, 2024

I am not aware of any way to manually set up this kind of "path redirection" mechanism on Windows outside of an MS Store app.

So the linked example from the python docs only applies if executed via a MS store python version?

@dechamps
Copy link
Contributor

dechamps commented Feb 4, 2024

As far as I'm aware, yes.

@Gitznik
Copy link
Contributor

Gitznik commented Feb 7, 2024

As reproducing the issue in a test seems unlikely, please fix the failing CI and then this is ready for review.

The discussion on moving away from using AppDir by default on windows lives at #1247

@guahki
Copy link
Contributor Author

guahki commented Feb 10, 2024

please fix the failing CI and then this is ready for review

I fixed the CI by just deleting the two test functions, testing the path resolution in the run_subprocess function. As discussed, I do not know, what (or better how) to test these changes in a better way.

Thanks for all your help, moderation and understanding @Gitznik!

@guahki guahki marked this pull request as ready for review February 10, 2024 13:01
Copy link
Contributor

@Gitznik Gitznik left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for all the investigation @guahki

@Gitznik Gitznik merged commit edec578 into pypa:main Feb 19, 2024
12 checks passed
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.

--python flag is broken on Windows
4 participants