Repository: readthedocs/readthedocs.org

Issue:
Empty arguments not supported w/ python --mvirtualenv /dest/
## Details

Firstly, I have a patch that's tested and works and will submit a PR soon :innocent: 

In relation a set of specific deployment needs, I found it necessary to try out Read The Docs on a newer platform (Python 3.7), and I think it causes the following breakage:

![image](https://user-images.githubusercontent.com/374612/88542411-aab3f600-d016-11ea-9c94-74c34fa8e17d.png)

* Read the Docs project URL: n/a
* Build URL (if applicable): n/a
* Read the Docs username (if applicable): n/a

The issue is also reported 14 days ago here:

https://stackoverflow.com/questions/62050977/readthedocs-virtualenv-error-unrecognized-arguments-during-update-repos

It was also reported in #6915 but closed for the wrong reasons (it's not due to an environment variable or similar)

## Reproducing

If there is no option `--system-site-packages` passed to `python3 -mvirtualenv`, it will be invoked like this, resulting in an error:

```
(venv) readthedocs@6d51bf78d765:~/git$ python3.7 -mvirtualenv "" /usr/share/readthedocs/git/user_builds/myproject/envs/latest
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--app-data APP_DATA] [--clear-app-data] [--discovery {builtin}] [-p py] [--creator {builtin,cpython3-posix,venv}] [--seeder {app-data,pip}] [--no-seed]
                  [--activators comma_sep_list] [--clear] [--system-site-packages] [--symlinks | --copies] [--no-download | --download] [--extra-search-dir d [d ...]] [--pip version] [--setuptools version] [--wheel version] [--no-pip]
                  [--no-setuptools] [--no-wheel] [--symlink-app-data] [--prompt prompt] [-h]
                  dest
virtualenv: error: unrecognized arguments: /usr/share/readthedocs/git/user_builds/myproject/envs/latest
```

## Expected Result

I'm pretty sure this is not reproducible for the core team's Docker containers. But you'll probably see it once the Python environments are updated from Python 3.6 to Python 3.7.

## Actual Result

My environment fails executing because of an empty-string command line argument.

Empty arguments not supported w/ python --mvirtualenv /dest/
## Details

Firstly, I have a patch that's tested and works and will submit a PR soon :innocent: 

In relation a set of specific deployment needs, I found it necessary to try out Read The Docs on a newer platform (Python 3.7), and I think it causes the following breakage:

![image](https://user-images.githubusercontent.com/374612/88542411-aab3f600-d016-11ea-9c94-74c34fa8e17d.png)

* Read the Docs project URL: n/a
* Build URL (if applicable): n/a
* Read the Docs username (if applicable): n/a

The issue is also reported 14 days ago here:

https://stackoverflow.com/questions/62050977/readthedocs-virtualenv-error-unrecognized-arguments-during-update-repos

It was also reported in #6915 but closed for the wrong reasons (it's not due to an environment variable or similar)

## Reproducing

If there is no option `--system-site-packages` passed to `python3 -mvirtualenv`, it will be invoked like this, resulting in an error:

```
(venv) readthedocs@6d51bf78d765:~/git$ python3.7 -mvirtualenv "" /usr/share/readthedocs/git/user_builds/myproject/envs/latest
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--app-data APP_DATA] [--clear-app-data] [--discovery {builtin}] [-p py] [--creator {builtin,cpython3-posix,venv}] [--seeder {app-data,pip}] [--no-seed]
                  [--activators comma_sep_list] [--clear] [--system-site-packages] [--symlinks | --copies] [--no-download | --download] [--extra-search-dir d [d ...]] [--pip version] [--setuptools version] [--wheel version] [--no-pip]
                  [--no-setuptools] [--no-wheel] [--symlink-app-data] [--prompt prompt] [-h]
                  dest
virtualenv: error: unrecognized arguments: /usr/share/readthedocs/git/user_builds/myproject/envs/latest
```

## Expected Result

I'm pretty sure this is not reproducible for the core team's Docker containers. But you'll probably see it once the Python environments are updated from Python 3.6 to Python 3.7.

## Actual Result

My environment fails executing because of an empty-string command line argument.



Discussion:
I think this is the same as https://github.com/readthedocs/readthedocs.org/issues/6893#issuecomment-624104344
Good point, thanks @stsewd, I'll try to address this separately from pinning virtualenv to the same version on both environments, as https://github.com/readthedocs/readthedocs.org/issues/6893#issuecomment-624104344 mentions (but sounds like a really good idea, too!)
I think this is the same as https://github.com/readthedocs/readthedocs.org/issues/6893#issuecomment-624104344
Good point, thanks @stsewd, I'll try to address this separately from pinning virtualenv to the same version on both environments, as https://github.com/readthedocs/readthedocs.org/issues/6893#issuecomment-624104344 mentions (but sounds like a really good idea, too!)

Resolving patch:
diff --git a/readthedocs/doc_builder/python_environments.py b/readthedocs/doc_builder/python_environments.py
--- a/readthedocs/doc_builder/python_environments.py
+++ b/readthedocs/doc_builder/python_environments.py
@@ -295,19 +295,31 @@ def venv_path(self):
         return os.path.join(self.project.doc_path, 'envs', self.version.slug)
 
     def setup_base(self):
-        site_packages = ''
+        """
+        Create a virtualenv, invoking ``python -mvirtualenv``.
+
+        .. note::
+
+            ``--no-download`` was removed because of the pip breakage,
+            it was sometimes installing pip 20.0 which broke everything
+            https://github.com/readthedocs/readthedocs.org/issues/6585
+
+            Important not to add empty string arguments, see:
+            https://github.com/readthedocs/readthedocs.org/issues/7322
+        """
+        cli_args = [
+            '-mvirtualenv',
+        ]
         if self.config.python.use_system_site_packages:
-            site_packages = '--system-site-packages'
-        env_path = self.venv_path()
+            cli_args.append('--system-site-packages')
+
+        # Append the positional destination argument
+        cli_args.append(
+            self.venv_path(),
+        )
         self.build_env.run(
             self.config.python_interpreter,
-            '-mvirtualenv',
-            site_packages,
-            # This is removed because of the pip breakage,
-            # it was sometimes installing pip 20.0 which broke everything
-            # https://github.com/readthedocs/readthedocs.org/issues/6585
-            # '--no-download',
-            env_path,
+            *cli_args,
             # Don't use virtualenv bin that doesn't exist yet
             bin_path=None,
             # Don't use the project's root, some config files can interfere


Test patch: