Repository: tox-dev/tox

Issue:
Parallel mode with -e ALL fails
Using ``-p`` together with ``-e ALL`` runs **all environments in all subprocesses**. Using ``-e <list,of,all,envs>`` on the other hand works as expected (each subprocess runs one environment).

Minimal example:

``tox.ini``

```ini
[tox]
envlist = py

[testenv]
deps = requests
skip_install = true
commands = python -V

[testenv:lint]
commands = python -c "print('Hello lint')"

[testenv:dev]
commands = python -c "print('Hello dev')"
```

Environment:

```bash
$ tox --version
3.24.2 imported from ~\.local\pipx\venvs\tox\lib\site-packages\tox\__init__.py
$ ~/.local/pipx/venvs/tox/Scripts/python -V
Python 3.9.6
```

Correct command:

```bash
$ tox -p -o -e py,lint,dev
py create: ~\Dev\tmp\test_tox\.tox\py
dev create: ~\Dev\tmp\test_tox\.tox\dev
lint create: ~\Dev\tmp\test_tox\.tox\lint
py installdeps: requests
dev installdeps: requests
lint installdeps: requests
py installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
py run-test-pre: PYTHONHASHSEED='937'
py run-test: commands[0] | python -V
dev installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
dev run-test-pre: PYTHONHASHSEED='633'
dev run-test: commands[0] | python -c 'print('"'"'Hello dev'"'"')'
lint installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
lint run-test-pre: PYTHONHASHSEED='783'
lint run-test: commands[0] | python -c 'print('"'"'Hello lint'"'"')'
Python 3.9.7
✔ OK py in 8.858 seconds
Hello dev
Hello lint
✔ OK dev in 8.929 seconds
✔ OK lint in 8.936 seconds
____________________________________________________________ summary ____________________________________________________________
  py: commands succeeded
  lint: commands succeeded
  dev: commands succeeded
  congratulations :)
```

Incorrect behaviour:

```bash
$ tox -p -o -e ALL
py installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
py run-test-pre: PYTHONHASHSEED='672'
py run-test: commands[0] | python -V
py installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
py run-test-pre: PYTHONHASHSEED='524'
py run-test: commands[0] | python -V
py installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
py run-test-pre: PYTHONHASHSEED='977'
py run-test: commands[0] | python -V
Python 3.9.7
Python 3.9.7
Python 3.9.7
lint installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
lint installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
lint run-test-pre: PYTHONHASHSEED='524'
lint run-test: commands[0] | python -c 'print('"'"'Hello lint'"'"')'
lint run-test-pre: PYTHONHASHSEED='672'
lint run-test: commands[0] | python -c 'print('"'"'Hello lint'"'"')'
lint installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
lint run-test-pre: PYTHONHASHSEED='977'
lint run-test: commands[0] | python -c 'print('"'"'Hello lint'"'"')'
Hello lint
Hello lint
Hello lint
dev installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
dev run-test-pre: PYTHONHASHSEED='524'
dev run-test: commands[0] | python -c 'print('"'"'Hello dev'"'"')'
dev installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
dev run-test-pre: PYTHONHASHSEED='672'
dev run-test: commands[0] | python -c 'print('"'"'Hello dev'"'"')'
dev installed: certifi==2021.5.30,charset-normalizer==2.0.4,idna==3.2,requests==2.26.0,urllib3==1.26.6
dev run-test-pre: PYTHONHASHSEED='977'
dev run-test: commands[0] | python -c 'print('"'"'Hello dev'"'"')'
Hello dev
Hello dev
Hello dev
✔ OK dev in 5.777 seconds
✔ OK py in 5.802 seconds
✔ OK lint in 5.812 seconds
____________________________________________________________ summary ____________________________________________________________
  py: commands succeeded
  lint: commands succeeded
  dev: commands succeeded
  congratulations :)
```

Please note, that in the incorrect case, all three echos happen three times.

