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

8.0.0 + pyargs + permissions causes failures #11904

Closed
pulkin opened this issue Jan 31, 2024 · 8 comments · Fixed by #12043
Closed

8.0.0 + pyargs + permissions causes failures #11904

pulkin opened this issue Jan 31, 2024 · 8 comments · Fixed by #12043
Labels
topic: collection related to the collection phase topic: selection related to test selection from the command line type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously

Comments

@pulkin
Copy link

pulkin commented Jan 31, 2024

Example of a successful test-install build on 7.4.4:

https://github.com/pulkin/pyteleport/actions/runs/7603885071/job/20706143053

On 8.0.0 it starts looking into inaccessible folders and fails right away:

https://github.com/pulkin/pyteleport/actions/runs/7733355863/job/21085241736

@bluetech
Copy link
Member

bluetech commented Feb 4, 2024

This has been reported a few times already. The previous cases were legitimately bad ideas, running tests from /tmp. But since it seems like a common issue, we probably should make it work.

The easiest thing is to simply ignore files/dirs which raise a permission error, however I don't like this because it may silently skip over files which are meant to be included and really have wrong perms.

Another easy option is to warn and ignore, but the people who report this probably don't want the noise.

The problem started happening in pytest 8 because of the Session collection changes. Previously Session would collect the initial files itself, and thus could filter out non-matching files early. Now Session only collects the initial Directorys and then does a "matchnodes" pass after the fact, which means some files now get stat'ed when they previously weren't.

With some effort we could probably go back to avoiding stats on files that are going to get discarded anyway. However it is probably too big a change for 8.0.x so will only be fixed in a feature release.

@bluetech bluetech added type: bug problem that needs to be addressed topic: collection related to the collection phase topic: selection related to test selection from the command line type: regression indicates a problem that was introduced in a release which was working previously labels Feb 4, 2024
@pulkin
Copy link
Author

pulkin commented Feb 4, 2024

I am all ears if you come up with a solution to test an installed package in my case. I used cd / to distance from my src folder as far as it is possible before running pytest.

@bluetech
Copy link
Member

bluetech commented Feb 4, 2024

I do recommend cd'ing into some empty directory your user owns, or at least /var/empty if you need something ready made.

@pulkin
Copy link
Author

pulkin commented Feb 4, 2024

To whomever interested, I did this

pulkin/pyteleport@8a5e87c

Creating an empty subfolder in the repo folder did not work because pytest can still find setup.py and use repo files. So you have to create an empty folder side-by-side and test from there.

@KellyP5
Copy link

KellyP5 commented Feb 8, 2024

I too am experiencing similar issues, am curious about a way forward. Given my use case, I do not have the option of creating a directory and cd'ing into it given my working environment.

@mrlifetime
Copy link

This is an issue for my org.
We deploy applications fully isolated, so they all ship with their own python deps, similar to venv.
We have a generic test tool that allows running pytest via --pyargs, however the collector now doesn't stay within the confines of the package and starts adding dirs starting from root which contains tons of files and folders that it isn't permissioned to look at.

The typical invocation would look something like:

/root/packages/libexec/mypackage/bin/python -m pytest --pyargs myorg.myteam.mypackage

With v8, looking at pytest debug file, this starts traversing /root first, encountering all sorts of things. Instead, what we expect is that it will stay within the confines of where the module is installed, i.e.

/root/packages/libexec/mypackage/python/lib/python3.11/site-packages/myorg/myteam/mypackage

As a workaround, I can simply run pytest on the folder ...myorg/myteam/mypackage but this is far less elegant than --pyargs that should just resolve this.

@nicoddemus
Copy link
Member

Another possible workaround is specifying --confcutdir to the directory you want pytest to start to and below.

But I agree the current situation is not optimal.

krassowski added a commit to krassowski/jupyterlab-lsp that referenced this issue Feb 23, 2024
oleksandr-pavlyk added a commit to IntelPython/dpctl that referenced this issue Feb 27, 2024
Create empty temporary directory to run "pytest" command from to
work-around an issue in pytest accessing files/directories it may
not have access to during test collection.

pytest-dev/pytest#11904
oleksandr-pavlyk added a commit to IntelPython/dpctl that referenced this issue Feb 27, 2024
Create empty temporary directory to run "pytest" command from to
work-around an issue in pytest accessing files/directories it may
not have access to during test collection.

