Skip to content

Commit 717e27f

Browse files
authored
Support fresh subprocess for build backends (#3233)
1 parent 0fd872d commit 717e27f

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

docs/changelog/3227.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support enabling fresh subprocess for packaging build backends via :ref:`fresh_subprocess` - by :user:`gaborbernat`.

docs/config.rst

+10
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,16 @@ Python virtual environment packaging
794794

795795
Config settings (``dict[str, str]``) passed to the ``build_editable`` backend API endpoint.
796796

797+
.. conf::
798+
:keys: fresh_subprocess
799+
:version_added: 4.14.0
800+
:default: False
801+
802+
A flag controlling if each call to the build backend should be done in a fresh subprocess or not (especially older
803+
build backends such as ``setuptools`` might require this to discover newly provisioned dependencies).
804+
805+
Directory where to put the project metadata files.
806+
797807
Pip installer
798808
~~~~~~~~~~~~~
799809

src/tox/tox_env/python/virtual_env/package/pyproject.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ def id() -> str:
117117
@property
118118
def _frontend(self) -> Pep517VirtualEnvFrontend:
119119
if self._frontend_ is None:
120-
self._frontend_ = Pep517VirtualEnvFrontend(self.root, self)
120+
fresh = cast(bool, self.conf["fresh_subprocess"])
121+
self._frontend_ = Pep517VirtualEnvFrontend(self.root, self, fresh_subprocess=fresh)
121122
return self._frontend_
122123

123124
def register_config(self) -> None:
@@ -136,6 +137,12 @@ def register_config(self) -> None:
136137
)
137138
for key in ("sdist", "wheel", "editable"):
138139
self._add_config_settings(key)
140+
self.conf.add_config(
141+
keys=["fresh_subprocess"],
142+
of_type=bool,
143+
default=False,
144+
desc="create a fresh subprocess for every backend request",
145+
)
139146

140147
def _add_config_settings(self, build_type: str) -> None:
141148
# config settings passed to PEP-517-compliant build backend https://peps.python.org/pep-0517/#config-settings
@@ -370,9 +377,10 @@ def id() -> str:
370377

371378

372379
class Pep517VirtualEnvFrontend(Frontend):
373-
def __init__(self, root: Path, env: Pep517VenvPackager) -> None:
380+
def __init__(self, root: Path, env: Pep517VenvPackager, *, fresh_subprocess: bool) -> None:
374381
super().__init__(*Frontend.create_args_from_folder(root))
375382
self._tox_env = env
383+
self._fresh_subprocess = fresh_subprocess
376384
self._backend_executor_: LocalSubProcessPep517Executor | None = None
377385
into: dict[str, Any] = {}
378386

@@ -412,19 +420,23 @@ def _send_msg(
412420
result_file: Path, # noqa: ARG002
413421
msg: str,
414422
) -> Iterator[ToxCmdStatus]:
415-
with self._tox_env.execute_async(
416-
cmd=self.backend_cmd,
417-
cwd=self._root,
418-
stdin=StdinSource.API,
419-
show=None,
420-
run_id=cmd,
421-
executor=self.backend_executor,
422-
) as execute_status:
423-
execute_status.write_stdin(f"{msg}{os.linesep}")
424-
yield ToxCmdStatus(execute_status)
425-
outcome = execute_status.outcome
426-
if outcome is not None: # pragma: no branch
427-
outcome.assert_success()
423+
try:
424+
with self._tox_env.execute_async(
425+
cmd=self.backend_cmd,
426+
cwd=self._root,
427+
stdin=StdinSource.API,
428+
show=None,
429+
run_id=cmd,
430+
executor=self.backend_executor,
431+
) as execute_status:
432+
execute_status.write_stdin(f"{msg}{os.linesep}")
433+
yield ToxCmdStatus(execute_status)
434+
outcome = execute_status.outcome
435+
if outcome is not None: # pragma: no branch
436+
outcome.assert_success()
437+
finally:
438+
if self._fresh_subprocess:
439+
self.backend_executor.close()
428440

429441
def _unexpected_response( # noqa: PLR0913
430442
self,

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,4 @@ extras =
100100
commands =
101101
python -m pip list --format=columns
102102
python -c "print(r'{envpython}')"
103+
uv_seed = true

0 commit comments

Comments
 (0)