Skip to content

Commit

Permalink
Hide more internal PowerShell scripts from debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleejordan committed May 12, 2023
1 parent f4faebe commit ec81e75
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ private void LogHostInformation()
private static string GetPSOutputEncoding()
{
using SMA.PowerShell pwsh = SMA.PowerShell.Create();
return pwsh.AddScript("$OutputEncoding.EncodingName", useLocalScope: true).Invoke<string>()[0];
return pwsh.AddScript(
"[System.Diagnostics.DebuggerHidden()]param() $OutputEncoding.EncodingName",
useLocalScope: true).Invoke<string>()[0];
}

// TODO: Deduplicate this with VersionUtils.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public async Task<string> SetVariableAsync(int variableContainerReferenceId, str

// Evaluate the expression to get back a PowerShell object from the expression string.
// This may throw, in which case the exception is propagated to the caller
PSCommand evaluateExpressionCommand = new PSCommand().AddScript(value);
PSCommand evaluateExpressionCommand = new PSCommand().AddScript($"[System.Diagnostics.DebuggerHidden()]param() {value}");
IReadOnlyList<object> expressionResults = await _executionService.ExecutePSCommandAsync<object>(evaluateExpressionCommand, CancellationToken.None).ConfigureAwait(false);
if (expressionResults.Count == 0)
{
Expand Down Expand Up @@ -500,7 +500,7 @@ public async Task<VariableDetails> EvaluateExpressionAsync(
bool writeResultAsOutput,
CancellationToken cancellationToken)
{
PSCommand command = new PSCommand().AddScript(expressionString);
PSCommand command = new PSCommand().AddScript($"[System.Diagnostics.DebuggerHidden()]param() {expressionString}");
IReadOnlyList<PSObject> results;
try
{
Expand Down Expand Up @@ -799,7 +799,7 @@ private async Task FetchStackFramesAsync(string scriptNameOverride)

// PSObject is used here instead of the specific type because we get deserialized
// objects from remote sessions and want a common interface.
PSCommand psCommand = new PSCommand().AddScript($"[Collections.ArrayList]{callStackVarName} = @(); {getPSCallStack}; {returnSerializedIfInRemoteRunspace}");
PSCommand psCommand = new PSCommand().AddScript($"[System.Diagnostics.DebuggerHidden()]param() [Collections.ArrayList]{callStackVarName} = @(); {getPSCallStack}; {returnSerializedIfInRemoteRunspace}");
IReadOnlyList<PSObject> results = await _executionService.ExecutePSCommandAsync<PSObject>(psCommand, CancellationToken.None).ConfigureAwait(false);

IEnumerable callStack = isRemoteRunspace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request,
if (isFromRepl)
{
await _executionService.ExecutePSCommandAsync(
new PSCommand().AddScript(request.Expression),
new PSCommand().AddScript($"[System.Diagnostics.DebuggerHidden()]param() {request.Expression}"),
cancellationToken,
new PowerShellExecutionOptions { WriteOutputToHost = true, ThrowOnError = false, AddToHistory = true }).HandleErrorsAsync(_logger).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh
try
{
Hashtable psVersionTable = pwsh
.AddScript("$PSVersionTable", useLocalScope: true)
.AddScript("[System.Diagnostics.DebuggerHidden()]param() $PSVersionTable", useLocalScope: true)
.InvokeAndClear<Hashtable>()
.FirstOrDefault();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<ExpandAliasResult> Handle(ExpandAliasParams request, Cancellat
{
const string script = @"
function __Expand-Alias {
[System.Diagnostics.DebuggerHidden()]
param($targetScript)
[ref]$errors=$null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ internal class ShowHelpHandler : IShowHelpHandler

public async Task<Unit> Handle(ShowHelpParams request, CancellationToken cancellationToken)
{
// TODO: Refactor to not rerun the function definition every time.
const string CheckHelpScript = @"
[System.Diagnostics.DebuggerHidden()]
[CmdletBinding()]
param (
[String]$CommandName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static SessionDetails GetFromPowerShell(PowerShell pwsh)
{
Hashtable detailsObject = pwsh
.AddScript(
$"@{{ '{Property_ComputerName}' = if ([Environment]::MachineName) {{[Environment]::MachineName}} else {{'localhost'}}; '{Property_ProcessId}' = $PID; '{Property_InstanceId}' = $host.InstanceId }}",
$"[System.Diagnostics.DebuggerHidden()]param() @{{ '{Property_ComputerName}' = if ([Environment]::MachineName) {{[Environment]::MachineName}} else {{'localhost'}}; '{Property_ProcessId}' = $PID; '{Property_InstanceId}' = $host.InstanceId }}",
useLocalScope: true)
.InvokeAndClear<Hashtable>()
.FirstOrDefault();
Expand Down

0 comments on commit ec81e75

Please sign in to comment.