Repository: pypa/pip

Issue:
python subprocess invocation to inject setuptools pollutes setuptools argument list
setuptools (and probably distutils) assume the very first argument from sys.argv to be the executed setup file. Invoking a python subprocess with -c to execute a command will set '-c' to be the very first argument to sys.argv (see the docs). This leads to the manifest_maker warning noted at https://github.com/pypa/pip/blob/9b11ed231cc0c7a8101f1ffbdc8b352a12c511b7/pip/req/req_install.py#L868 and also will cause arguments to the setuptools (or distutils) commands to be polluted with an additional '-c', whether it's wanted or not.

As a workaround, manipulate the argument list prior to exec() in https://github.com/pypa/pip/blob/9b11ed231cc0c7a8101f1ffbdc8b352a12c511b7/pip/req/req_install.py#L843 and similar invocations within pip:

```
import sys, setuptools, tokenize
sys.argv[0] = %r; ....
```



Discussion:
For more details, see our review and diff here: https://phabric.freebsd.org/D270

The diff above is no longer available, however this should be pretty easy to fix. It should just require modifying the shim code so that it munges ``sys.argv`` to remove the ``-c`` and such from it.

Resolving patch:
diff --git a/src/pip/_internal/req/req_install.py b/src/pip/_internal/req/req_install.py
--- a/src/pip/_internal/req/req_install.py
+++ b/src/pip/_internal/req/req_install.py
@@ -595,7 +595,7 @@ def run_egg_info(self):
                 'Running setup.py (path:%s) egg_info for package from %s',
                 self.setup_py_path, self.link,
             )
-        script = SETUPTOOLS_SHIM % self.setup_py_path
+        script = SETUPTOOLS_SHIM.format(self.setup_py_path)
         base_cmd = [sys.executable, '-c', script]
         if self.isolated:
             base_cmd += ["--no-user-cfg"]
@@ -757,7 +757,7 @@ def install_editable(
                     [
                         sys.executable,
                         '-c',
-                        SETUPTOOLS_SHIM % self.setup_py_path
+                        SETUPTOOLS_SHIM.format(self.setup_py_path)
                     ] +
                     list(global_options) +
                     ['develop', '--no-deps'] +
@@ -1004,7 +1004,7 @@ def get_install_args(
         # type: (...) -> List[str]
         install_args = [sys.executable, "-u"]
         install_args.append('-c')
-        install_args.append(SETUPTOOLS_SHIM % self.setup_py_path)
+        install_args.append(SETUPTOOLS_SHIM.format(self.setup_py_path))
         install_args += list(global_options) + \
             ['install', '--record', record_filename]
         install_args += ['--single-version-externally-managed']
diff --git a/src/pip/_internal/utils/setuptools_build.py b/src/pip/_internal/utils/setuptools_build.py
--- a/src/pip/_internal/utils/setuptools_build.py
+++ b/src/pip/_internal/utils/setuptools_build.py
@@ -1,6 +1,11 @@
 # Shim to wrap setup.py invocation with setuptools
+#
+# We set sys.argv[0] to the path to the underlying setup.py file so
+# setuptools / distutils don't take the path to the setup.py to be "-c" when
+# invoking via the shim.  This avoids e.g. the following manifest_maker
+# warning: "warning: manifest_maker: standard file '-c' not found".
 SETUPTOOLS_SHIM = (
-    "import setuptools, tokenize;__file__=%r;"
+    "import sys, setuptools, tokenize; sys.argv[0] = {0!r}; __file__={0!r};"
     "f=getattr(tokenize, 'open', open)(__file__);"
     "code=f.read().replace('\\r\\n', '\\n');"
     "f.close();"
diff --git a/src/pip/_internal/wheel.py b/src/pip/_internal/wheel.py
--- a/src/pip/_internal/wheel.py
+++ b/src/pip/_internal/wheel.py
@@ -930,7 +930,7 @@ def _base_setup_args(self, req):
         # virtualenv.
         return [
             sys.executable, '-u', '-c',
-            SETUPTOOLS_SHIM % req.setup_py_path,
+            SETUPTOOLS_SHIM.format(req.setup_py_path)
         ] + list(self.global_options)
 
     def _build_one_pep517(self, req, tempd, python_tag=None):


Test patch: