From 1b03005d1ac669e5089cc96e707042b0c9bb8883 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 18 Feb 2025 10:18:30 -0800 Subject: [PATCH] Never load a module targeting the PSReadLine module's `SessionState` (#24909) --- .../engine/Modules/ModuleCmdletBase.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index f07ddcc633..f47f9b45e4 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -136,17 +136,12 @@ namespace Microsoft.PowerShell.Commands internal SessionState TargetSessionState { - get - { - if (BaseGlobal) - { - return this.Context.TopLevelSessionState.PublicSessionState; - } - else - { - return this.Context.SessionState; - } - } + // Module loading could happen during tab completion triggered by PSReadLine, + // but that doesn't mean the module should be loaded targeting the PSReadLine + // module's session state. In that case, use Global session state instead. + get => BaseGlobal || Context.EngineSessionState.Module?.Name is "PSReadLine" + ? Context.TopLevelSessionState.PublicSessionState + : Context.SessionState; } ///