From 72a59020b5b4cb0b0ea5e3fbb250a176f1d7d078 Mon Sep 17 00:00:00 2001 From: Richard Si Date: Sun, 22 Dec 2024 18:04:53 -0500 Subject: [PATCH] Pass --proxy to build subprocesses Similar to --cert and --client-cert, the --proxy flag was not passed down to the isolated build environment. This was simply an oversight. I opted to store the original proxy string in a new attribute on the session as digging into the .proxies dictionary felt janky, and so did passing the proxy string to the finder as an argument. Co-authored-by: lcmartin --- news/6018.bugfix.rst | 1 + src/pip/_internal/build_env.py | 2 ++ src/pip/_internal/cli/index_command.py | 1 + src/pip/_internal/index/package_finder.py | 4 ++++ src/pip/_internal/network/session.py | 1 + tests/functional/test_proxy.py | 16 ++++++++++++++++ 6 files changed, 25 insertions(+) create mode 100644 news/6018.bugfix.rst diff --git a/news/6018.bugfix.rst b/news/6018.bugfix.rst new file mode 100644 index 00000000000..0171b5bbc3b --- /dev/null +++ b/news/6018.bugfix.rst @@ -0,0 +1 @@ +The ``--proxy`` command-line option is now respected while installing build dependencies. diff --git a/src/pip/_internal/build_env.py b/src/pip/_internal/build_env.py index b2cc6682291..e820dc3d5fb 100644 --- a/src/pip/_internal/build_env.py +++ b/src/pip/_internal/build_env.py @@ -272,6 +272,8 @@ def _install_requirements( for link in finder.find_links: args.extend(["--find-links", link]) + if finder.proxy: + args.extend(["--proxy", finder.proxy]) for host in finder.trusted_hosts: args.extend(["--trusted-host", host]) if finder.client_cert: diff --git a/src/pip/_internal/cli/index_command.py b/src/pip/_internal/cli/index_command.py index db105d0fef9..295108ed605 100644 --- a/src/pip/_internal/cli/index_command.py +++ b/src/pip/_internal/cli/index_command.py @@ -123,6 +123,7 @@ def _build_session( "https": options.proxy, } session.trust_env = False + session.pip_proxy = options.proxy # Determine if we can prompt the user for authentication or not session.auth.prompting = not options.no_input diff --git a/src/pip/_internal/index/package_finder.py b/src/pip/_internal/index/package_finder.py index 86efb9adac0..c10103320e3 100644 --- a/src/pip/_internal/index/package_finder.py +++ b/src/pip/_internal/index/package_finder.py @@ -661,6 +661,10 @@ def find_links(self) -> List[str]: def index_urls(self) -> List[str]: return self.search_scope.index_urls + @property + def proxy(self) -> Optional[str]: + return self._link_collector.session.pip_proxy + @property def trusted_hosts(self) -> Iterable[str]: for host_port in self._link_collector.session.pip_trusted_origins: diff --git a/src/pip/_internal/network/session.py b/src/pip/_internal/network/session.py index 1765b4f6bd7..5e10f8f5615 100644 --- a/src/pip/_internal/network/session.py +++ b/src/pip/_internal/network/session.py @@ -339,6 +339,7 @@ def __init__( # Namespace the attribute with "pip_" just in case to prevent # possible conflicts with the base class. self.pip_trusted_origins: List[Tuple[str, Optional[int]]] = [] + self.pip_proxy = None # Attach our User Agent to the request self.headers["User-Agent"] = user_agent() diff --git a/tests/functional/test_proxy.py b/tests/functional/test_proxy.py index ab53637900f..52f7ea55d52 100644 --- a/tests/functional/test_proxy.py +++ b/tests/functional/test_proxy.py @@ -90,3 +90,19 @@ def test_proxy_does_not_override_netrc( "simple", ) script.assert_installed(simple="3.0") + + +@pytest.mark.network +def test_build_deps_use_proxy_from_cli( + script: PipTestEnvironment, capfd: pytest.CaptureFixture[str], data: TestData +) -> None: + args = ["wheel", "-v", str(data.packages / "pep517_setup_and_pyproject")] + args.extend(["--proxy", "http://127.0.0.1:9000"]) + + with proxy.Proxy(port=9000, num_acceptors=1, plugins=[AccessLogPlugin]): + result = script.pip(*args) + + wheel_path = script.scratch / "pep517_setup_and_pyproject-1.0-py3-none-any.whl" + result.did_create(wheel_path) + access_log, _ = capfd.readouterr() + assert "CONNECT" in access_log, "setuptools was not fetched using proxy"