Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent __pypackages__ leaking into a PDM subcommand #8

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 26 additions & 2 deletions src/pdm_multirun/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING
import sys
from contextlib import contextmanager
from typing import TYPE_CHECKING, Iterator

from pdm import termui
from pdm.cli.commands.run import Command as RunCommand
Expand All @@ -24,6 +26,27 @@ def _comma_separated_list(value: str) -> list[str]:
return value.split(",")


@contextmanager
def _clean_sys_path(project: Project, options: argparse.Namespace) -> Iterator[None]:
if options.script != "pdm":
yield
return
old_syspath = sys.path
clean = [path for path in old_syspath if "__pypackages__" not in path]
if clean == old_syspath:
yield
return
project.core.ui.echo(
"Temporarily removed `__pypackages__` from `sys.path`",
verbosity=termui.Verbosity.DETAIL,
)
sys.path = clean
try:
yield
finally:
sys.path = old_syspath


class MultirunCommand(RunCommand):
"""Run a command under multiple Python versions."""

Expand Down Expand Up @@ -67,7 +90,8 @@ def handle(self, project: Project, options: argparse.Namespace) -> None: # noqa
project.core.ui.echo(f"Skipped interpreter/venv: {selected}", verbosity=termui.Verbosity.DETAIL)
continue
try:
super().handle(project, options)
with _clean_sys_path(project, options):
super().handle(project, options)
except SystemExit as exit:
if exit.code:
self._use(project, options, old_python)
Expand Down