Skip to content

Commit 9e5bab5

Browse files
committed
Hide more internal PowerShell scripts from debugger
1 parent f4faebe commit 9e5bab5

File tree

8 files changed

+13
-9
lines changed

8 files changed

+13
-9
lines changed

Diff for: src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ private void LogHostInformation()
350350
private static string GetPSOutputEncoding()
351351
{
352352
using SMA.PowerShell pwsh = SMA.PowerShell.Create();
353-
return pwsh.AddScript("$OutputEncoding.EncodingName", useLocalScope: true).Invoke<string>()[0];
353+
return pwsh.AddScript(
354+
"[System.Diagnostics.DebuggerHidden()]param() $OutputEncoding.EncodingName",
355+
useLocalScope: true).Invoke<string>()[0];
354356
}
355357

356358
// TODO: Deduplicate this with VersionUtils.

Diff for: src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public async Task<string> SetVariableAsync(int variableContainerReferenceId, str
367367

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

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

805805
IEnumerable callStack = isRemoteRunspace

Diff for: src/PowerShellEditorServices/Services/DebugAdapter/Handlers/DebugEvaluateHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request,
4848
if (isFromRepl)
4949
{
5050
await _executionService.ExecutePSCommandAsync(
51-
new PSCommand().AddScript(request.Expression),
51+
new PSCommand().AddScript($"[System.Diagnostics.DebuggerHidden()]param() {request.Expression}"),
5252
cancellationToken,
5353
new PowerShellExecutionOptions { WriteOutputToHost = true, ThrowOnError = false, AddToHistory = true }).HandleErrorsAsync(_logger).ConfigureAwait(false);
5454
}

Diff for: src/PowerShellEditorServices/Services/PowerShell/Context/PowerShellVersionDetails.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh
5555
try
5656
{
5757
Hashtable psVersionTable = pwsh
58-
.AddScript("$PSVersionTable", useLocalScope: true)
58+
.AddScript("[System.Diagnostics.DebuggerHidden()]param() $PSVersionTable", useLocalScope: true)
5959
.InvokeAndClear<Hashtable>()
6060
.FirstOrDefault();
6161

Diff for: src/PowerShellEditorServices/Services/PowerShell/Handlers/EvaluateHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request,
2323
{
2424
// This API is mostly used for F8 execution so it requires the foreground.
2525
await _executionService.ExecutePSCommandAsync(
26-
new PSCommand().AddScript(request.Expression),
26+
new PSCommand().AddScript($"[System.Diagnostics.DebuggerHidden()]param() {request.Expression}"),
2727
CancellationToken.None,
2828
new PowerShellExecutionOptions
2929
{

Diff for: src/PowerShellEditorServices/Services/PowerShell/Handlers/ExpandAliasHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task<ExpandAliasResult> Handle(ExpandAliasParams request, Cancellat
3333
{
3434
const string script = @"
3535
function __Expand-Alias {
36-
36+
[System.Diagnostics.DebuggerHidden()]
3737
param($targetScript)
3838
3939
[ref]$errors=$null

Diff for: src/PowerShellEditorServices/Services/PowerShell/Handlers/ShowHelpHandler.cs

+2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ internal class ShowHelpHandler : IShowHelpHandler
2727

2828
public async Task<Unit> Handle(ShowHelpParams request, CancellationToken cancellationToken)
2929
{
30+
// TODO: Refactor to not rerun the function definition every time.
3031
const string CheckHelpScript = @"
32+
[System.Diagnostics.DebuggerHidden()]
3133
[CmdletBinding()]
3234
param (
3335
[String]$CommandName

Diff for: src/PowerShellEditorServices/Services/PowerShell/Runspace/SessionDetails.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static SessionDetails GetFromPowerShell(PowerShell pwsh)
2727
{
2828
Hashtable detailsObject = pwsh
2929
.AddScript(
30-
$"@{{ '{Property_ComputerName}' = if ([Environment]::MachineName) {{[Environment]::MachineName}} else {{'localhost'}}; '{Property_ProcessId}' = $PID; '{Property_InstanceId}' = $host.InstanceId }}",
30+
$"[System.Diagnostics.DebuggerHidden()]param() @{{ '{Property_ComputerName}' = if ([Environment]::MachineName) {{[Environment]::MachineName}} else {{'localhost'}}; '{Property_ProcessId}' = $PID; '{Property_InstanceId}' = $host.InstanceId }}",
3131
useLocalScope: true)
3232
.InvokeAndClear<Hashtable>()
3333
.FirstOrDefault();

0 commit comments

Comments
 (0)