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

Skip $PATH entries we cannot check rather than dying with PermissionError #2782

Merged
merged 1 commit into from
Oct 18, 2024

Conversation

hroncok
Copy link
Contributor

@hroncok hroncok commented Oct 16, 2024

On Fedora CI, I used runuser to get rid of root privileges before testing virtualenv.

The invocation I used did not clear the root's $PATH and as a result, one of the entries in $PATH was /root/.local/bin.
But /root/ does not allow other users to browse (execute, read).

That lead to an unhanded PermissionError.

When this happens, the $PATH entry is now skipped as if it did not exist.

As there might be other reasons we cannot check if the $PATH entry exists, I decided to suppress all OSError types.

For clarity, here is the Traceback I was getting:

# runuser testuser -c 'virtualenv --with-traceback --python pypy3.10 venv'
Traceback (most recent call last):
  File "/usr/bin/virtualenv", line 8, in <module>
    sys.exit(run_with_catch())
             ~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/virtualenv/__main__.py", line 56, in run_with_catch
    run(args, options, env)
    ~~~^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/virtualenv/__main__.py", line 18, in run
    session = cli_run(args, options, env)
  File "/usr/lib/python3.13/site-packages/virtualenv/run/__init__.py", line 31, in cli_run
    of_session = session_via_cli(args, options, setup_logging, env)
  File "/usr/lib/python3.13/site-packages/virtualenv/run/__init__.py", line 49, in session_via_cli
    parser, elements = build_parser(args, options, setup_logging, env)
                       ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/virtualenv/run/__init__.py", line 77, in build_parser
    parser._interpreter = interpreter = discover.interpreter  # noqa: SLF001
                                        ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/virtualenv/discovery/discover.py", line 41, in interpreter
    self._interpreter = self.run()
                        ~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/virtualenv/discovery/builtin.py", line 58, in run
    result = get_interpreter(python_spec, self.try_first_with, self.app_data, self._env)
  File "/usr/lib/python3.13/site-packages/virtualenv/discovery/builtin.py", line 75, in get_interpreter
    for interpreter, impl_must_match in propose_interpreters(spec, try_first_with, app_data, env):
                                        ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/virtualenv/discovery/builtin.py", line 147, in propose_interpreters
    for pos, path in enumerate(get_paths(env)):
                     ~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/virtualenv/discovery/builtin.py", line 169, in get_paths
    if p.exists():
       ~~~~~~~~^^
  File "/usr/lib64/python3.13/pathlib/_abc.py", line 450, in exists
    self.stat(follow_symlinks=follow_symlinks)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.13/pathlib/_local.py", line 515, in stat
    return os.stat(self, follow_symlinks=follow_symlinks)
           ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/root/.local/bin'

Thanks for contributing, make sure you address all the checklists (for details on how see development documentation)

  • 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

@hroncok hroncok force-pushed the do-not-die-nonbrowseable-path branch from c053e03 to 4b6acb3 Compare October 16, 2024 22:46
…rror

On Fedora CI, I used runuser to get rid of root privileges before testing virtualenv.

The invocation I used did not clear the root's $PATH and as a result,
one of the entries in $PATH was /root/.local/bin.
But /root/ does not allow other users to browse (execute, read).

That lead to an unhanded PermissionError.

When this happens, the $PATH entry is now skipped as if it did not exist.

As there might be other reasons we cannot check if the $PATH entry exists,
I decided to suppress all OSError types.

For clarity, here is the Traceback I was getting:

    # runuser testuser -c 'virtualenv --with-traceback --python pypy3.10 venv'
    Traceback (most recent call last):
      File "/usr/bin/virtualenv", line 8, in <module>
        sys.exit(run_with_catch())
                 ~~~~~~~~~~~~~~^^
      File "/usr/lib/python3.13/site-packages/virtualenv/__main__.py", line 56, in run_with_catch
        run(args, options, env)
        ~~~^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/virtualenv/__main__.py", line 18, in run
        session = cli_run(args, options, env)
      File "/usr/lib/python3.13/site-packages/virtualenv/run/__init__.py", line 31, in cli_run
        of_session = session_via_cli(args, options, setup_logging, env)
      File "/usr/lib/python3.13/site-packages/virtualenv/run/__init__.py", line 49, in session_via_cli
        parser, elements = build_parser(args, options, setup_logging, env)
                           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/virtualenv/run/__init__.py", line 77, in build_parser
        parser._interpreter = interpreter = discover.interpreter  # noqa: SLF001
                                            ^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/virtualenv/discovery/discover.py", line 41, in interpreter
        self._interpreter = self.run()
                            ~~~~~~~~^^
      File "/usr/lib/python3.13/site-packages/virtualenv/discovery/builtin.py", line 58, in run
        result = get_interpreter(python_spec, self.try_first_with, self.app_data, self._env)
      File "/usr/lib/python3.13/site-packages/virtualenv/discovery/builtin.py", line 75, in get_interpreter
        for interpreter, impl_must_match in propose_interpreters(spec, try_first_with, app_data, env):
                                            ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/virtualenv/discovery/builtin.py", line 147, in propose_interpreters
        for pos, path in enumerate(get_paths(env)):
                         ~~~~~~~~~^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/virtualenv/discovery/builtin.py", line 169, in get_paths
        if p.exists():
           ~~~~~~~~^^
      File "/usr/lib64/python3.13/pathlib/_abc.py", line 450, in exists
        self.stat(follow_symlinks=follow_symlinks)
        ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib64/python3.13/pathlib/_local.py", line 515, in stat
        return os.stat(self, follow_symlinks=follow_symlinks)
               ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    PermissionError: [Errno 13] Permission denied: '/root/.local/bin'
@hroncok hroncok force-pushed the do-not-die-nonbrowseable-path branch from 54eba67 to 7a77931 Compare October 16, 2024 22:47
@hroncok hroncok marked this pull request as ready for review October 16, 2024 23:14
@hroncok
Copy link
Contributor Author

hroncok commented Oct 16, 2024

(I don't think the CI failures are related.)

@gaborbernat
Copy link
Contributor

Can you put in another PR to fix that failure to unblock merging?

@hroncok
Copy link
Contributor Author

hroncok commented Oct 17, 2024

I have no idea what's going on there.

@gaborbernat gaborbernat merged commit 6f16059 into pypa:main Oct 18, 2024
30 of 40 checks passed
@hroncok hroncok deleted the do-not-die-nonbrowseable-path branch October 18, 2024 09:16
@hroncok
Copy link
Contributor Author

hroncok commented Oct 18, 2024

Thanks.

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