Repository: tox-dev/tox

Issue:
Indirect Extra-Dependencies use dependency metadata from PyPI
## Issue

We've run into the problem that tox when told to use the `tests` extra uses the Mypy pin from PyPI instead of the local package. Pinning tox to `<4` fixes it.

While researching this bug for this issue, I've found out that the problem is that tests is a [second-level extra](https://github.com/python-attrs/attrs/blob/d7eb692ff6c25f1c40d45ad45f306de04886131a/pyproject.toml#L50).

Using `tests-no-zope` works as expected (see below at repro).

Ref: https://github.com/python-attrs/attrs/pull/1070#issuecomment-1383993922


## Environment

- macOS 
- tox 4.4.2
- installed using pipx:

```console
❯ /Users/hynek/.local/pipx/venvs/tox/bin/python -m pip list
Package       Version
------------- -------
cachetools    5.2.1
chardet       5.1.0
colorama      0.4.6
distlib       0.3.6
filelock      3.9.0
packaging     23.0
pip           22.3.1
platformdirs  2.6.2
pluggy        1.0.0
pyproject_api 1.5.0
setuptools    66.1.1
tox           4.4.2
virtualenv    20.17.1
wheel         0.38.4
```

## Output of running tox


Provide the output of `tox -rvv`:

<details>

