From 7668eb41c0849bf5be01f4be3ec7410779695e08 Mon Sep 17 00:00:00 2001 From: Matthijs van der Burgh Date: Thu, 12 Feb 2026 13:57:10 +0100 Subject: [PATCH 1/2] Improve primary shell heuristics --- colcon_powershell/shell/powershell.py | 17 ++++++++++++++++- setup.cfg | 1 + stdeb.cfg | 2 +- test/spell_check.words | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/colcon_powershell/shell/powershell.py b/colcon_powershell/shell/powershell.py index b65ba11..bf24f7b 100644 --- a/colcon_powershell/shell/powershell.py +++ b/colcon_powershell/shell/powershell.py @@ -15,6 +15,7 @@ from colcon_core.shell import logger from colcon_core.shell import ShellExtensionPoint from colcon_core.shell.template import expand_template +import psutil """Environment variable to override the CMake executable""" POWERSHELL_COMMAND_ENVIRONMENT_VARIABLE = EnvironmentVariable( @@ -44,6 +45,20 @@ def which_executable(environment_variable, executable_name): POWERSHELL_COMMAND_ENVIRONMENT_VARIABLE.name, powershell_executable_name) +def parent_process_is_powershell(): + """Determine if the parent process is PowerShell. + + In case the executable name is not found, + it is assumed that the parent process is not PowerShell. + """ + parent = psutil.Process().parent() + if parent is None: + return False + + parent_name = parent.name() + return parent_name == powershell_executable_name + + class PowerShellExtension(ShellExtensionPoint): """Generate `.ps1` scripts to extend the environment.""" @@ -66,7 +81,7 @@ def __init__(self): # noqa: D107 pathexts = os.environ.get('PATHEXT', '').lower().split(os.pathsep) self._is_primary = '.cpl' in pathexts else: - self._is_primary = bool(os.environ.get('PSModulePath')) + self._is_primary = parent_process_is_powershell() if not POWERSHELL_EXECUTABLE: self._is_primary = False diff --git a/setup.cfg b/setup.cfg index 630c8de..7796c9e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,6 +27,7 @@ keywords = colcon python_requires = >=3.6 install_requires = colcon-core>=0.12.0 + psutil>=5.6.0 packages = find: zip_safe = false diff --git a/stdeb.cfg b/stdeb.cfg index 19167c2..63d917e 100644 --- a/stdeb.cfg +++ b/stdeb.cfg @@ -1,6 +1,6 @@ [colcon-powershell] No-Python2: -Depends3: python3-colcon-core (>= 0.12.0) +Depends3: python3-colcon-core (>= 0.12.0), python3-psutil (>= 5.6.0) Suite: focal jammy noble resolute bookworm trixie X-Python3-Version: >= 3.6 Upstream-Version-Suffix: +upstream diff --git a/test/spell_check.words b/test/spell_check.words index e535729..9cd1237 100644 --- a/test/spell_check.words +++ b/test/spell_check.words @@ -9,6 +9,7 @@ pathlib plugin powershell prepend +psutil pwsh pydocstyle pytest From 892221be94f9dfca50765c80b4041182db4cbe15 Mon Sep 17 00:00:00 2001 From: Matthijs van der Burgh Date: Mon, 16 Feb 2026 08:52:13 +0100 Subject: [PATCH 2/2] Use psutil >=5.4.3 --- setup.cfg | 2 +- stdeb.cfg | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index 7796c9e..b5ab345 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,7 +27,7 @@ keywords = colcon python_requires = >=3.6 install_requires = colcon-core>=0.12.0 - psutil>=5.6.0 + psutil>=5.4.3 packages = find: zip_safe = false diff --git a/stdeb.cfg b/stdeb.cfg index 63d917e..ce61098 100644 --- a/stdeb.cfg +++ b/stdeb.cfg @@ -1,6 +1,6 @@ [colcon-powershell] No-Python2: -Depends3: python3-colcon-core (>= 0.12.0), python3-psutil (>= 5.6.0) +Depends3: python3-colcon-core (>= 0.12.0), python3-psutil (>= 5.4.3) Suite: focal jammy noble resolute bookworm trixie X-Python3-Version: >= 3.6 Upstream-Version-Suffix: +upstream