Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog.d/+backend-path-pythonpath.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Keep the caller's ``PYTHONPATH`` importable when ``SubprocessFrontend`` starts a backend that sets
``build-system.backend-path``; the in-tree paths now come first instead of replacing it.
3 changes: 2 additions & 1 deletion src/pyproject_api/_via_fresh_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def __init__(
def _send_msg(self, cmd: str, result_file: Path, msg: str) -> Generator[SubprocessCmdStatus, None, None]: # ruff:ignore[unused-method-argument] # mirrors the Frontend._send_msg signature
env = os.environ.copy()
if backend := os.pathsep.join(str(i) for i in self._backend_paths):
env["PYTHONPATH"] = backend
# backend-path goes first (PEP 517), but what the caller already put on PYTHONPATH must stay importable
env["PYTHONPATH"] = os.pathsep.join(filter(None, (backend, env.get("PYTHONPATH"))))
process = Popen( # ruff:ignore[subprocess-without-shell-equals-true] # fixed argv, no shell
args=[self.executable, *self.backend_args],
stdout=PIPE,
Expand Down
28 changes: 28 additions & 0 deletions tests/pyproject_api/test_via_fresh_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,31 @@ def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
# A failed cleanup test must not leave a live child process.
process.kill()
process.wait(timeout=10)


def test_backend_path_keeps_existing_pythonpath(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
deps: Final = tmp_path / "deps"
deps.mkdir()
(deps / "helper.py").write_text('NAME = "demo-1.0.tar.gz"\n', encoding="utf-8")
# same name as the in-tree backend, so it would shadow it if PYTHONPATH came before backend-path
(deps / "build_tester.py").write_text('raise ImportError("PYTHONPATH shadowed backend-path")\n', encoding="utf-8")
project: Final = tmp_path / "project"
(project / "_build").mkdir(parents=True)
(project / "pyproject.toml").write_text(
'[build-system]\nrequires=[]\nbuild-backend = "build_tester"\nbackend-path=["_build"]', encoding="utf-8"
)
(project / "_build" / "build_tester.py").write_text(
dedent("""\
import helper

def build_sdist(sdist_directory, config_settings=None):
return helper.NAME
"""),
encoding="utf-8",
)
monkeypatch.setenv("PYTHONPATH", str(deps))

frontend: Final = SubprocessFrontend(*SubprocessFrontend.create_args_from_folder(project)[:-1])
result: Final = frontend.build_sdist(tmp_path / "dist")

assert result.sdist == tmp_path / "dist" / "demo-1.0.tar.gz"
Loading