```console
.pkg: 196 W remove tox env folder /Users/hynek/FOSS/attrs/.tox/.pkg [tox/tox_env/api.py:321]
py311: 406 I find interpreter for spec PythonSpec(major=3, minor=11) [virtualenv/discovery/builtin.py:56]
py311: 412 D got python info of /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 from /Users/hynek/Library/Application Support/virtualenv/py_info/1/0045e09ab8f0ffed9fbd8e035337220de588ae52a0f88a6a71fae12ee607209a.json [virtualenv/app_data/via_disk_folder.py:129]
py311: 412 I proposed PythonInfo(spec=CPython3.11.1.final.0-64, system=/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11, exe=/Users/hynek/.local/pipx/venvs/tox/bin/python, platform=darwin, version='3.11.1 (main, Dec 30 2022, 09:54:37) [Clang 14.0.0 (clang-1400.0.29.202)]', encoding_fs_io=utf-8-utf-8) [virtualenv/discovery/builtin.py:63]
py311: 412 D accepted PythonInfo(spec=CPython3.11.1.final.0-64, system=/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11, exe=/Users/hynek/.local/pipx/venvs/tox/bin/python, platform=darwin, version='3.11.1 (main, Dec 30 2022, 09:54:37) [Clang 14.0.0 (clang-1400.0.29.202)]', encoding_fs_io=utf-8-utf-8) [virtualenv/discovery/builtin.py:65]
py311: 416 D filesystem is not case-sensitive [virtualenv/info.py:24]
py311: 461 I create virtual environment via CPython3Posix(dest=/Users/hynek/FOSS/attrs/.tox/py311, clear=False, no_vcs_ignore=False, global=False) [virtualenv/run/session.py:48]
py311: 462 D create folder /Users/hynek/FOSS/attrs/.tox/py311/bin [virtualenv/util/path/_sync.py:9]
py311: 462 D create folder /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages [virtualenv/util/path/_sync.py:9]
py311: 462 D write /Users/hynek/FOSS/attrs/.tox/py311/pyvenv.cfg [virtualenv/create/pyenv_cfg.py:30]
py311: 462 D    home = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin [virtualenv/create/pyenv_cfg.py:34]
py311: 462 D    implementation = CPython [virtualenv/create/pyenv_cfg.py:34]
py311: 462 D    version_info = 3.11.1.final.0 [virtualenv/create/pyenv_cfg.py:34]
py311: 462 D    virtualenv = 20.17.1 [virtualenv/create/pyenv_cfg.py:34]
py311: 462 D    include-system-site-packages = false [virtualenv/create/pyenv_cfg.py:34]
py311: 463 D    base-prefix = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11 [virtualenv/create/pyenv_cfg.py:34]
py311: 463 D    base-exec-prefix = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11 [virtualenv/create/pyenv_cfg.py:34]
py311: 463 D    base-executable = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 [virtualenv/create/pyenv_cfg.py:34]
py311: 463 D symlink /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 to /Users/hynek/FOSS/attrs/.tox/py311/bin/python [virtualenv/util/path/_sync.py:28]
py311: 464 D create virtualenv import hook file /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/_virtualenv.pth [virtualenv/create/via_global_ref/api.py:89]
py311: 464 D create /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/_virtualenv.py [virtualenv/create/via_global_ref/api.py:92]
py311: 465 D ============================== target debug ============================== [virtualenv/run/session.py:50]
py311: 465 D debug via /Users/hynek/FOSS/attrs/.tox/py311/bin/python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/virtualenv/create/debug.py [virtualenv/create/creator.py:197]
py311: 465 D {
  "sys": {
    "executable": "/Users/hynek/FOSS/attrs/.tox/py311/bin/python",
    "_base_executable": "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11",
    "prefix": "/Users/hynek/FOSS/attrs/.tox/py311",
    "base_prefix": "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11",
    "real_prefix": null,
    "exec_prefix": "/Users/hynek/FOSS/attrs/.tox/py311",
    "base_exec_prefix": "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11",
    "path": [
      "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip",
      "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11",
      "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload",
      "/Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages"
    ],
    "meta_path": [
      "<class '_virtualenv._Finder'>",
      "<class '_frozen_importlib.BuiltinImporter'>",
      "<class '_frozen_importlib.FrozenImporter'>",
      "<class '_frozen_importlib_external.PathFinder'>"
    ],
    "fs_encoding": "utf-8",
    "io_encoding": "utf-8"
  },
  "version": "3.11.1 (main, Dec 30 2022, 09:54:37) [Clang 14.0.0 (clang-1400.0.29.202)]",
  "makefile_filename": "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/config-3.11-darwin/Makefile",
  "os": "<module 'os' (frozen)>",
  "site": "<module 'site' (frozen)>",
  "datetime": "<module 'datetime' from '/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/datetime.py'>",
  "math": "<module 'math' from '/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload/math.cpython-311-darwin.so'>",
  "json": "<module 'json' from '/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py'>"
} [virtualenv/run/session.py:51]
py311: 522 I add seed packages via FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/hynek/Library/Application Support/virtualenv) [virtualenv/run/session.py:55]
py311: 526 D got embed update of distribution setuptools from /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/embed/3/setuptools.json [virtualenv/app_data/via_disk_folder.py:129]
py311: 527 D got embed update of distribution pip from /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/embed/3/pip.json [virtualenv/app_data/via_disk_folder.py:129]
py311: 527 D got embed update of distribution wheel from /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/embed/3/wheel.json [virtualenv/app_data/via_disk_folder.py:129]
py311: 531 D install setuptools from wheel /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/virtualenv/seed/wheels/embed/setuptools-65.6.3-py3-none-any.whl via CopyPipInstall [virtualenv/seed/embed/via_app_data/via_app_data.py:47]
py311: 531 D install pip from wheel /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/virtualenv/seed/wheels/embed/pip-22.3.1-py3-none-any.whl via CopyPipInstall [virtualenv/seed/embed/via_app_data/via_app_data.py:47]
py311: 531 D install wheel from wheel /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/virtualenv/seed/wheels/embed/wheel-0.38.4-py3-none-any.whl via CopyPipInstall [virtualenv/seed/embed/via_app_data/via_app_data.py:47]
py311: 533 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/wheel-0.38.4-py3-none-any/wheel to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/wheel [virtualenv/util/path/_sync.py:36]
py311: 533 D copy /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/pip-22.3.1-py3-none-any/pip-22.3.1.virtualenv to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/pip-22.3.1.virtualenv [virtualenv/util/path/_sync.py:36]
py311: 534 D copy /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/setuptools-65.6.3.virtualenv to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/setuptools-65.6.3.virtualenv [virtualenv/util/path/_sync.py:36]
py311: 534 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/pip-22.3.1-py3-none-any/pip to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/pip [virtualenv/util/path/_sync.py:36]
py311: 535 D copy /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/distutils-precedence.pth to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/distutils-precedence.pth [virtualenv/util/path/_sync.py:36]
py311: 536 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/setuptools-65.6.3.dist-info to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/setuptools-65.6.3.dist-info [virtualenv/util/path/_sync.py:36]
py311: 544 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/setuptools to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/setuptools [virtualenv/util/path/_sync.py:36]
py311: 551 D copy /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/wheel-0.38.4-py3-none-any/wheel-0.38.4.virtualenv to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/wheel-0.38.4.virtualenv [virtualenv/util/path/_sync.py:36]
py311: 552 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/wheel-0.38.4-py3-none-any/wheel-0.38.4.dist-info to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/wheel-0.38.4.dist-info [virtualenv/util/path/_sync.py:36]
py311: 562 D generated console scripts wheel3.11 wheel wheel3 wheel-3.11 [virtualenv/seed/embed/via_app_data/pip_install/base.py:41]
py311: 682 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/pkg_resources to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/pkg_resources [virtualenv/util/path/_sync.py:36]
py311: 713 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/_distutils_hack to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/_distutils_hack [virtualenv/util/path/_sync.py:36]
py311: 714 D generated console scripts  [virtualenv/seed/embed/via_app_data/pip_install/base.py:41]
py311: 861 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/pip-22.3.1-py3-none-any/pip-22.3.1.dist-info to /Users/hynek/FOSS/attrs/.tox/py311/lib/python3.11/site-packages/pip-22.3.1.dist-info [virtualenv/util/path/_sync.py:36]
py311: 865 D generated console scripts pip-3.11 pip3 pip3.11 pip [virtualenv/seed/embed/via_app_data/pip_install/base.py:41]
py311: 866 I add activators for Bash, CShell, Fish, Nushell, PowerShell, Python [virtualenv/run/session.py:61]
py311: 870 D write /Users/hynek/FOSS/attrs/.tox/py311/pyvenv.cfg [virtualenv/create/pyenv_cfg.py:30]
py311: 870 D    home = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin [virtualenv/create/pyenv_cfg.py:34]
py311: 870 D    implementation = CPython [virtualenv/create/pyenv_cfg.py:34]
py311: 870 D    version_info = 3.11.1.final.0 [virtualenv/create/pyenv_cfg.py:34]
py311: 870 D    virtualenv = 20.17.1 [virtualenv/create/pyenv_cfg.py:34]
py311: 870 D    include-system-site-packages = false [virtualenv/create/pyenv_cfg.py:34]
py311: 870 D    base-prefix = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11 [virtualenv/create/pyenv_cfg.py:34]
py311: 870 D    base-exec-prefix = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11 [virtualenv/create/pyenv_cfg.py:34]
py311: 870 D    base-executable = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 876 I find interpreter for spec PythonSpec(path=/Users/hynek/.local/pipx/venvs/tox/bin/python) [virtualenv/discovery/builtin.py:56]
.pkg: 876 I proposed PythonInfo(spec=CPython3.11.1.final.0-64, system=/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11, exe=/Users/hynek/.local/pipx/venvs/tox/bin/python, platform=darwin, version='3.11.1 (main, Dec 30 2022, 09:54:37) [Clang 14.0.0 (clang-1400.0.29.202)]', encoding_fs_io=utf-8-utf-8) [virtualenv/discovery/builtin.py:63]
.pkg: 876 D accepted PythonInfo(spec=CPython3.11.1.final.0-64, system=/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11, exe=/Users/hynek/.local/pipx/venvs/tox/bin/python, platform=darwin, version='3.11.1 (main, Dec 30 2022, 09:54:37) [Clang 14.0.0 (clang-1400.0.29.202)]', encoding_fs_io=utf-8-utf-8) [virtualenv/discovery/builtin.py:65]
.pkg: 877 I create virtual environment via CPython3Posix(dest=/Users/hynek/FOSS/attrs/.tox/.pkg, clear=False, no_vcs_ignore=False, global=False) [virtualenv/run/session.py:48]
.pkg: 878 D create folder /Users/hynek/FOSS/attrs/.tox/.pkg/bin [virtualenv/util/path/_sync.py:9]
.pkg: 878 D create folder /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages [virtualenv/util/path/_sync.py:9]
.pkg: 878 D write /Users/hynek/FOSS/attrs/.tox/.pkg/pyvenv.cfg [virtualenv/create/pyenv_cfg.py:30]
.pkg: 878 D     home = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin [virtualenv/create/pyenv_cfg.py:34]
.pkg: 878 D     implementation = CPython [virtualenv/create/pyenv_cfg.py:34]
.pkg: 878 D     version_info = 3.11.1.final.0 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 878 D     virtualenv = 20.17.1 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 878 D     include-system-site-packages = false [virtualenv/create/pyenv_cfg.py:34]
.pkg: 878 D     base-prefix = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 878 D     base-exec-prefix = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 878 D     base-executable = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 878 D symlink /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 to /Users/hynek/FOSS/attrs/.tox/.pkg/bin/python [virtualenv/util/path/_sync.py:28]
.pkg: 879 D create virtualenv import hook file /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/_virtualenv.pth [virtualenv/create/via_global_ref/api.py:89]
.pkg: 879 D create /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/_virtualenv.py [virtualenv/create/via_global_ref/api.py:92]
.pkg: 879 D ============================== target debug ============================== [virtualenv/run/session.py:50]
.pkg: 880 D debug via /Users/hynek/FOSS/attrs/.tox/.pkg/bin/python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/virtualenv/create/debug.py [virtualenv/create/creator.py:197]
.pkg: 879 D {
  "sys": {
    "executable": "/Users/hynek/FOSS/attrs/.tox/.pkg/bin/python",
    "_base_executable": "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11",
    "prefix": "/Users/hynek/FOSS/attrs/.tox/.pkg",
    "base_prefix": "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11",
    "real_prefix": null,
    "exec_prefix": "/Users/hynek/FOSS/attrs/.tox/.pkg",
    "base_exec_prefix": "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11",
    "path": [
      "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip",
      "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11",
      "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload",
      "/Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages"
    ],
    "meta_path": [
      "<class '_virtualenv._Finder'>",
      "<class '_frozen_importlib.BuiltinImporter'>",
      "<class '_frozen_importlib.FrozenImporter'>",
      "<class '_frozen_importlib_external.PathFinder'>"
    ],
    "fs_encoding": "utf-8",
    "io_encoding": "utf-8"
  },
  "version": "3.11.1 (main, Dec 30 2022, 09:54:37) [Clang 14.0.0 (clang-1400.0.29.202)]",
  "makefile_filename": "/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/config-3.11-darwin/Makefile",
  "os": "<module 'os' (frozen)>",
  "site": "<module 'site' (frozen)>",
  "datetime": "<module 'datetime' from '/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/datetime.py'>",
  "math": "<module 'math' from '/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload/math.cpython-311-darwin.so'>",
  "json": "<module 'json' from '/Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py'>"
} [virtualenv/run/session.py:51]
.pkg: 929 I add seed packages via FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/hynek/Library/Application Support/virtualenv) [virtualenv/run/session.py:55]
.pkg: 933 D got embed update of distribution setuptools from /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/embed/3/setuptools.json [virtualenv/app_data/via_disk_folder.py:129]
.pkg: 934 D got embed update of distribution pip from /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/embed/3/pip.json [virtualenv/app_data/via_disk_folder.py:129]
.pkg: 935 D got embed update of distribution wheel from /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/embed/3/wheel.json [virtualenv/app_data/via_disk_folder.py:129]
.pkg: 935 D install setuptools from wheel /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/virtualenv/seed/wheels/embed/setuptools-65.6.3-py3-none-any.whl via CopyPipInstall [virtualenv/seed/embed/via_app_data/via_app_data.py:47]
.pkg: 935 D install pip from wheel /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/virtualenv/seed/wheels/embed/pip-22.3.1-py3-none-any.whl via CopyPipInstall [virtualenv/seed/embed/via_app_data/via_app_data.py:47]
.pkg: 936 D install wheel from wheel /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/virtualenv/seed/wheels/embed/wheel-0.38.4-py3-none-any.whl via CopyPipInstall [virtualenv/seed/embed/via_app_data/via_app_data.py:47]
.pkg: 937 D copy /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/pip-22.3.1-py3-none-any/pip-22.3.1.virtualenv to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/pip-22.3.1.virtualenv [virtualenv/util/path/_sync.py:36]
.pkg: 937 D copy /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/setuptools-65.6.3.virtualenv to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/setuptools-65.6.3.virtualenv [virtualenv/util/path/_sync.py:36]
.pkg: 938 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/wheel-0.38.4-py3-none-any/wheel to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/wheel [virtualenv/util/path/_sync.py:36]
.pkg: 938 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/pip-22.3.1-py3-none-any/pip to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/pip [virtualenv/util/path/_sync.py:36]
.pkg: 939 D copy /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/distutils-precedence.pth to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/distutils-precedence.pth [virtualenv/util/path/_sync.py:36]
.pkg: 940 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/setuptools-65.6.3.dist-info to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/setuptools-65.6.3.dist-info [virtualenv/util/path/_sync.py:36]
.pkg: 945 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/setuptools to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/setuptools [virtualenv/util/path/_sync.py:36]
.pkg: 953 D copy /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/wheel-0.38.4-py3-none-any/wheel-0.38.4.virtualenv to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/wheel-0.38.4.virtualenv [virtualenv/util/path/_sync.py:36]
.pkg: 954 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/wheel-0.38.4-py3-none-any/wheel-0.38.4.dist-info to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/wheel-0.38.4.dist-info [virtualenv/util/path/_sync.py:36]
.pkg: 961 D generated console scripts wheel3 wheel-3.11 wheel wheel3.11 [virtualenv/seed/embed/via_app_data/pip_install/base.py:41]
.pkg: 1043 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/pkg_resources to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/pkg_resources [virtualenv/util/path/_sync.py:36]
.pkg: 1070 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/setuptools-65.6.3-py3-none-any/_distutils_hack to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/_distutils_hack [virtualenv/util/path/_sync.py:36]
.pkg: 1072 D generated console scripts  [virtualenv/seed/embed/via_app_data/pip_install/base.py:41]
.pkg: 1155 D copy directory /Users/hynek/Library/Application Support/virtualenv/wheel/3.11/image/1/CopyPipInstall/pip-22.3.1-py3-none-any/pip-22.3.1.dist-info to /Users/hynek/FOSS/attrs/.tox/.pkg/lib/python3.11/site-packages/pip-22.3.1.dist-info [virtualenv/util/path/_sync.py:36]
.pkg: 1159 D generated console scripts pip3.11 pip pip-3.11 pip3 [virtualenv/seed/embed/via_app_data/pip_install/base.py:41]
.pkg: 1159 I add activators for Bash, CShell, Fish, Nushell, PowerShell, Python [virtualenv/run/session.py:61]
.pkg: 1162 D write /Users/hynek/FOSS/attrs/.tox/.pkg/pyvenv.cfg [virtualenv/create/pyenv_cfg.py:30]
.pkg: 1162 D    home = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin [virtualenv/create/pyenv_cfg.py:34]
.pkg: 1162 D    implementation = CPython [virtualenv/create/pyenv_cfg.py:34]
.pkg: 1162 D    version_info = 3.11.1.final.0 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 1162 D    virtualenv = 20.17.1 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 1162 D    include-system-site-packages = false [virtualenv/create/pyenv_cfg.py:34]
.pkg: 1162 D    base-prefix = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 1162 D    base-exec-prefix = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 1162 D    base-executable = /Users/hynek/.asdf/installs/python/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 [virtualenv/create/pyenv_cfg.py:34]
.pkg: 1165 W install_requires> python -I -m pip install hatch-fancy-pypi-readme hatchling [tox/tox_env/api.py:427]
Looking in indexes: https://pypi.vm.ag/root/vrmd/+simple/
Collecting hatch-fancy-pypi-readme
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/a29/671f0083ef1e9/hatch_fancy_pypi_readme-22.8.0-py3-none-any.whl (10 kB)
Collecting hatchling
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/d3b/2f89719bf5200/hatchling-1.12.2-py3-none-any.whl (79 kB)
Collecting editables>=0.3
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/ee6/86a8db9f5d91d/editables-0.3-py3-none-any.whl (4.7 kB)
Collecting packaging>=21.3
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/714/ac14496c3e68c/packaging-23.0-py3-none-any.whl (42 kB)
Collecting pathspec>=0.10.1
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/3a6/6eb970cbac598/pathspec-0.11.0-py3-none-any.whl (29 kB)
Collecting pluggy>=1.0.0
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/741/34bbf457f031a/pluggy-1.0.0-py2.py3-none-any.whl (13 kB)
Installing collected packages: pluggy, pathspec, packaging, editables, hatchling, hatch-fancy-pypi-readme
Successfully installed editables-0.3 hatch-fancy-pypi-readme-22.8.0 hatchling-1.12.2 packaging-23.0 pathspec-0.11.0 pluggy-1.0.0
.pkg: 3244 I exit 0 (2.08 seconds) /Users/hynek/FOSS/attrs> python -I -m pip install hatch-fancy-pypi-readme hatchling pid=87825 [tox/execute/api.py:275]
.pkg: 3246 W _optional_hooks> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build [tox/tox_env/api.py:427]
Backend: run command _optional_hooks with args {}
Backend: Wrote response {'return': {'get_requires_for_build_sdist': True, 'prepare_metadata_for_build_wheel': True, 'get_requires_for_build_wheel': True, 'build_editable': True, 'get_requires_for_build_editable': True, 'prepare_metadata_for_build_editable': True}} to /var/folders/7f/5bq0ctcs4tl_7k2721s0ycy80000gn/T/pep517__optional_hooks-sclezwr6.json
.pkg: 3321 I exit None (0.07 seconds) /Users/hynek/FOSS/attrs> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build pid=87829 [tox/execute/api.py:275]
.pkg: 3322 W get_requires_for_build_sdist> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build [tox/tox_env/api.py:427]
Backend: run command get_requires_for_build_sdist with args {'config_settings': None}
Backend: Wrote response {'return': []} to /var/folders/7f/5bq0ctcs4tl_7k2721s0ycy80000gn/T/pep517_get_requires_for_build_sdist-_hak529q.json
.pkg: 3396 I exit None (0.07 seconds) /Users/hynek/FOSS/attrs> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build pid=87829 [tox/execute/api.py:275]
.pkg: 3397 W build_sdist> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build [tox/tox_env/api.py:427]
Backend: run command build_sdist with args {'sdist_directory': '/Users/hynek/FOSS/attrs/.tox/.pkg/dist', 'config_settings': None}
Backend: Wrote response {'return': 'attrs-23.1.0.dev0.tar.gz'} to /var/folders/7f/5bq0ctcs4tl_7k2721s0ycy80000gn/T/pep517_build_sdist-5ftjwttt.json
.pkg: 11979 I exit None (8.58 seconds) /Users/hynek/FOSS/attrs> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build pid=87829 [tox/execute/api.py:275]
.pkg: 11981 D package .tmp/package/5/attrs-23.1.0.dev0.tar.gz links to .pkg/dist/attrs-23.1.0.dev0.tar.gz (/Users/hynek/FOSS/attrs/.tox) [tox/util/file_view.py:36]
py311: 11982 W install_package_deps> python -m pip install --no-compile 'attrs[tests]' 'coverage[toml]>=5.3' 'importlib_metadata; python_version < "3.8"' [tox/tox_env/api.py:427]
Looking in indexes: https://pypi.vm.ag/root/vrmd/+simple/
Ignoring importlib_metadata: markers 'python_version < "3.8"' don't match your environment
Collecting attrs[tests]
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/29e/95c7f6778868d/attrs-22.2.0-py3-none-any.whl (60 kB)
Collecting coverage[toml]>=5.3
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/da9/b41d4539eefd4/coverage-7.1.0-cp311-cp311-macosx_10_9_x86_64.whl (198 kB)
Collecting zope.interface
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/757/9960be23d1fdd/zope.interface-5.5.2-cp311-cp311-macosx_10_9_x86_64.whl (210 kB)
Collecting hypothesis
  Downloading https://pypi.vm.ag/root/pypi/%2Bf/26c/52473a526b316/hypothesis-6.65.2-py3-none-any.whl (402 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 402.5/402.5 kB 8.0 MB/s eta 0:00:00
Collecting pympler
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/d26/0dda9ae781e1e/Pympler-1.0.1-py3-none-any.whl (164 kB)
Collecting pytest>=4.3.0
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/c7c/6ca206e933550/pytest-7.2.1-py3-none-any.whl (317 kB)
Collecting pytest-xdist[psutil]
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/70a/76f191d8a1d2d/pytest_xdist-3.1.0-py3-none-any.whl (36 kB)
Collecting cloudpickle
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/61f/594d1f4c295fa/cloudpickle-2.2.1-py3-none-any.whl (25 kB)
Collecting mypy<0.990,>=0.971
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/102/1c241e8b6e1ca/mypy-0.982-py3-none-any.whl (2.3 MB)
Requirement already satisfied: setuptools in ./.tox/py311/lib/python3.11/site-packages (from zope.interface->attrs[tests]) (65.6.3)
Collecting typing-extensions>=3.10
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/16f/a4864408f655d/typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Collecting mypy-extensions>=0.4.3
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/090/fedd75945a69a/mypy_extensions-0.4.3-py2.py3-none-any.whl (4.5 kB)
Collecting iniconfig
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/b6a/85871a79d2e3b/iniconfig-2.0.0-py3-none-any.whl (5.9 kB)
Collecting packaging
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/714/ac14496c3e68c/packaging-23.0-py3-none-any.whl (42 kB)
Collecting pluggy<2.0,>=0.12
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/741/34bbf457f031a/pluggy-1.0.0-py2.py3-none-any.whl (13 kB)
Collecting sortedcontainers<3.0.0,>=2.1.0
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/a16/3dcaede0f1c02/sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)
Collecting execnet>=1.1
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/a29/5f7cc774947aa/execnet-1.9.0-py2.py3-none-any.whl (39 kB)
Collecting psutil>=3.0
  Using cached https://pypi.vm.ag/root/pypi/%2Bf/54d/5b184728298f2/psutil-5.9.4-cp36-abi3-macosx_10_9_x86_64.whl (243 kB)
Installing collected packages: sortedcontainers, mypy-extensions, zope.interface, typing-extensions, pympler, psutil, pluggy, packaging, iniconfig, execnet, coverage, cloudpickle, attrs, pytest, mypy, hypothesis, pytest-xdist
Successfully installed attrs-22.2.0 cloudpickle-2.2.1 coverage-7.1.0 execnet-1.9.0 hypothesis-6.65.2 iniconfig-2.0.0 mypy-0.982 mypy-extensions-0.4.3 packaging-23.0 pluggy-1.0.0 psutil-5.9.4 pympler-1.0.1 pytest-7.2.1 pytest-xdist-3.1.0 sortedcontainers-2.4.0 typing-extensions-4.4.0 zope.interface-5.5.2
py311: 19493 I exit 0 (7.51 seconds) /Users/hynek/FOSS/attrs> python -m pip install --no-compile 'attrs[tests]' 'coverage[toml]>=5.3' 'importlib_metadata; python_version < "3.8"' pid=87842 [tox/execute/api.py:275]
py311: 19493 W install_package> python -m pip install --no-compile --force-reinstall --no-deps /Users/hynek/FOSS/attrs/.tox/.tmp/package/5/attrs-23.1.0.dev0.tar.gz [tox/tox_env/api.py:427]
Looking in indexes: https://pypi.vm.ag/root/vrmd/+simple/
Processing ./.tox/.tmp/package/5/attrs-23.1.0.dev0.tar.gz
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: attrs
  Building wheel for attrs (pyproject.toml) ... done
  Created wheel for attrs: filename=attrs-23.1.0.dev0-py3-none-any.whl size=60173 sha256=8ee6f9581440f978247d55fda33e71bdfa0bf25c4b4650bf26398f1424e33672
  Stored in directory: /Users/hynek/Library/Caches/pip/wheels/ed/4c/b6/1c31bccca99544d171f08cf9652298d6e87ae113a6e61229df
Successfully built attrs
Installing collected packages: attrs
  Attempting uninstall: attrs
    Found existing installation: attrs 22.2.0
    Uninstalling attrs-22.2.0:
      Successfully uninstalled attrs-22.2.0
Successfully installed attrs-23.1.0.dev0
py311: 24043 I exit 0 (4.55 seconds) /Users/hynek/FOSS/attrs> python -m pip install --no-compile --force-reinstall --no-deps /Users/hynek/FOSS/attrs/.tox/.tmp/package/5/attrs-23.1.0.dev0.tar.gz pid=87855 [tox/execute/api.py:275]
py311: 24044 W commands[0]> coverage run -m pytest -n auto [tox/tox_env/api.py:427]
========================================================================== test session starts ==========================================================================
platform darwin -- Python 3.11.1, pytest-7.2.1, pluggy-1.0.0
cachedir: .tox/py311/.pytest_cache
rootdir: /Users/hynek/FOSS/attrs, configfile: pyproject.toml, testpaths: tests
```

