Skip to content

Commit 33aabe1

Browse files
authored
Avoid PSModulePath inheritance during update on Windows (#10541)
1 parent e734225 commit 33aabe1

3 files changed

Lines changed: 65 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "bugfix",
3+
"category": "``update``",
4+
"description": "Reset PSModulePath before launching new PowerShell console to avoid inheriting an incompatible PSModulePath"
5+
}

awscli/customizations/update.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ def _do_update(self):
211211
f.write('set AWS_CLI_DISTRIBUTION_SOURCE_OVERRIDE=update-exe\n')
212212
if self._no_color:
213213
f.write('set NO_COLOR=1\n')
214+
# Clear the inherited PSModulePath so the launched PowerShell
215+
# rebuilds its own default.
216+
# https://github.com/aws/aws-cli/issues/10532
217+
f.write('set PSModulePath=\n')
214218
f.write('ping -n 3 127.0.0.1 >nul 2>&1\n')
215219
f.write(f'"{ps_exe}" {ps_args}\n')
216220

@@ -228,10 +232,13 @@ def _run_install(self, cmd):
228232
)
229233

230234
def _find_powershell(self):
231-
path = shutil.which('powershell')
232-
if not path:
233-
raise UpdateError('powershell.exe not found on PATH.')
234-
return path
235+
for name in ('powershell', 'pwsh'):
236+
path = shutil.which(name)
237+
if path:
238+
return path
239+
raise UpdateError(
240+
'Neither powershell.exe nor pwsh.exe was found on PATH.'
241+
)
235242

236243
def _is_system_install(self, install_metadata):
237244
if 'script_install' in install_metadata:

tests/unit/customizations/test_update.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,22 @@ def test_resolves_symlink_before_checking_install_dir(
222222

223223

224224
class TestWindowsUpdateCommand:
225-
def _command(self, install, elevated=True, runner=None, downloader=None):
225+
def _command(
226+
self,
227+
install,
228+
elevated=True,
229+
runner=None,
230+
downloader=None,
231+
powershell_path='powershell.exe',
232+
):
226233
return WindowsUpdateCommand(
227234
mock_session(),
228235
source='exe',
229236
install_metadata=install,
230237
downloader=downloader or mock.Mock(),
231238
is_elevated=elevated,
232239
runner=runner or mock.Mock(),
233-
powershell_path='powershell.exe',
240+
powershell_path=powershell_path,
234241
)
235242

236243
def _run(self, install, color='auto', **kwargs):
@@ -275,6 +282,46 @@ def test_wrapper_sets_no_color_when_color_off(self):
275282

276283
assert 'set NO_COLOR=1' in wrapper
277284

285+
def test_wrapper_clears_psmodulepath(self):
286+
_, wrapper = self._run(USER_INSTALL)
287+
288+
assert 'set PSModulePath=\n' in wrapper
289+
290+
def test_prefers_windows_powershell_when_available(self, monkeypatch):
291+
def which(name):
292+
return {
293+
'powershell': 'C:\\powershell.exe',
294+
'pwsh': 'C:\\pwsh.exe',
295+
}.get(name)
296+
297+
monkeypatch.setattr(update_module.shutil, 'which', which)
298+
299+
_, wrapper = self._run(USER_INSTALL, powershell_path=None)
300+
301+
assert '"C:\\powershell.exe" -NoProfile' in wrapper
302+
303+
def test_falls_back_to_pwsh_when_windows_powershell_missing(
304+
self, monkeypatch
305+
):
306+
def which(name):
307+
return 'C:\\pwsh.exe' if name == 'pwsh' else None
308+
309+
monkeypatch.setattr(update_module.shutil, 'which', which)
310+
311+
_, wrapper = self._run(USER_INSTALL, powershell_path=None)
312+
313+
assert '"C:\\pwsh.exe" -NoProfile' in wrapper
314+
315+
def test_raises_when_no_powershell_found(self, monkeypatch):
316+
def which(name):
317+
return None
318+
319+
monkeypatch.setattr(update_module.shutil, 'which', which)
320+
command = self._command(USER_INSTALL, powershell_path=None)
321+
322+
with pytest.raises(UpdateError, match='Neither powershell'):
323+
command([], global_args())
324+
278325
def test_system_install_requires_elevation(self):
279326
runner = mock.Mock()
280327
command = self._command(SYSTEM_INSTALL, elevated=False, runner=runner)

0 commit comments

Comments
 (0)