Repository: tox-dev/tox

Issue:
[Windows] User site-packages is not added to python path when running test with sitepackages = true
In Windows, the default user site-packages location is `%APPDATA%\Python\PythonXY\site-package`. If `%APPDATA%` doesn't exist, python won't find the user site-packages folder and won't add it to the python path.

When using tox with the `sitepackages = true` option, even though a normal virtual environment would have the user site-packages in the python path, it seems that because tox doesn't add `%APPDATA%` to the subprocess environment, python can't find whatever packages are expected to exist in the user site-packages folder when it is being executed in a toxenv.

This is about the **user** site-packages folder, not the system one.

Discussion:

Patch:
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index f12b86f2..feb454c9 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -92,6 +92,7 @@ Miro Hrončok
 Monty Taylor
 Morgan Fainberg
 Naveen S R
+Niander Assis
 Nick Douma
 Nick Prendergast
 Nicolas Vivet
diff --git a/docs/changelog/2402.bugfix.rst b/docs/changelog/2402.bugfix.rst
new file mode 100644
index 00000000..25f5d724
--- /dev/null
+++ b/docs/changelog/2402.bugfix.rst
@@ -0,0 +1,1 @@
+``sitepackages = true`` will add user's site-package to the python path on Windows as expected -- by :user:`niander`
diff --git a/docs/config.rst b/docs/config.rst
index 2ca82e3a..67bdb66b 100644
--- a/docs/config.rst
+++ b/docs/config.rst
@@ -468,7 +468,7 @@ Complete list of settings that you can put into ``testenv*`` sections:
       ``LD_LIBRARY_PATH``, ``PIP_INDEX_URL``, ``PIP_EXTRA_INDEX_URL``,
       ``REQUESTS_CA_BUNDLE``, ``SSL_CERT_FILE``,
       ``HTTP_PROXY``, ``HTTPS_PROXY``, ``NO_PROXY``
-    * Windows: ``SYSTEMDRIVE``, ``SYSTEMROOT``, ``PATHEXT``, ``TEMP``, ``TMP``
+    * Windows: ``APPDATA``, ``SYSTEMDRIVE``, ``SYSTEMROOT``, ``PATHEXT``, ``TEMP``, ``TMP``
        ``NUMBER_OF_PROCESSORS``, ``USERPROFILE``, ``MSYSTEM``,
        ``PROGRAMFILES``, ``PROGRAMFILES(X86)``, ``PROGRAMDATA``
     * Others (e.g. UNIX, macOS): ``TMPDIR``
diff --git a/src/tox/config/__init__.py b/src/tox/config/__init__.py
index b49c06f6..24f669d3 100644
--- a/src/tox/config/__init__.py
+++ b/src/tox/config/__init__.py
@@ -796,6 +796,7 @@ def tox_addoption(parser):
         # but this leads to very long paths when run with jenkins
         # so we just pass it on by default for now.
         if tox.INFO.IS_WIN:
+            passenv.add("APPDATA")  # needed to find user site-packages location
             passenv.add("SYSTEMDRIVE")  # needed for pip6
             passenv.add("SYSTEMROOT")  # needed for python's crypto module
             passenv.add("PATHEXT")  # needed for discovering executables


Test patch:
diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py
index 3408de02..b1a72b44 100644
--- a/tests/unit/config/test_config.py
+++ b/tests/unit/config/test_config.py
@@ -1508,6 +1508,7 @@ class TestConfigTestEnv:
         assert len(config.envconfigs) == 1
         envconfig = config.envconfigs["python"]
         if plat == "win32":
+            assert "APPDATA" in envconfig.passenv
             assert "PATHEXT" in envconfig.passenv
             assert "SYSTEMDRIVE" in envconfig.passenv
             assert "SYSTEMROOT" in envconfig.passenv
@@ -1560,6 +1561,7 @@ class TestConfigTestEnv:
         assert len(config.envconfigs) == 1
         envconfig = config.envconfigs["python"]
         if plat == "win32":
+            assert "APPDATA" in envconfig.passenv
             assert "PATHEXT" in envconfig.passenv
             assert "SYSTEMDRIVE" in envconfig.passenv
             assert "SYSTEMROOT" in envconfig.passenv