</details>

## Minimal example

In an attrs checkout use this:

```ini
[tox]
envlist = py310
isolated_build = True

[testenv]
extras = tests
commands = pip list
```

Gives you:

```console
$ tox
.pkg: _optional_hooks> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: get_requires_for_build_sdist> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: build_sdist> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build
py310: install_package_deps> python -I -m pip install 'attrs[tests-no-zope]' 'importlib_metadata; python_version < "3.8"' zope.interface
py310: install_package> python -I -m pip install --force-reinstall --no-deps /Users/hynek/FOSS/attrs/.tox/.tmp/package/6/attrs-23.1.0.dev0.tar.gz
py310: commands[0]> pip list
Package             Version
------------------- -----------
attrs               23.1.0.dev0
chevron             0.14.0
cloudpickle         2.2.1
decorator           5.1.1
exceptiongroup      1.1.0
execnet             1.9.0
hypothesis          6.65.2
iniconfig           2.0.0
mypy                0.982 <--- 👎
mypy-extensions     0.4.3
packaging           23.0
pip                 22.3.1
pluggy              1.0.0
psutil              5.9.4
Pympler             1.0.1
pytest              7.2.1
pytest-mypy-plugins 1.10.1
pytest-xdist        3.1.0
PyYAML              6.0
regex               2022.10.31
setuptools          65.6.3
sortedcontainers    2.4.0
tomli               2.0.1
typing_extensions   4.4.0
wheel               0.38.4
zope.interface      5.5.2
```

