From dd58dfcd6902371855c59ebfe934a620d70fb3ef Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Fri, 7 Jun 2024 16:24:46 -0500 Subject: [PATCH] Override sys.prefix only if it hasn't been changed The purpose of the sitecustomize here is to make setuptools install stuff to our prefix instead of the global prefix, and to appear enough like a virtual environment that platform-specific patches to sysconfig don't interfere with that effort. It isn't inconceivable that there may be subprocesses involved in the build, and we want any installation happening in those subprocesses to also get the redirect, and that currently works as expected as long as the sitecustomize directory remains on PYTHONPATH somewhere. However, if some subprocess is also modifying sys.path, as would be the case if a subprocess was using a new virtual environment, we don't want to override those changes. Since we know what sys.prefix was when colcon was invoked, we can just check to see if it changed. --- colcon_core/task/python/build.py | 1 + colcon_core/task/python/template/sitecustomize.py.em | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/colcon_core/task/python/build.py b/colcon_core/task/python/build.py index a72c1a54..5320a939 100644 --- a/colcon_core/task/python/build.py +++ b/colcon_core/task/python/build.py @@ -70,6 +70,7 @@ async def build(self, *, additional_hooks=None): # noqa: D102 Path(__file__).parent / 'template' / 'sitecustomize.py.em', prefix_override / 'sitecustomize.py', { + 'current_prefix': sys.prefix, 'site_prefix': args.install_base, }) diff --git a/colcon_core/task/python/template/sitecustomize.py.em b/colcon_core/task/python/template/sitecustomize.py.em index bbcd662c..b6bbe2f0 100644 --- a/colcon_core/task/python/template/sitecustomize.py.em +++ b/colcon_core/task/python/template/sitecustomize.py.em @@ -1,4 +1,4 @@ import sys -if sys.prefix == sys.base_prefix: +if sys.prefix == @repr(current_prefix): sys.real_prefix = sys.prefix sys.prefix = sys.exec_prefix = @repr(site_prefix)