pytest-dev/pytest#11904
oleksandr-pavlyk added a commit to IntelPython/dpctl that referenced this issue Feb 27, 2024
Create empty temporary directory to run "pytest" command from to
work-around an issue in pytest accessing files/directories it may
not have access to during test collection.

pytest-dev/pytest#11904
oleksandr-pavlyk added a commit to IntelPython/dpctl that referenced this issue Feb 27, 2024
Create empty temporary directory to run "pytest" command from to
work-around an issue in pytest accessing files/directories it may
not have access to during test collection.

pytest-dev/pytest#11904
bluetech added a commit to bluetech/pytest that referenced this issue Mar 1, 2024
…llection arguments

(diff better viewed ignoring whitespace)

In pytest<8, the collection tree for `pyargs` arguments in an invocation
like this:

    pytest --collect-only --pyargs pyflakes.test.test_undefined_names

looked like this:

```
<Package test>
  <Module test_undefined_names.py>
    <UnitTestCase Test>
      <TestCaseFunction test_annotationUndefined>
      ... snipped ...
```

The pytest 8 collection improvements changed it to this:

```
<Dir pytest>
  <Dir .tox>
    <Dir venv>
      <Dir lib>
        <Dir python3.11>
          <Dir site-packages>
            <Package pyflakes>
              <Package test>
                <Module test_undefined_names.py>
                  <UnitTestCase Test>
                    <TestCaseFunction test_annotationUndefined>
                    ... snipped ...
```

Besides being egregious (and potentially worse than the above, going all
the way to the root, for system-installed packages, as is apparently
common in CI), this also caused permission errors when trying to probe
some of those intermediate directories.

This change makes `--pyargs` arguments no longer try to add parent
directories to the collection tree according to the `--confcutdir` like
their regular arguments. Instead, only add the parents that are in the
import path. This now looks like this:

```
<Package .tox/venv/lib/python3.11/site-packages/pyflakes>
  <Package test>
    <Module test_undefined_names.py>
      <UnitTestCase Test>
        <TestCaseFunction test_annotationUndefined>
        ... snipped ...
```

Fix pytest-dev#11904.
bluetech added a commit to bluetech/pytest that referenced this issue Mar 1, 2024
…llection arguments

(diff better viewed ignoring whitespace)

In pytest<8, the collection tree for `pyargs` arguments in an invocation
like this:

    pytest --collect-only --pyargs pyflakes.test.test_undefined_names

looked like this:

```
<Package test>
  <Module test_undefined_names.py>
    <UnitTestCase Test>
      <TestCaseFunction test_annotationUndefined>
      ... snipped ...
```

The pytest 8 collection improvements changed it to this:

```
<Dir pytest>
  <Dir .tox>
    <Dir venv>
      <Dir lib>
        <Dir python3.11>
          <Dir site-packages>
            <Package pyflakes>
              <Package test>
                <Module test_undefined_names.py>
                  <UnitTestCase Test>
                    <TestCaseFunction test_annotationUndefined>
                    ... snipped ...
```

Besides being egregious (and potentially even worse than the above,
going all the way to the root, for system-installed packages, as is
apparently common in CI), this also caused permission errors when trying
to probe some of those intermediate directories.

This change makes `--pyargs` arguments no longer try to add parent
directories to the collection tree according to the `--confcutdir` like
they're regular arguments. Instead, only add the parents that are in the
import path. This now looks like this:

```
<Package .tox/venv/lib/python3.11/site-packages/pyflakes>
  <Package test>
    <Module test_undefined_names.py>
      <UnitTestCase Test>
        <TestCaseFunction test_annotationUndefined>
        ... snipped ...
```

Fix pytest-dev#11904.
bluetech added a commit to bluetech/pytest that referenced this issue Mar 1, 2024
…llection arguments

(diff better viewed ignoring whitespace)

In pytest<8, the collection tree for `pyargs` arguments in an invocation
like this:

    pytest --collect-only --pyargs pyflakes.test.test_undefined_names

looked like this:

```
<Package test>
  <Module test_undefined_names.py>
    <UnitTestCase Test>
      <TestCaseFunction test_annotationUndefined>
      ... snipped ...
```

The pytest 8 collection improvements changed it to this:

```
<Dir pytest>
  <Dir .tox>
    <Dir venv>
      <Dir lib>
        <Dir python3.11>
          <Dir site-packages>
            <Package pyflakes>
              <Package test>
                <Module test_undefined_names.py>
                  <UnitTestCase Test>
                    <TestCaseFunction test_annotationUndefined>
                    ... snipped ...
```

Besides being egregious (and potentially even worse than the above,
going all the way to the root, for system-installed packages, as is
apparently common in CI), this also caused permission errors when trying
to probe some of those intermediate directories.

This change makes `--pyargs` arguments no longer try to add parent
directories to the collection tree according to the `--confcutdir` like
they're regular arguments. Instead, only add the parents that are in the
import path. This now looks like this:

```
<Package .tox/venv/lib/python3.11/site-packages/pyflakes>
  <Package test>
    <Module test_undefined_names.py>
      <UnitTestCase Test>
        <TestCaseFunction test_annotationUndefined>
        ... snipped ...
```

Fix pytest-dev#11904.
bluetech added a commit to bluetech/pytest that referenced this issue Mar 2, 2024
…llection arguments

(diff better viewed ignoring whitespace)

In pytest<8, the collection tree for `pyargs` arguments in an invocation
like this:

    pytest --collect-only --pyargs pyflakes.test.test_undefined_names

looked like this:

```
<Package test>
  <Module test_undefined_names.py>
    <UnitTestCase Test>
      <TestCaseFunction test_annotationUndefined>
      ... snipped ...
```

The pytest 8 collection improvements changed it to this:

```
<Dir pytest>
  <Dir .tox>
    <Dir venv>
      <Dir lib>
        <Dir python3.11>
          <Dir site-packages>
            <Package pyflakes>
              <Package test>
                <Module test_undefined_names.py>
                  <UnitTestCase Test>
                    <TestCaseFunction test_annotationUndefined>
                    ... snipped ...
```

Besides being egregious (and potentially even worse than the above,
going all the way to the root, for system-installed packages, as is
apparently common in CI), this also caused permission errors when trying
to probe some of those intermediate directories.

This change makes `--pyargs` arguments no longer try to add parent
directories to the collection tree according to the `--confcutdir` like
they're regular arguments. Instead, only add the parents that are in the
import path. This now looks like this:

```
<Package .tox/venv/lib/python3.11/site-packages/pyflakes>
  <Package test>
    <Module test_undefined_names.py>
      <UnitTestCase Test>
        <TestCaseFunction test_annotationUndefined>
        ... snipped ...
```

Fix pytest-dev#11904.
ricolin added a commit to vexxhost/ansible-collection-kubernetes that referenced this issue Mar 4, 2024
ricolin added a commit to vexxhost/ansible-collection-kubernetes that referenced this issue Mar 5, 2024
ricolin added a commit to vexxhost/ansible-collection-kubernetes that referenced this issue Mar 7, 2024
ricolin added a commit to vexxhost/ansible-collection-kubernetes that referenced this issue Mar 18, 2024
flying-sheep pushed a commit to flying-sheep/pytest that referenced this issue Apr 9, 2024
…llection arguments

(diff better viewed ignoring whitespace)

In pytest<8, the collection tree for `pyargs` arguments in an invocation
like this:

    pytest --collect-only --pyargs pyflakes.test.test_undefined_names

looked like this:

```
<Package test>
  <Module test_undefined_names.py>
    <UnitTestCase Test>
      <TestCaseFunction test_annotationUndefined>
      ... snipped ...
```

The pytest 8 collection improvements changed it to this:

```
<Dir pytest>
  <Dir .tox>
    <Dir venv>
      <Dir lib>
        <Dir python3.11>
          <Dir site-packages>
            <Package pyflakes>
              <Package test>
                <Module test_undefined_names.py>
                  <UnitTestCase Test>
                    <TestCaseFunction test_annotationUndefined>
                    ... snipped ...
```

Besides being egregious (and potentially even worse than the above,
going all the way to the root, for system-installed packages, as is
apparently common in CI), this also caused permission errors when trying
to probe some of those intermediate directories.

This change makes `--pyargs` arguments no longer try to add parent
directories to the collection tree according to the `--confcutdir` like
they're regular arguments. Instead, only add the parents that are in the
import path. This now looks like this:

```
<Package .tox/venv/lib/python3.11/site-packages/pyflakes>
  <Package test>
    <Module test_undefined_names.py>
      <UnitTestCase Test>
        <TestCaseFunction test_annotationUndefined>
        ... snipped ...
```

Fix pytest-dev#11904.
@Erotemic
Copy link

I'm seeing something which looks like this issue in line-profiler.

The PR containing the issue is: pyutils/line_profiler#257

When I pin the pytest version to pytest<=7.4.4, everything works correctly. This run shows that behavior: https://github.com/pyutils/line_profiler/actions/runs/8863760338

But if I unpin pytest I start getting permission errors in osx and win32 builds inside the cibuildwheel job.
https://github.com/pyutils/line_profiler/actions/runs/8863904478

The invocation of pytest is inside another Python file: run_tests.py (link is to the version in the PR) I'm doing import pytest and then pytest.main(pytest_args). Is there anything I can do in the surrounding script to ensure pytest doesn't try to check these directories?

Errors I get look like:

  =================================== ERRORS ====================================
  ________________________ ERROR collecting test session ________________________
  C:\Users\runneradmin\AppData\Local\Temp\cibw-run-muf21jxz\cp38-win_amd64\venv-test\lib\site-packages\_pytest\runner.py:341: in from_call
      result: Optional[TResult] = func()
  C:\Users\runneradmin\AppData\Local\Temp\cibw-run-muf21jxz\cp38-win_amd64\venv-test\lib\site-packages\_pytest\runner.py:389: in collect
      return list(collector.collect())
  C:\Users\runneradmin\AppData\Local\Temp\cibw-run-muf21jxz\cp38-win_amd64\venv-test\lib\site-packages\_pytest\main.py:523: in collect
      for direntry in scandir(self.path):
  C:\Users\runneradmin\AppData\Local\Temp\cibw-run-muf21jxz\cp38-win_amd64\venv-test\lib\site-packages\_pytest\pathlib.py:896: in scandir
      with os.scandir(path) as s:
  E   PermissionError: [WinError 5] Access is denied: 'C:\\Documents and Settings'
  
  ---------- coverage: platform win32, python 3.8.10-final-0 -----------

...

  =========================== short test summary info ===========================
  ERROR ::::Documents and Settings - PermissionError: [WinError 5] Access is denied: 'C:\\Documents and Settings'
  !!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
  ============================== 1 error in 1.39s ===============================

and on OSX:

  ============================= test session starts ==============================
  platform darwin -- Python 3.8.10, pytest-8.2.0, pluggy-1.5.0
  rootdir: /Users/runner/work/line_profiler/line_profiler
  configfile: pyproject.toml
  plugins: cov-5.0.0, xdoctest-1.1.3
  collected 0 items / 1 error
  
  ==================================== ERRORS ====================================
  ________________________ ERROR collecting test session _________________________
  /private/var/folders/3m/p59k4qdj0f17st0gn2cmj3640000gn/T/cibw-run-cy6pklkf/cp38-macosx_x86_64/venv-test-x86_64/lib/python3.8/site-packages/pluggy/_hooks.py:513: in __call__
      return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  /private/var/folders/3m/p59k4qdj0f17st0gn2cmj3640000gn/T/cibw-run-cy6pklkf/cp38-macosx_x86_64/venv-test-x86_64/lib/python3.8/site-packages/pluggy/_manager.py:120: in _hookexec
      return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  /private/var/folders/3m/p59k4qdj0f17st0gn2cmj3640000gn/T/cibw-run-cy6pklkf/cp38-macosx_x86_64/venv-test-x86_64/lib/python3.8/site-packages/_pytest/python.py:179: in pytest_collect_directory
      if pkginit.is_file():
  /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py:1439: in is_file
      return S_ISREG(self.stat().st_mode)
  /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py:1198: in stat
      return self._accessor.stat(self)
  E   PermissionError: [Errno 13] Permission denied: '/private/var/audit/__init__.py'
  =========================== short test summary info ============================
  ERROR ../::private::var - PermissionError: [Errno 13] Permission denied: '/private/var/audit/__init__.py'
  =============================== 1 error in 0.16s ===============================
  [run_tests] pytest returned ret=4
  [run_tests] Restoring cwd = '/private/var/folders/3m/p59k4qdj0f17st0gn2cmj3640000gn/T/cibw-run-cy6pklkf/cp38-macosx_x86_64/test_cwd'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase topic: selection related to test selection from the command line type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants