diff --git a/news/3743.bugfix.md b/news/3743.bugfix.md new file mode 100644 index 0000000000..950d8f5c48 --- /dev/null +++ b/news/3743.bugfix.md @@ -0,0 +1 @@ +Clear build directory cache in pytest fixture to avoid stale references. diff --git a/news/3744.bugfix.md b/news/3744.bugfix.md new file mode 100644 index 0000000000..d67bbc071c --- /dev/null +++ b/news/3744.bugfix.md @@ -0,0 +1 @@ +Handle `Version._key` assignment for packaging compatibility. diff --git a/news/3746.bugfix.md b/news/3746.bugfix.md new file mode 100644 index 0000000000..a8d302862e --- /dev/null +++ b/news/3746.bugfix.md @@ -0,0 +1 @@ +Don't inherit `PYTHONPATH` environment variable from parent process when running `pdm run`. diff --git a/src/pdm/cli/commands/run.py b/src/pdm/cli/commands/run.py index 234e6fcee7..d7abdd5c03 100644 --- a/src/pdm/cli/commands/run.py +++ b/src/pdm/cli/commands/run.py @@ -243,6 +243,7 @@ def _run_process( check_project_file(project) project_env = project.environment this_path = project_env.get_paths()["scripts"] + os.environ.pop("PYTHONPATH", None) # Don't inherit PYTHONPATH from the parent process os.environ.update(project_env.process_env) if env_file is not None: if isinstance(env_file, str): diff --git a/src/pdm/environments/local.py b/src/pdm/environments/local.py index ef6e4908ce..99611c7ac0 100644 --- a/src/pdm/environments/local.py +++ b/src/pdm/environments/local.py @@ -85,10 +85,10 @@ def process_env(self) -> dict[str, str]: from pdm.cli.utils import get_pep582_path env = super().process_env - pythonpath = os.getenv("PYTHONPATH", "").split(os.pathsep) - pythonpath = [get_pep582_path(self.project)] + [p for p in pythonpath if "/pep582" not in p.replace("\\", "/")] - env["PYTHONPATH"] = os.pathsep.join(pythonpath) - env["PEP582_PACKAGES"] = str(self.packages_path) + env.update( + PYTHONPATH=get_pep582_path(self.project), + PEP582_PACKAGES=str(self.packages_path), + ) return env @cached_property diff --git a/tests/cli/test_run.py b/tests/cli/test_run.py index 6cea897b95..5111b95975 100644 --- a/tests/cli/test_run.py +++ b/tests/cli/test_run.py @@ -546,26 +546,23 @@ def test_run_with_another_project_root(project, pdm, capfd, explicit_python): assert out.strip() == "1" -def test_import_another_sitecustomize(project, pdm, capfd): - project.pyproject.metadata["requires-python"] = ">=2.7" - project.pyproject.write() - # a script for checking another sitecustomize is imported - project.root.joinpath("foo.py").write_text("import os;print(os.getenv('FOO'))") - # ensure there have at least one sitecustomize can be imported - # there may have more than one sitecustomize.py in sys.path - project.root.joinpath("sitecustomize.py").write_text("import os;os.environ['FOO'] = 'foo'") - env = os.environ.copy() - paths = env.get("PYTHONPATH") - this_path = str(project.root) - new_paths = [this_path] if not paths else [this_path, paths] - env["PYTHONPATH"] = os.pathsep.join(new_paths) - project._environment = None +def test_run_does_not_inherit_parent_pythonpath(project, pdm, capfd): + inherited_path = project.root / "inherited-pythonpath" + inherited_path.mkdir() + inherited_path.joinpath("sitecustomize.py").write_text("import os;os.environ['PDM_RUN_PYTHONPATH_MARKER'] = '1'") + check_cmd = "import os;print(os.getenv('PDM_RUN_PYTHONPATH_MARKER'));print(os.getenv('PYTHONPATH'))" capfd.readouterr() - with cd(project.root): - result = pdm(["run", "python", "foo.py"], env=env) + result = pdm( + ["run", "python", "-c", check_cmd], + obj=project, + env={"PYTHONPATH": str(inherited_path)}, + ) assert result.exit_code == 0, result.stderr out, _ = capfd.readouterr() - assert out.strip() == "foo" + marker, pythonpath = out.strip().splitlines() + assert marker == "None" + assert pythonpath == get_pep582_path(project) + assert str(inherited_path) not in pythonpath def test_run_with_patched_sysconfig(project, pdm, capfd):