But:

```ini
[tox]
envlist = py310
isolated_build = True

[testenv]
extras = tests-no-zope 
commands = pip list
```

Gives you:

```console
$ tox 
.pkg: _optional_hooks> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: get_requires_for_build_sdist> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: build_sdist> python /Users/hynek/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True hatchling.build
py310: recreate env because dependencies removed: attrs[tests-no-zope], zope.interface
py310: remove tox env folder /Users/hynek/FOSS/attrs/.tox/py310
py310: install_package_deps> python -I -m pip install 'cloudpickle; platform_python_implementation == "CPython"' hypothesis 'importlib_metadata; python_version < "3.8"' 'mypy>=0.991; platform_python_implementation == "CPython"' pympler 'pytest-mypy-plugins; platform_python_implementation == "CPython" and python_version < "3.11"' 'pytest-xdist[psutil]' 'pytest>=4.3.0'
py310: install_package> python -I -m pip install --force-reinstall --no-deps /Users/hynek/FOSS/attrs/.tox/.tmp/package/8/attrs-23.1.0.dev0.tar.gz
py310: commands[0]> pip list
Package             Version
------------------- -----------
attrs               23.1.0.dev0
chevron             0.14.0
cloudpickle         2.2.1
decorator           5.1.1
exceptiongroup      1.1.0
execnet             1.9.0
hypothesis          6.65.2
iniconfig           2.0.0
mypy                0.991 <-- 👍
mypy-extensions     0.4.3
packaging           23.0
pip                 22.3.1
pluggy              1.0.0
psutil              5.9.4
Pympler             1.0.1
pytest              7.2.1
pytest-mypy-plugins 1.10.1
pytest-xdist        3.1.0
PyYAML              6.0
regex               2022.10.31
setuptools          65.6.3
sortedcontainers    2.4.0
tomli               2.0.1
typing_extensions   4.4.0
wheel               0.38.4
```