The issues comes from https://github.com/tox-dev/tox/blob/156d4961dd50ee629c3c4c78c09ba6b1c0da76d3/src/tox/config/__init__.py#L1501, which causes https://github.com/tox-dev/tox/blob/156d4961dd50ee629c3c4c78c09ba6b1c0da76d3/src/tox/config/__init__.py#L1507 to not correctly restrict the ``env_list`` to the environment given by ``os.environ.get(PARALLEL_ENV_VAR_KEY_PRIVATE)``.

Discussion:
guahki: As

1. this is my first contribution to this project and
1. I'm not sure about the correct way to fix this

this is kind of a draft/request PR. I will happily create appropriate changelog when you tell me, this is a sensible way of fixing the problem.

Thanks for the awesome ``tox`` tool!
guahki: I added the changelog.

Unfortunately I do not have the time to write a test right now. I would be happy, if anybody can assist with this.
guahki: I had a look into it and it wasn't that hard indeed.
Thanks for your patience!

Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 49c5fdc5..ed1077bf 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -14,7 +14,7 @@ repos:
       - id: end-of-file-fixer
       - id: trailing-whitespace
   - repo: https://github.com/asottile/pyupgrade
-    rev: v2.25.0
+    rev: v2.26.0
     hooks:
       - id: pyupgrade
   - repo: https://github.com/PyCQA/isort
diff --git a/docs/changelog/2167.bugfix.rst b/docs/changelog/2167.bugfix.rst
new file mode 100644
index 00000000..1d280247
--- /dev/null
+++ b/docs/changelog/2167.bugfix.rst
@@ -0,0 +1,1 @@
+Fixed handling of ``-e ALL`` in parallel mode by ignoring the ``ALL`` in subprocesses -- by :user:`guahki`.
diff --git a/src/tox/config/__init__.py b/src/tox/config/__init__.py
index 54dbf0bd..b51c8633 100644
--- a/src/tox/config/__init__.py
+++ b/src/tox/config/__init__.py
@@ -1498,9 +1498,10 @@ class ParseIni(object):
 
         env_list = []
         envlist_explicit = False
-        if (from_option and "ALL" in from_option) or (
-            not from_option and from_environ and "ALL" in from_environ.split(",")
-        ):
+        if (
+            (from_option and "ALL" in from_option)
+            or (not from_option and from_environ and "ALL" in from_environ.split(","))
+        ) and PARALLEL_ENV_VAR_KEY_PRIVATE not in os.environ:
             all_envs = self._getallenvs(reader)
         else:
             candidates = (


Test patch:
diff --git a/tests/unit/config/test_config_parallel.py b/tests/unit/config/test_config_parallel.py
index 0e42a2c5..f070ca80 100644
--- a/tests/unit/config/test_config_parallel.py
+++ b/tests/unit/config/test_config_parallel.py
@@ -1,5 +1,7 @@
 import pytest
 
+from tox.config.parallel import ENV_VAR_KEY_PRIVATE as PARALLEL_ENV_VAR_KEY_PRIVATE
+
 
 def test_parallel_default(newconfig):
     config = newconfig([], "")
@@ -70,3 +72,17 @@ def test_depends_factor(newconfig):
         """,
     )
     assert config.envconfigs["py"].depends == ("py37-cov", "py37-no", "py36-cov", "py36-no")
+
+
+def test_parallel_env_selection_with_ALL(newconfig, monkeypatch):
+    # Regression test for #2167
+    inisource = """
+        [tox]
+        envlist = py,lint
+    """
+    monkeypatch.setenv(PARALLEL_ENV_VAR_KEY_PRIVATE, "py")
+    config = newconfig(["-eALL"], inisource)
+    assert config.envlist == ["py"]
+    monkeypatch.setenv(PARALLEL_ENV_VAR_KEY_PRIVATE, "lint")
+    config = newconfig(["-eALL"], inisource)
+    assert config.envlist == ["lint"]