Skip to content

Commit c940e5b

Browse files
committed
WIP: More cleanup
1 parent 2f023a7 commit c940e5b

File tree

8 files changed

+68
-61
lines changed

8 files changed

+68
-61
lines changed

.vscode/launch.json

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
22
"version": "0.2.0",
33
"configurations": [
4+
{
5+
"name": "PowerShell Launch Current File",
6+
"type": "PowerShell",
7+
"request": "launch",
8+
"script": "${file}",
9+
"cwd": "${workspaceFolder}"
10+
},
411
{
512
"name": ".NET Full: Attach to Process",
613
"type": "clr",

src/PowerShellEditorServices/Services/DebugAdapter/DebugEventHandlerService.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ e.OriginalEvent.Breakpoints[0] is CommandBreakpoint
8484
: "breakpoint";
8585
}
8686

87-
_debugAdapterServer.SendNotification(EventNames.Stopped,
87+
_debugAdapterServer?.SendNotification(EventNames.Stopped,
8888
new StoppedEvent
8989
{
9090
ThreadId = 1,
@@ -114,7 +114,7 @@ private void OnRunspaceChanged(object sender, RunspaceChangedEventArgs e)
114114
// Exited the session while the debugger is stopped,
115115
// send a ContinuedEvent so that the client changes the
116116
// UI to appear to be running again
117-
_debugAdapterServer.SendNotification(
117+
_debugAdapterServer?.SendNotification(
118118
EventNames.Continued,
119119
new ContinuedEvent
120120
{
@@ -128,7 +128,7 @@ private void OnRunspaceChanged(object sender, RunspaceChangedEventArgs e)
128128

129129
private void OnDebuggerResuming(object sender, DebuggerResumingEventArgs e)
130130
{
131-
_debugAdapterServer.SendNotification(EventNames.Continued,
131+
_debugAdapterServer?.SendNotification(EventNames.Continued,
132132
new ContinuedEvent
133133
{
134134
ThreadId = ThreadsHandler.PipelineThread.Id,
@@ -159,7 +159,7 @@ private void OnBreakpointUpdated(object sender, BreakpointUpdatedEventArgs e)
159159
_ => "InvalidBreakpointUpdateTypeEnum"
160160
};
161161

162-
_debugAdapterServer.SendNotification(
162+
_debugAdapterServer?.SendNotification(
163163
EventNames.Breakpoint,
164164
new BreakpointEvent { Breakpoint = breakpoint, Reason = reason }
165165
);

src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using System;

src/PowerShellEditorServices/Services/DebugAdapter/DebugStateService.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ internal void ReleaseSetBreakpointHandle()
4646

4747
internal async Task WaitForSetBreakpointHandleAsync()
4848
{
49-
await _setBreakpointInProgressHandle.WaitAsync()
50-
.ConfigureAwait(continueOnCapturedContext: false);
49+
await _setBreakpointInProgressHandle.WaitAsync().ConfigureAwait(false);
5150
}
5251
}
5352
}

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/SetVariableHandler.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ await _debugService.SetVariableAsync(
3636
request.Name,
3737
request.Value).ConfigureAwait(false);
3838

39-
return new SetVariableResponse
40-
{
41-
Value = updatedValue
42-
};
43-
39+
return new SetVariableResponse { Value = updatedValue };
4440
}
4541
catch (Exception ex) when(ex is ArgumentTransformationMetadataException ||
4642
ex is InvalidPowerShellExpressionException ||

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+35-33
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,16 @@ private void RunTopLevelExecutionLoop()
532532
// Signal that we are ready for outside services to use
533533
_started.TrySetResult(true);
534534

535-
if (_hostInfo.ConsoleReplEnabled)
536-
{
537-
RunExecutionLoop();
538-
}
539-
else
540-
{
541-
// TODO: Is this really necessary?
542-
RunNoPromptExecutionLoop();
543-
}
535+
RunExecutionLoop();
536+
// if (_hostInfo.ConsoleReplEnabled)
537+
// {
538+
// RunExecutionLoop();
539+
// }
540+
// else
541+
// {
542+
// // TODO: Is this really necessary?
543+
// RunNoPromptExecutionLoop();
544+
// }
544545
}
545546
catch (Exception e)
546547
{
@@ -554,30 +555,30 @@ private void RunTopLevelExecutionLoop()
554555
}
555556

556557
// TODO: This seems unncessary?
557-
private void RunNoPromptExecutionLoop()
558-
{
559-
while (!ShouldExitExecutionLoop)
560-
{
561-
using (CancellationScope cancellationScope = _cancellationContext.EnterScope(isIdleScope: false))
562-
{
563-
string taskRepresentation = null;
564-
try
565-
{
566-
ISynchronousTask task = _taskQueue.Take(cancellationScope.CancellationToken);
567-
taskRepresentation = task.ToString();
568-
task.ExecuteSynchronously(cancellationScope.CancellationToken);
569-
}
570-
catch (OperationCanceledException)
571-
{
572-
// Just continue
573-
}
574-
catch (Exception e)
575-
{
576-
_logger.LogError(e, $"Fatal exception occurred with task '{taskRepresentation ?? "<null task>"}'");
577-
}
578-
}
579-
}
580-
}
558+
// private void RunNoPromptExecutionLoop()
559+
// {
560+
// while (!ShouldExitExecutionLoop)
561+
// {
562+
// using (CancellationScope cancellationScope = _cancellationContext.EnterScope(isIdleScope: false))
563+
// {
564+
// string taskRepresentation = null;
565+
// try
566+
// {
567+
// ISynchronousTask task = _taskQueue.Take(cancellationScope.CancellationToken);
568+
// taskRepresentation = task.ToString();
569+
// task.ExecuteSynchronously(cancellationScope.CancellationToken);
570+
// }
571+
// catch (OperationCanceledException)
572+
// {
573+
// // Just continue
574+
// }
575+
// catch (Exception e)
576+
// {
577+
// _logger.LogError(e, $"Fatal exception occurred with task '{taskRepresentation ?? "<null task>"}'");
578+
// }
579+
// }
580+
// }
581+
// }
581582

582583
private void RunDebugExecutionLoop()
583584
{
@@ -692,6 +693,7 @@ private void InvokeInput(string input, CancellationToken cancellationToken)
692693

693694
private void AddRunspaceEventHandlers(Runspace runspace)
694695
{
696+
// TODO: I think these handlers must be added earlier.
695697
runspace.Debugger.DebuggerStop += OnDebuggerStopped;
696698
runspace.Debugger.BreakpointUpdated += OnBreakpointUpdated;
697699
runspace.StateChanged += OnRunspaceStateChanged;

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ await debugService.SetLineBreakpointsAsync(
393393
debugService.GetVariables(stackFrames[0].AutoVariables.Id);
394394

395395
// Verify the breakpoint only broke at the condition ie. $i -eq breakpointValue1
396-
var i = variables.FirstOrDefault(v => v.Name == "$i");
396+
var i = Array.Find(variables, v => v.Name == "$i");
397397
Assert.NotNull(i);
398398
Assert.False(i.IsExpandable);
399399
Assert.Equal($"{hitCount}", i.ValueString);
@@ -653,14 +653,14 @@ await debugService.SetLineBreakpointsAsync(
653653
VariableScope[] scopes = debugService.GetVariableScopes(0);
654654

655655
// Test set of script scope bool variable (strongly typed)
656-
VariableScope scriptScope = scopes.FirstOrDefault(s => s.Name == VariableContainerDetails.ScriptScopeName);
656+
VariableScope scriptScope = Array.Find(scopes, s => s.Name == VariableContainerDetails.ScriptScopeName);
657657
string newBoolValue = "$true";
658658
string newBoolExpr = "1";
659659
string setBoolValue = await debugService.SetVariableAsync(scriptScope.Id, "$scriptBool", newBoolExpr).ConfigureAwait(false);
660660
Assert.Equal(newBoolValue, setBoolValue);
661661

662662
// Test set of global scope ActionPreference variable (strongly typed)
663-
VariableScope globalScope = scopes.FirstOrDefault(s => s.Name == VariableContainerDetails.GlobalScopeName);
663+
VariableScope globalScope = Array.Find(scopes, s => s.Name == VariableContainerDetails.GlobalScopeName);
664664
string newGlobalValue = "Continue";
665665
string newGlobalExpr = "'Continue'";
666666
string setGlobalValue = await debugService.SetVariableAsync(globalScope.Id, "$VerbosePreference", newGlobalExpr).ConfigureAwait(false);
@@ -675,21 +675,21 @@ await debugService.SetLineBreakpointsAsync(
675675

676676
// Test set of a local string variable (not strongly typed but force conversion)
677677
variables = debugService.GetVariables(stackFrames[0].AutoVariables.Id);
678-
var strVar = variables.FirstOrDefault(v => v.Name == "$strVar2");
678+
var strVar = Array.Find(variables, v => v.Name == "$strVar2");
679679
Assert.Equal(newStrValue, strVar.ValueString);
680680

681681
scopes = debugService.GetVariableScopes(0);
682682

683683
// Test set of script scope bool variable (strongly typed)
684-
scriptScope = scopes.FirstOrDefault(s => s.Name == VariableContainerDetails.ScriptScopeName);
684+
scriptScope = Array.Find(scopes, s => s.Name == VariableContainerDetails.ScriptScopeName);
685685
variables = debugService.GetVariables(scriptScope.Id);
686-
var boolVar = variables.FirstOrDefault(v => v.Name == "$scriptBool");
686+
var boolVar = Array.Find(variables, v => v.Name == "$scriptBool");
687687
Assert.Equal(newBoolValue, boolVar.ValueString);
688688

689689
// Test set of global scope ActionPreference variable (strongly typed)
690-
globalScope = scopes.FirstOrDefault(s => s.Name == VariableContainerDetails.GlobalScopeName);
690+
globalScope = Array.Find(scopes, s => s.Name == VariableContainerDetails.GlobalScopeName);
691691
variables = debugService.GetVariables(globalScope.Id);
692-
var globalVar = variables.FirstOrDefault(v => v.Name == "$VerbosePreference");
692+
var globalVar = Array.Find(variables, v => v.Name == "$VerbosePreference");
693693
Assert.Equal(newGlobalValue, globalVar.ValueString);
694694
}
695695

@@ -713,7 +713,7 @@ await debugService.SetLineBreakpointsAsync(
713713
VariableDetailsBase[] variables =
714714
debugService.GetVariables(stackFrames[0].AutoVariables.Id);
715715

716-
var var = variables.FirstOrDefault(v => v.Name == "$enumVar");
716+
var var = Array.Find(variables, v => v.Name == "$enumVar");
717717
Assert.NotNull(var);
718718
Assert.Equal("Continue", var.ValueString);
719719
Assert.False(var.IsExpandable);
@@ -739,7 +739,7 @@ await debugService.SetLineBreakpointsAsync(
739739
VariableDetailsBase[] variables =
740740
debugService.GetVariables(stackFrames[0].AutoVariables.Id);
741741

742-
VariableDetailsBase var = variables.FirstOrDefault(v => v.Name == "$assocArrVar");
742+
VariableDetailsBase var = Array.Find(variables, v => v.Name == "$assocArrVar");
743743
Assert.NotNull(var);
744744
Assert.Equal("[Hashtable: 2]", var.ValueString);
745745
Assert.True(var.IsExpandable);
@@ -781,7 +781,7 @@ await debugService.SetLineBreakpointsAsync(
781781
VariableDetailsBase[] variables =
782782
debugService.GetVariables(stackFrames[0].AutoVariables.Id);
783783

784-
var nullStringVar = variables.FirstOrDefault(v => v.Name == "$nullString");
784+
var nullStringVar = Array.Find(variables, v => v.Name == "$nullString");
785785
Assert.NotNull(nullStringVar);
786786
Assert.Equal("[NullString]", nullStringVar.ValueString);
787787
Assert.True(nullStringVar.IsExpandable);
@@ -807,7 +807,7 @@ await debugService.SetLineBreakpointsAsync(
807807
VariableDetailsBase[] variables =
808808
debugService.GetVariables(stackFrames[0].AutoVariables.Id);
809809

810-
var psObjVar = variables.FirstOrDefault(v => v.Name == "$psObjVar");
810+
var psObjVar = Array.Find(variables, v => v.Name == "$psObjVar");
811811
Assert.NotNull(psObjVar);
812812
Assert.True("@{Age=75; Name=John}".Equals(psObjVar.ValueString) || "@{Name=John; Age=75}".Equals(psObjVar.ValueString));
813813
Assert.True(psObjVar.IsExpandable);
@@ -840,7 +840,7 @@ await debugService.SetLineBreakpointsAsync(
840840
VariableDetailsBase[] variables =
841841
debugService.GetVariables(stackFrames[0].AutoVariables.Id);
842842

843-
var var = variables.FirstOrDefault(v => v.Name == "$psCustomObjVar");
843+
var var = Array.Find(variables, v => v.Name == "$psCustomObjVar");
844844
Assert.NotNull(var);
845845
Assert.Equal("@{Name=Paul; Age=73}", var.ValueString);
846846
Assert.True(var.IsExpandable);

test/PowerShellEditorServices.Test/PsesHostFactory.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static PsesInternalHost Create(ILoggerFactory loggerFactory)
5858
initialSessionState.ExecutionPolicy = ExecutionPolicy.Bypass;
5959
}
6060

61-
HostStartupInfo testHostDetails = new HostStartupInfo(
61+
HostStartupInfo testHostDetails = new(
6262
"PowerShell Editor Services Test Host",
6363
"Test.PowerShellEditorServices",
6464
new Version("1.0.0"),
@@ -75,9 +75,12 @@ public static PsesInternalHost Create(ILoggerFactory loggerFactory)
7575

7676
var psesHost = new PsesInternalHost(loggerFactory, null, testHostDetails);
7777

78-
psesHost.TryStartAsync(new HostStartOptions { LoadProfiles = true }, CancellationToken.None).GetAwaiter().GetResult();
78+
if (psesHost.TryStartAsync(new HostStartOptions { LoadProfiles = false }, CancellationToken.None).GetAwaiter().GetResult())
79+
{
80+
return psesHost;
81+
}
7982

80-
return psesHost;
83+
throw new Exception("Host didn't start!");
8184
}
8285
}
8386

0 commit comments

Comments
 (0)