Skip to content

Commit

Permalink
Pass --proxy to build subprocesses
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ichard26 and martinezlc99 committed Jan 12, 2025
1 parent 39be130 commit 72a5902
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/6018.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ``--proxy`` command-line option is now respected while installing build dependencies.
2 changes: 2 additions & 0 deletions src/pip/_internal/build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/cli/index_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/pip/_internal/index/package_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions tests/functional/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 72a5902

Please sign in to comment.