Discussion:
masenf: Thought about this implementation a bit and it could get stuck in an infinite loop if there is a circular extras chain. Need up update the code to only visit each extra once.
masenf: Pushed another test case for when the package deps themselves reference an extra, and this currently fails. So I'm going to fix that as well and unify the extra unroll routines in `tox/src/tox/tox_env/python/virtual_env/package/util.py` so both metadata and pyproject can use the same algorithm.

Patch:
diff --git a/docs/changelog/2904.bugfix.rst b/docs/changelog/2904.bugfix.rst
new file mode 100644
index 00000000..39628f2a
--- /dev/null
+++ b/docs/changelog/2904.bugfix.rst
@@ -0,0 +1,3 @@
+Tox will now expand self-referential extras discovered in package deps to respect local modifications to package
+metadata. This allows a package extra to explicitly depend on another package extra, which previously only worked with
+non-static metadata - by :user:`masenf`.
diff --git a/src/tox/tox_env/python/virtual_env/package/pyproject.py b/src/tox/tox_env/python/virtual_env/package/pyproject.py
index 77508732..2e3e61b1 100644
--- a/src/tox/tox_env/python/virtual_env/package/pyproject.py
+++ b/src/tox/tox_env/python/virtual_env/package/pyproject.py
@@ -33,7 +33,7 @@ from tox.tox_env.runner import RunToxEnv
 from tox.util.file_view import create_session_view
 
 from ..api import VirtualEnv
-from .util import dependencies_with_extras
+from .util import dependencies_with_extras, dependencies_with_extras_from_markers
 
 if sys.version_info >= (3, 8):  # pragma: no cover (py38+)
     from importlib.metadata import Distribution, PathDistribution
@@ -253,11 +253,17 @@ class Pep517VirtualEnvPackager(PythonPackageToxEnv, VirtualEnv):
             if dynamic == "dependencies" or (extras and dynamic == "optional-dependencies"):
                 return None  # if any dependencies are dynamic we can just calculate all dynamically
 
-        deps: list[Requirement] = [Requirement(i) for i in project.get("dependencies", [])]
+        deps_with_markers: list[tuple[Requirement, set[str | None]]] = [
+            (Requirement(i), {None}) for i in project.get("dependencies", [])
+        ]
         optional_deps = project.get("optional-dependencies", {})
-        for extra in extras:
-            deps.extend(Requirement(i) for i in optional_deps.get(extra, []))
-        return deps
+        for extra, reqs in optional_deps.items():
+            deps_with_markers.extend((Requirement(req), {extra}) for req in (reqs or []))
+        return dependencies_with_extras_from_markers(
+            deps_with_markers=deps_with_markers,
+            extras=extras,
+            package_name=project.get("name", "."),
+        )
 
     def _load_deps_from_built_metadata(self, for_env: EnvConfigSet) -> list[Requirement]:
         # dependencies might depend on the python environment we're running in => if we build a wheel use that env
