Skip to content

Reinstate debug service tests #1605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 181 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
181 commits
Select commit Hold shift + click to select a range
f441174
Add progress
rjmholt Jun 18, 2020
325711d
Get to console repl, remove PSContext dependency
rjmholt Jun 22, 2020
7e962de
Completions working
rjmholt Jun 23, 2020
2eab92d
Convert MethodInfo to compiled delegate call
rjmholt Jun 23, 2020
0534a34
Continue tweaking REPL
rjmholt Jun 24, 2020
69bc568
Implement cancellation properly, make F8 work
rjmholt Jun 25, 2020
d8ba6a4
Set execution policy
rjmholt Jun 25, 2020
1e9e7d5
Make initialisation executions synchronous
rjmholt Jun 25, 2020
4941342
Remove extraneous runspace set
rjmholt Jun 25, 2020
ba8da6f
Reuse conhost for prompts
rjmholt Jun 25, 2020
8365f62
Fix newlines
rjmholt Jun 25, 2020
726787d
Begin nested prompt implementation
rjmholt Jul 7, 2020
cb16210
Move to PSContext
rjmholt Jul 8, 2020
81de70f
Make nested prompts work
rjmholt Jul 8, 2020
dc73db6
Fix nesting issues with stacks
rjmholt Jul 8, 2020
993a8c1
Fix handling of prompt cancellation
rjmholt Jul 14, 2020
c3cabcd
Use concurrent datastructure for stacks
rjmholt Jul 14, 2020
6f7c564
Add simple debug REPL support
rjmholt Jul 17, 2020
2f0822d
Fix out-string usage in debugger
rjmholt Jul 21, 2020
66903b6
Attempt to handle debug exit
rjmholt Jul 21, 2020
05cb6b6
Make debugger resume commands work
rjmholt Jul 23, 2020
8950e73
Change use of PS context and events to safely manage prompts
rjmholt Jul 24, 2020
c9fc931
Improve debug handling, add remoting support, improve idle processing
rjmholt Jul 29, 2020
09e53e1
Merge PS context into execution service, make REPL service a subcompo…
rjmholt Aug 3, 2020
3821b75
Implement remote debugger waiting
rjmholt Aug 4, 2020
7839f68
Improve cancellation handling and runspace cleanup
rjmholt Aug 5, 2020
58f5d99
Deal with debug abort in WinPS
rjmholt Aug 5, 2020
ecf1ed0
Temp refactor
rjmholt Aug 10, 2020
2fc9516
Migrate runspace pieces
rjmholt Aug 20, 2020
0987a4f
Bring code up to compilation
rjmholt Aug 27, 2020
cd8933e
Fix initialization
rjmholt Aug 28, 2020
4a82196
Finally get the REPL and completions back
rjmholt Aug 28, 2020
0c0a019
Fix cancellation
rjmholt Aug 28, 2020
2aab375
Get debugging to work again
rjmholt Aug 28, 2020
e7bbe6f
Fix rebase issues
rjmholt Apr 15, 2021
b3cd5af
Fix incorrect namespace
rjmholt Sep 30, 2021
c07be47
Remove defunct comment
rjmholt Sep 30, 2021
bdb8a7d
Add comment about extension service
rjmholt Sep 30, 2021
f8a9db8
Remove defunct code
rjmholt Sep 30, 2021
beecda0
Use correct wildcard escape method
rjmholt Sep 30, 2021
e2bc293
Remove unused value
rjmholt Sep 30, 2021
4b01b8f
Add comment about debugger implementation
rjmholt Sep 30, 2021
906aeba
Add comment about Debugger.ProcessCommand()
rjmholt Sep 30, 2021
a246c6f
Add comments around exception handling
rjmholt Sep 30, 2021
8acff15
Remove unused exception
rjmholt Sep 30, 2021
7b1c700
Add comments to EvaluateHandler
rjmholt Sep 30, 2021
54dce85
Fix SessionDetails comment
rjmholt Sep 30, 2021
41399b7
Add CancellationContext comment
rjmholt Sep 30, 2021
ecd51d2
Use FirstOrDefault()
rjmholt Sep 30, 2021
4c2cd6f
Add explanation to ErrorRecordExtensions
rjmholt Sep 30, 2021
315baf2
Add comments to PSCommandExtensions
rjmholt Sep 30, 2021
3be5e75
Add code ref to ErrorRecordExtensions
rjmholt Sep 30, 2021
122cae4
Add comment to PowerShellExtensions
rjmholt Sep 30, 2021
12bec6a
Simplify $PROFILE code
rjmholt Sep 30, 2021
1c5f5f2
Document runspace extension method
rjmholt Sep 30, 2021
b9bd30e
Fix profiling loading logic in config handler
rjmholt Sep 30, 2021
122c2dd
Remove extraneous JS file
rjmholt May 10, 2021
ada5528
Debugging dependency injection
rjmholt Apr 20, 2021
0229fc2
Get UI debugger running
rjmholt Apr 21, 2021
e2f64ec
Resolve commented-out code
rjmholt May 10, 2021
24c03d2
Improve queue implementation
rjmholt May 25, 2021
abf9121
Add async error handling code
rjmholt May 10, 2021
225eae7
Rework execution queue to allow correct cancellation
rjmholt Jun 21, 2021
8c7477e
TEMP: Need to fix cancellation from PSRL scope
rjmholt Jun 28, 2021
e359563
TEMP: Moving to monolithic host
rjmholt Jul 20, 2021
db428b1
WIP
rjmholt Aug 4, 2021
3ba93fe
Move all basic functionality into a new synchronous internal host
rjmholt Aug 13, 2021
c4d901e
Restore host to simple working order
rjmholt Aug 24, 2021
992c6c7
Add simple cancellation support
rjmholt Aug 30, 2021
c72ba4d
Ensure startup operations are performed before first prompt
rjmholt Aug 30, 2021
dc29f3e
Fix dependency issue
rjmholt Aug 30, 2021
1f3b015
Fix incorrect remote runspace labelling
rjmholt Aug 30, 2021
b3ad735
Begin to make the debugger work
rjmholt Aug 30, 2021
c58fc92
Fix recursive PSRL cancellation token issue in debugger
rjmholt Sep 1, 2021
a77c21f
Enable proper debug UI interaction
rjmholt Sep 2, 2021
12d7592
Fix remote session connection
rjmholt Sep 3, 2021
fb0a680
Check the current runspace for debug API support
rjmholt Sep 8, 2021
ada9e66
Fix debug disconnect handling
rjmholt Sep 9, 2021
5a2bf53
Make remote debugger function correctly
rjmholt Sep 14, 2021
b75e516
Remove dead code
rjmholt Sep 15, 2021
8a0fe1f
Remove more dead code
rjmholt Sep 15, 2021
f336092
Add comment about remote debugging
rjmholt Sep 15, 2021
fab64e9
Rename event handler
rjmholt Sep 15, 2021
d53e02c
Add ConfigureAwait(false)
rjmholt Sep 15, 2021
9743969
Make psEditor variable name a constant
rjmholt Sep 15, 2021
03a5c3a
Remove debug cancellation token
rjmholt Sep 15, 2021
7a313c3
Remove duplicated check
rjmholt Sep 15, 2021
29da9a0
Add comment
rjmholt Sep 15, 2021
d8a5e1a
Add data structure for interlocked latch pattern
rjmholt Sep 15, 2021
e2156ad
Move cancellation token argument to end of methods
rjmholt Sep 15, 2021
50635e4
Make BlockingConcurrentDeque disposable
rjmholt Sep 15, 2021
be8eb1e
Rename PSES InternalHost to PsesInternalHost
rjmholt Oct 5, 2021
330afeb
Change WriteErrorsToHost to ThrowOnError
rjmholt Oct 5, 2021
8760388
Make default PowerShellExecutionOptions static
rjmholt Oct 7, 2021
15ffe03
Remove PowerShellContextService
rjmholt Oct 7, 2021
cf0065e
Ensure ConfigureAwait(false) is used everywhere
rjmholt Oct 7, 2021
aca79c9
Fix debugger evaluation not being written out
rjmholt Oct 7, 2021
5450954
Add comment about nameless variables
rjmholt Oct 7, 2021
27c1041
Use nicer pattern matching for condition
rjmholt Oct 7, 2021
4c39aac
Add TODO about debugger disconnection handling
rjmholt Oct 7, 2021
92afce8
Convert to `is null` for consistency
rjmholt Oct 7, 2021
c6d9518
Remove unused dependencies
rjmholt Oct 7, 2021
fcf5d35
Use AllThreadsContinued consistently
rjmholt Oct 7, 2021
1bedf3a
Add comment about debug context lifetime
rjmholt Oct 7, 2021
2401643
Add context to comment
rjmholt Oct 7, 2021
9db4639
Fix breakpoint API test
rjmholt Oct 8, 2021
e97123e
Fix ThrowOnError configuration check
rjmholt Oct 8, 2021
5abc952
Ensure the DSC module import fails with an error
rjmholt Oct 8, 2021
afc6eab
Remove unneeded async/await
rjmholt Oct 8, 2021
00ea2b7
Add comment explaining remote debug setting
rjmholt Oct 8, 2021
f2433b4
Remove unused enum
rjmholt Oct 8, 2021
dbd98f5
Add comment to execution options class
rjmholt Oct 8, 2021
216a946
Add comment to synchronous task about synchronous results
rjmholt Oct 8, 2021
7c34495
Ensure F8 works
rjmholt Oct 8, 2021
dc7ae36
Add comment about not exiting the top level
rjmholt Oct 8, 2021
49bc7fb
Generalize exit handling
rjmholt Oct 8, 2021
168a239
Add comment about remote prompt
rjmholt Oct 9, 2021
3ec25c6
Simplify nested PowerShell creation
rjmholt Oct 9, 2021
0a3ce49
Use a more explicit return
rjmholt Oct 9, 2021
482ac91
Fix Ctrl-C detection
rjmholt Oct 9, 2021
ac2e63b
Rename debug event handlers
rjmholt Oct 11, 2021
2a119c6
Remove unneeded PSRL static ctor call - PSRL is now loaded on the pip…
rjmholt Oct 11, 2021
8f21cea
Move execution service to an interface
rjmholt Oct 11, 2021
bdf1379
Enhance remoting comment
rjmholt Oct 11, 2021
5f5a86e
Turn on psedit registration in remote sessions
rjmholt Oct 11, 2021
6e03fcc
Ensure failed remote file saves are logged
rjmholt Oct 11, 2021
27b694e
Add comment about IsExternalInit
rjmholt Oct 11, 2021
279a810
Reinstate runspace cleanup logic
rjmholt Oct 11, 2021
8364c5d
Fix remote psedit registration
rjmholt Oct 11, 2021
d5c6900
Fix AnalysisService using
rjmholt Oct 11, 2021
5331817
Fix InitialSessionState LanguageMode usage
rjmholt Oct 11, 2021
32a7dea
Address static analyser compiler errors
rjmholt Oct 11, 2021
4fcdd83
Apply suppression to ExitDebugLoop() API
rjmholt Oct 11, 2021
ccc2fc2
Fix NRE in DSC capability discovery
rjmholt Oct 11, 2021
b2e452f
Remove unused using statements
rjmholt Oct 12, 2021
08e2f69
Add copyright headers
rjmholt Oct 12, 2021
de18309
Remove unneeded internal usings
rjmholt Oct 12, 2021
ec85f59
Implement PowerShell engine OnIdle events
rjmholt Oct 12, 2021
e4730a1
Improve artificial idle event comment
rjmholt Oct 13, 2021
fe6a142
Fix typo
rjmholt Oct 13, 2021
08df557
Change BlockingConcurrentDequeue to provide IsEmpty property
rjmholt Oct 13, 2021
79a586b
Merge pull request #1580 from rjmholt/pt-fix-headers
rjmholt Oct 13, 2021
fa3413a
Merge pull request #1581 from rjmholt/pt-eventing
rjmholt Oct 13, 2021
7ed4903
Fix using
rjmholt Oct 13, 2021
8108224
Convert PSRL OnIdle handler to take a CancellationToken
rjmholt Oct 13, 2021
a9bcc75
Merge pull request #1587 from rjmholt/pt-idle-cancellation
rjmholt Oct 14, 2021
0f8dd05
Add initial ReadLine implementation
rjmholt Oct 12, 2021
ee874e4
Add common secure string read functionality
rjmholt Oct 12, 2021
20b3ed3
Implement shared Ctrl-C test implementation
rjmholt Oct 12, 2021
8fbfca7
Avoid unused parser call
rjmholt Oct 12, 2021
7337696
Add idle support for legacy readline
rjmholt Oct 12, 2021
2140120
Add comment about unimplemented line continuation support
rjmholt Oct 12, 2021
9787b58
Hook up legacy readline support in the host
rjmholt Oct 13, 2021
30f58b0
Remove stale TODOs
rjmholt Oct 13, 2021
20bd69b
Remove commented out code
rjmholt Oct 13, 2021
b68c7ac
Remove unused usings
rjmholt Oct 13, 2021
43bddb1
Remove unneeded readline interface methods
rjmholt Oct 14, 2021
ff77a68
Update comment
rjmholt Oct 14, 2021
9fd0dfa
Merge pull request #1584 from rjmholt/pt-legacy-readline
rjmholt Oct 15, 2021
dfee772
Initial test build
rjmholt Oct 14, 2021
61ae8f2
Add Host factory
rjmholt Oct 14, 2021
def819e
Enable host with no repl
rjmholt Oct 19, 2021
e6f21dc
Comment out LSP unit tests
rjmholt Oct 19, 2021
2daa962
Bump CI
rjmholt Oct 19, 2021
702636c
Add timeout to test
rjmholt Oct 20, 2021
d5aebd6
Ensure CI is triggered for PRs not for master
rjmholt Oct 20, 2021
d1acc64
Route startup exceptions through and ensure startup has completed bef…
rjmholt Oct 20, 2021
5d94ad6
Fix test compile error
rjmholt Oct 21, 2021
b0edca9
Fix process exit throwing
rjmholt Oct 25, 2021
2846bef
Fix bad output when console repl is disabled
rjmholt Oct 25, 2021
0681d37
Add debug logging for integration test streams
rjmholt Oct 25, 2021
196ec3b
Fix overrides
rjmholt Oct 25, 2021
d22b57c
Fix variable test with scope change
rjmholt Oct 25, 2021
5699e48
Fix assembly log exception
rjmholt Oct 25, 2021
ca7c815
Only set pipeline thread apartment state on Windows
rjmholt Oct 26, 2021
0c85a01
Remove defunct internal cmdlets (#1593)
rjmholt Oct 26, 2021
fcc4cd8
Remove debug code
rjmholt Oct 26, 2021
fbaff7c
Merge pull request #1588 from rjmholt/pt-fix-ci
rjmholt Oct 26, 2021
6447d82
Reinstate language service unit tests (#1598)
rjmholt Oct 27, 2021
328cc8b
Add initial debug service test changes
rjmholt Oct 27, 2021
3aa3fdc
Simplify debug queue
rjmholt Oct 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .vsts-ci/azure-pipelines-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ variables:
- name: DOTNET_CLI_TELEMETRY_OPTOUT
value: 'true'

trigger:
branches:
include:
- master

pr:
- master

jobs:
- job: PS51_Win2016
displayName: PowerShell 5.1 - Windows Server 2016
Expand Down

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public static EditorServicesLoader Create(
{
AppDomain.CurrentDomain.AssemblyLoad += (object sender, AssemblyLoadEventArgs args) =>
{
if (args.LoadedAssembly.IsDynamic)
{
return;
}

logger.Log(
PsesLogLevel.Diagnostic,
$"Loaded '{args.LoadedAssembly.GetName()}' from '{args.LoadedAssembly.Location}'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;

namespace Microsoft.PowerShell.EditorServices.Extensions.Services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using Microsoft.PowerShell.EditorServices.Utility;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;

Expand Down
14 changes: 10 additions & 4 deletions src/PowerShellEditorServices/Extensions/Api/EditorUIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;

namespace Microsoft.PowerShell.EditorServices.Extensions.Services
Expand Down Expand Up @@ -116,7 +116,9 @@ public async Task<string> PromptInputAsync(string message)
new ShowInputPromptRequest
{
Name = message,
}).Returning<ShowInputPromptResponse>(CancellationToken.None).ConfigureAwait(false);
})
.Returning<ShowInputPromptResponse>(CancellationToken.None)
.ConfigureAwait(false);

if (response.PromptCancelled)
{
Expand All @@ -142,7 +144,9 @@ public async Task<IReadOnlyList<string>> PromptMultipleSelectionAsync(string mes
Message = message,
Choices = choiceDetails,
DefaultChoices = defaultChoiceIndexes?.ToArray(),
}).Returning<ShowChoicePromptResponse>(CancellationToken.None).ConfigureAwait(false);
})
.Returning<ShowChoicePromptResponse>(CancellationToken.None)
.ConfigureAwait(false);

if (response.PromptCancelled)
{
Expand All @@ -168,7 +172,9 @@ public async Task<string> PromptSelectionAsync(string message, IReadOnlyList<Pro
Message = message,
Choices = choiceDetails,
DefaultChoices = defaultChoiceIndex > -1 ? new[] { defaultChoiceIndex } : null,
}).Returning<ShowChoicePromptResponse>(CancellationToken.None).ConfigureAwait(false);
})
.Returning<ShowChoicePromptResponse>(CancellationToken.None)
.ConfigureAwait(false);

if (response.PromptCancelled)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using MediatR;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using System.Threading;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Management.Automation.Language;
using System.Threading.Tasks;

namespace Microsoft.PowerShell.EditorServices.Extensions.Services
{
Expand Down
1 change: 0 additions & 1 deletion src/PowerShellEditorServices/Extensions/EditorContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;

namespace Microsoft.PowerShell.EditorServices.Extensions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using System;
Expand Down
2 changes: 1 addition & 1 deletion src/PowerShellEditorServices/Extensions/EditorObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Extensions.Services;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Extension;

namespace Microsoft.PowerShell.EditorServices.Extensions
{
Expand Down
2 changes: 0 additions & 2 deletions src/PowerShellEditorServices/Extensions/EditorWorkspace.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Threading.Tasks;

namespace Microsoft.PowerShell.EditorServices.Extensions
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Logging;
using Microsoft.PowerShell.EditorServices.Server;
using Microsoft.PowerShell.EditorServices.Services;
using Serilog;
using Serilog.Events;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using Microsoft.PowerShell.EditorServices.Services.Extension;

#if DEBUG
using Serilog.Debugging;
Expand Down
2 changes: 0 additions & 2 deletions src/PowerShellEditorServices/Logging/HostLoggerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.PowerShell.EditorServices.Logging
{
Expand Down
5 changes: 4 additions & 1 deletion src/PowerShellEditorServices/PowerShellEditorServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
</ItemGroup>

<ItemGroup>
<Compile Remove="Extensions\Api\DocumentSymbolService.cs"/>
<Compile Remove="Extensions\Api\DocumentSymbolService.cs" />
<Compile Remove="Services\Extension\Templating\**" />
<EmbeddedResource Remove="Services\Extension\Templating\**" />
<None Remove="Services\Extension\Templating\**" />
</ItemGroup>
</Project>
70 changes: 31 additions & 39 deletions src/PowerShellEditorServices/Server/PsesDebugServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@

using System;
using System.IO;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Utility;
using OmniSharp.Extensions.DebugAdapter.Protocol;
using OmniSharp.Extensions.DebugAdapter.Protocol.Serialization;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using OmniSharp.Extensions.DebugAdapter.Server;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Server;

namespace Microsoft.PowerShell.EditorServices.Server
Expand All @@ -24,25 +21,17 @@ namespace Microsoft.PowerShell.EditorServices.Server
/// </summary>
internal class PsesDebugServer : IDisposable
{
/// <summary>
/// This is a bool but must be an int, since Interlocked.Exchange can't handle a bool
/// </summary>
private static int s_hasRunPsrlStaticCtor = 0;

private static readonly Lazy<CmdletInfo> s_lazyInvokeReadLineConstructorCmdletInfo = new Lazy<CmdletInfo>(() =>
{
var type = Type.GetType("Microsoft.PowerShell.EditorServices.Commands.InvokeReadLineConstructorCommand, Microsoft.PowerShell.EditorServices.Hosting");
return new CmdletInfo("__Invoke-ReadLineConstructor", type);
});

private readonly Stream _inputStream;
private readonly Stream _outputStream;
private readonly bool _useTempSession;
private readonly bool _usePSReadLine;
private readonly TaskCompletionSource<bool> _serverStopped;

private DebugAdapterServer _debugAdapterServer;
private PowerShellContextService _powerShellContextService;

private PsesInternalHost _psesHost;

private bool _startedPses;

protected readonly ILoggerFactory _loggerFactory;

Expand Down Expand Up @@ -75,30 +64,17 @@ public async Task StartAsync()
{
// We need to let the PowerShell Context Service know that we are in a debug session
// so that it doesn't send the powerShell/startDebugger message.
_powerShellContextService = ServiceProvider.GetService<PowerShellContextService>();
_powerShellContextService.IsDebugServerActive = true;

// Needed to make sure PSReadLine's static properties are initialized in the pipeline thread.
// This is only needed for Temp sessions who only have a debug server.
if (_usePSReadLine && _useTempSession && Interlocked.Exchange(ref s_hasRunPsrlStaticCtor, 1) == 0)
{
var command = new PSCommand()
.AddCommand(s_lazyInvokeReadLineConstructorCmdletInfo.Value);

// This must be run synchronously to ensure debugging works
_powerShellContextService
.ExecuteCommandAsync<object>(command, sendOutputToHost: true, sendErrorToHost: true)
.GetAwaiter()
.GetResult();
}
_psesHost = ServiceProvider.GetService<PsesInternalHost>();
_psesHost.DebugContext.IsDebugServerActive = true;

options
.WithInput(_inputStream)
.WithOutput(_outputStream)
.WithServices(serviceCollection => serviceCollection
.AddLogging()
.AddOptions()
.AddPsesDebugServices(ServiceProvider, this, _useTempSession))
.WithServices(serviceCollection =>
serviceCollection
.AddLogging()
.AddOptions()
.AddPsesDebugServices(ServiceProvider, this, _useTempSession))
// TODO: Consider replacing all WithHandler with AddSingleton
.WithHandler<LaunchAndAttachHandler>()
.WithHandler<DisconnectHandler>()
Expand All @@ -115,6 +91,12 @@ public async Task StartAsync()
// The OnInitialize delegate gets run when we first receive the _Initialize_ request:
// https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Initialize
.OnInitialize(async (server, request, cancellationToken) => {
// We need to make sure the host has been started
_startedPses = !(await _psesHost.TryStartAsync(new HostStartOptions(), CancellationToken.None).ConfigureAwait(false));

// Ensure the debugger mode is set correctly - this is required for remote debugging to work
_psesHost.DebugContext.EnableDebugMode();

var breakpointService = server.GetService<BreakpointService>();
// Clear any existing breakpoints before proceeding
await breakpointService.RemoveAllBreakpointsAsync().ConfigureAwait(false);
Expand All @@ -136,17 +118,27 @@ public async Task StartAsync()

public void Dispose()
{
_powerShellContextService.IsDebugServerActive = false;
// TODO: If the debugger has stopped, should we clear the breakpoints?
// Note that the lifetime of the DebugContext is longer than the debug server;
// It represents the debugger on the PowerShell process we're in,
// while a new debug server is spun up for every debugging session
_psesHost.DebugContext.IsDebugServerActive = false;
_debugAdapterServer.Dispose();
_inputStream.Dispose();
_outputStream.Dispose();
_serverStopped.SetResult(true);
// TODO: If the debugger has stopped, should we clear the breakpoints?
}

public async Task WaitForShutdown()
{
await _serverStopped.Task.ConfigureAwait(false);

// If we started the host, we need to ensure any errors are marshalled back to us like this
if (_startedPses)
{
_psesHost.TriggerShutdown();
await _psesHost.Shutdown.ConfigureAwait(false);
}
}

#region Events
Expand Down
Loading