Skip to content

Commit f1c4ae5

Browse files
Create $psEditor as a constant (#1647)
This prevents accidental removal (and breakage of the extension) by disallowing `Remove-Variable` from removing `$psEditor`. While it's not expected anyone would do this, glob expressions may include the variable name and when piped to `Remove-Variable` resulted in a user-reported breakage.
1 parent 262aab7 commit f1c4ae5

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/PowerShellEditorServices/Services/Extension/ExtensionService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,14 @@ internal Task InitializeAsync()
104104

105105
// Assign the new EditorObject to be the static instance available to binary APIs
106106
EditorObject.SetAsStaticInstance();
107+
// This is constant so Remove-Variable cannot remove it.
108+
PSVariable psEditor = new(PSEditorVariableName, EditorObject, ScopedItemOptions.Constant);
107109

108110
// Register the editor object in the runspace
109111
return ExecutionService.ExecuteDelegateAsync(
110112
$"Create ${PSEditorVariableName} object",
111113
ExecutionOptions.Default,
112-
(pwsh, cancellationToken) =>
113-
{
114-
pwsh.Runspace.SessionStateProxy.PSVariable.Set(PSEditorVariableName, EditorObject);
115-
},
114+
(pwsh, _) => pwsh.Runspace.SessionStateProxy.PSVariable.Set(psEditor),
116115
CancellationToken.None);
117116
}
118117

0 commit comments

Comments
 (0)