diff --git a/src/tox/tox_env/python/virtual_env/package/util.py b/src/tox/tox_env/python/virtual_env/package/util.py
index 948de48c..603f93ed 100644
--- a/src/tox/tox_env/python/virtual_env/package/util.py
+++ b/src/tox/tox_env/python/virtual_env/package/util.py
@@ -8,7 +8,14 @@ from packaging.requirements import Requirement
 
 
 def dependencies_with_extras(deps: list[Requirement], extras: set[str], package_name: str) -> list[Requirement]:
-    deps_with_markers = extract_extra_markers(deps)
+    return dependencies_with_extras_from_markers(extract_extra_markers(deps), extras, package_name)
+
+
+def dependencies_with_extras_from_markers(
+    deps_with_markers: list[tuple[Requirement, set[str | None]]],
+    extras: set[str],
+    package_name: str,
+) -> list[Requirement]:
     result: list[Requirement] = []
     found: set[str] = set()
     todo: set[str | None] = extras | {None}


Test patch:
diff --git a/tests/tox_env/python/virtual_env/package/test_package_pyproject.py b/tests/tox_env/python/virtual_env/package/test_package_pyproject.py
index a8f6c9ed..0d6160a5 100644
--- a/tests/tox_env/python/virtual_env/package/test_package_pyproject.py
+++ b/tests/tox_env/python/virtual_env/package/test_package_pyproject.py
@@ -1,6 +1,7 @@
 from __future__ import annotations
 
 from pathlib import Path
+from textwrap import dedent
 
 import pytest
 
@@ -92,6 +93,57 @@ def test_package_root_via_testenv(tox_project: ToxProjectCreator, demo_pkg_inlin
             ["A"],
             id="deps_with_dynamic_optional_no_extra",
         ),
+        pytest.param(
+            dedent(
+                """
+                [project]
+                name='foo'
+                dependencies=['foo[alpha]']
+                optional-dependencies.alpha=['A']""",
+            ),
+            "",
+            ["A"],
+            id="deps_reference_extra",
+        ),
+        pytest.param(
+            dedent(
+                """
+                [project]
+                name='foo'
+                dependencies=['A']
+                optional-dependencies.alpha=['B']
+                optional-dependencies.beta=['foo[alpha]']""",
+            ),
+            "beta",
+            ["A", "B"],
+            id="deps_with_recursive_extra",
+        ),
+        pytest.param(
+            dedent(
+                """
+                [project]
+                name='foo'
+                dependencies=['A']
+                optional-dependencies.alpha=['B']
+                optional-dependencies.beta=['foo[alpha]']
+                optional-dependencies.delta=['foo[beta]', 'D']""",
+            ),
+            "delta",
+            ["A", "B", "D"],
+            id="deps_with_two_recursive_extra",
+        ),
+        pytest.param(
+            dedent(
+                """
+                [project]
+                name='foo'
+                optional-dependencies.alpha=['foo[beta]', 'A']
+                optional-dependencies.beta=['foo[alpha]', 'B']""",
+            ),
+            "alpha",
+            ["A", "B"],
+            id="deps_with_circular_recursive_extra",
+        ),
     ],
 )
 def test_pyproject_deps_from_static(