Skip to content

Fix TEMP debugging #1065

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

Merged
merged 6 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions module/PowerShellEditorServices/PowerShellEditorServices.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ function Start-EditorServicesHost {
}

if ($DebugServiceOnly.IsPresent) {
$editorServicesHost.StartDebugService($debugServiceConfig, $profilePaths, $false);
$editorServicesHost.StartDebugService($debugServiceConfig, $profilePaths, $true);
} elseif($Stdio.IsPresent) {
$editorServicesHost.StartLanguageService($languageServiceConfig, $profilePaths);
} else {
$editorServicesHost.StartLanguageService($languageServiceConfig, $profilePaths);
$editorServicesHost.StartDebugService($debugServiceConfig, $profilePaths, $true);
$editorServicesHost.StartDebugService($debugServiceConfig, $profilePaths, $false);
}

return $editorServicesHost
Expand Down
71 changes: 47 additions & 24 deletions src/PowerShellEditorServices/Hosting/EditorServicesHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.PowerShell.EditorServices.Server;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Utility;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using Serilog;

namespace Microsoft.PowerShell.EditorServices.Hosting
Expand Down Expand Up @@ -284,14 +285,33 @@ public void StartLanguageService(
/// </summary>
/// <param name="config">The config that contains information on the communication protocol that will be used.</param>
/// <param name="profilePaths">The profiles that will be loaded in the session.</param>
/// <param name="useExistingSession">Determines if we will reuse the session that we have.</param>
/// <param name="useTempSession">Determines if we will make a new session typically used for temporary console debugging.</param>
public void StartDebugService(
EditorServiceTransportConfig config,
ProfilePaths profilePaths,
bool useExistingSession)
bool useTempSession)
{
_logger.LogInformation($"Debug NamedPipe: {config.InOutPipeName}\nDebug OutPipe: {config.OutPipeName}");

IServiceProvider serviceProvider = null;
if (useTempSession)
{
serviceProvider = new ServiceCollection()
.AddLogging(builder => builder
.ClearProviders()
.AddSerilog()
.SetMinimumLevel(LogLevel.Trace))
.AddSingleton<ILanguageServer>(provider => null)
.AddPsesLanguageServices(
profilePaths,
_featureFlags,
_enableConsoleRepl,
_internalHost,
_hostDetails,
_additionalModules)
.BuildServiceProvider();
}

switch (config.TransportType)
{
case EditorServiceTransportType.NamedPipe:
Expand All @@ -312,7 +332,7 @@ public void StartDebugService(
.ContinueWith(async task =>
{
_logger.LogInformation("Starting debug server");
await _debugServer.StartAsync(_languageServer.LanguageServer.Services);
await _debugServer.StartAsync(serviceProvider ?? _languageServer.LanguageServer.Services, useTempSession);
_logger.LogInformation(
$"Debug service started, type = {config.TransportType}, endpoint = {config.Endpoint}");
});
Expand All @@ -325,25 +345,11 @@ public void StartDebugService(
Console.OpenStandardInput(),
Console.OpenStandardOutput());

_logger.LogInformation("Starting debug server");
Task.Run(async () =>
{
_logger.LogInformation("Starting debug server");

IServiceProvider serviceProvider = useExistingSession
? _languageServer.LanguageServer.Services
: new ServiceCollection().AddSingleton<PowerShellContextService>(
(provider) => PowerShellContextService.Create(
_factory,
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
profilePaths,
_featureFlags,
_enableConsoleRepl,
_internalHost,
_hostDetails,
_additionalModules))
.BuildServiceProvider();

await _debugServer.StartAsync(serviceProvider);

await _debugServer.StartAsync(serviceProvider ?? _languageServer.LanguageServer.Services, useTempSession);
_logger.LogInformation(
$"Debug service started, type = {config.TransportType}, endpoint = {config.Endpoint}");
});
Expand All @@ -353,14 +359,19 @@ public void StartDebugService(
throw new NotSupportedException($"The transport {config.TransportType} is not supported");
}

if(!alreadySubscribedDebug)
// If the instance of PSES is being used for debugging only, then we don't want to allow automatic restarting
// because the user can simply spin up a new PSES if they need to.
// This design decision was done since this "debug-only PSES" is used in the "Temporary Integrated Console debugging"
// feature which does not want PSES to be restarted so that the user can see the output of the last debug
// session.
if(!alreadySubscribedDebug && !useTempSession)
{
alreadySubscribedDebug = true;
_debugServer.SessionEnded += (sender, eventArgs) =>
{
_debugServer.Dispose();
alreadySubscribedDebug = false;
StartDebugService(config, profilePaths, useExistingSession);
StartDebugService(config, profilePaths, useTempSession);
};
}
}
Expand All @@ -378,8 +389,20 @@ public void StopServices()
/// </summary>
public void WaitForCompletion()
{
// TODO: We need a way to know when to complete this task!
_languageServer.WaitForShutdown().Wait();
// If _languageServer is not null, then we are either using:
// Stdio - that only uses a LanguageServer so we return when that has shutdown.
// NamedPipes - that uses both LanguageServer and DebugServer, but LanguageServer
// is the core of PowerShell Editor Services and if that shuts down,
// we want the whole process to shutdown.
if (_languageServer != null)
{
_languageServer.WaitForShutdown().GetAwaiter().GetResult();
return;
}

// If there is no LanguageServer, then we must be running with the DebugServiceOnly switch
// (used in Temporary console debugging) and we need to wait for the DebugServer to shutdown.
_debugServer.WaitForShutdown().GetAwaiter().GetResult();
}

#endregion
Expand Down
24 changes: 16 additions & 8 deletions src/PowerShellEditorServices/Server/PsesDebugServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class PsesDebugServer : IDisposable

private PowerShellContextService _powerShellContextService;

private readonly TaskCompletionSource<bool> _serverStopped;

public PsesDebugServer(
ILoggerFactory factory,
Stream inputStream,
Expand All @@ -34,9 +36,10 @@ public PsesDebugServer(
_loggerFactory = factory;
_inputStream = inputStream;
_outputStream = outputStream;
_serverStopped = new TaskCompletionSource<bool>();
}

public async Task StartAsync(IServiceProvider languageServerServiceProvider)
public async Task StartAsync(IServiceProvider languageServerServiceProvider, bool useTempSession)
{
_jsonRpcServer = await JsonRpcServer.From(options =>
{
Expand All @@ -50,14 +53,13 @@ public async Task StartAsync(IServiceProvider languageServerServiceProvider)
_powerShellContextService = languageServerServiceProvider.GetService<PowerShellContextService>();
_powerShellContextService.IsDebugServerActive = true;

// Needed to make sure PSReadLine's static properties are initialized in the pipeline thread.
_powerShellContextService
.ExecuteScriptStringAsync("[System.Runtime.CompilerServices.RuntimeHelpers]::RunClassConstructor([Microsoft.PowerShell.PSConsoleReadLine].TypeHandle)")
.Wait();

options.Services = new ServiceCollection()
.AddSingleton(_powerShellContextService)
.AddSingleton(languageServerServiceProvider.GetService<WorkspaceService>())
.AddSingleton(languageServerServiceProvider.GetService<RemoteFileManagerService>())
.AddSingleton<PsesDebugServer>(this)
.AddSingleton<DebugService>()
.AddSingleton<DebugStateService>()
.AddSingleton<DebugEventHandlerService>();
.AddPsesDebugServices(languageServerServiceProvider, this, useTempSession);

options
.WithInput(_inputStream)
Expand Down Expand Up @@ -95,6 +97,12 @@ public void Dispose()
{
_powerShellContextService.IsDebugServerActive = false;
_jsonRpcServer.Dispose();
_serverStopped.SetResult(true);
}

public async Task WaitForShutdown()
{
await _serverStopped.Task;
}

#region Events
Expand Down
44 changes: 7 additions & 37 deletions src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,43 +65,13 @@ public async Task StartAsync()
.WithInput(input)
.WithOutput(output)
.WithServices(serviceCollection => serviceCollection
.AddSingleton<WorkspaceService>()
.AddSingleton<SymbolsService>()
.AddSingleton<ConfigurationService>()
.AddSingleton<PowerShellContextService>(
(provider) =>
PowerShellContextService.Create(
provider.GetService<ILoggerFactory>(),
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
_profilePaths,
_featureFlags,
_enableConsoleRepl,
_internalHost,
_hostDetails,
_additionalModules))
.AddSingleton<TemplateService>()
.AddSingleton<EditorOperationsService>()
.AddSingleton<RemoteFileManagerService>()
.AddSingleton<ExtensionService>(
(provider) =>
{
var extensionService = new ExtensionService(
provider.GetService<PowerShellContextService>(),
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>());
extensionService.InitializeAsync(
serviceProvider: provider,
editorOperations: provider.GetService<EditorOperationsService>())
.Wait();
return extensionService;
})
.AddSingleton<AnalysisService>(
(provider) =>
{
return AnalysisService.Create(
provider.GetService<ConfigurationService>(),
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
provider.GetService<ILoggerFactory>().CreateLogger<AnalysisService>());
}))
.AddPsesLanguageServices(
_profilePaths,
_featureFlags,
_enableConsoleRepl,
_internalHost,
_hostDetails,
_additionalModules))
.ConfigureLogging(builder => builder
.AddSerilog(Log.Logger)
.SetMinimumLevel(LogLevel.Trace))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using System.Management.Automation.Host;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Services;

namespace Microsoft.PowerShell.EditorServices.Server
{
internal static class PsesServiceCollectionExtensions
{
public static IServiceCollection AddPsesLanguageServices (
this IServiceCollection collection,
ProfilePaths profilePaths,
HashSet<string> featureFlags,
bool enableConsoleRepl,
PSHost internalHost,
HostDetails hostDetails,
string[] additionalModules)
{
return collection.AddSingleton<WorkspaceService>()
.AddSingleton<SymbolsService>()
.AddSingleton<ConfigurationService>()
.AddSingleton<PowerShellContextService>(
(provider) =>
PowerShellContextService.Create(
provider.GetService<ILoggerFactory>(),
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
profilePaths,
featureFlags,
enableConsoleRepl,
internalHost,
hostDetails,
additionalModules))
.AddSingleton<TemplateService>()
.AddSingleton<EditorOperationsService>()
.AddSingleton<RemoteFileManagerService>()
.AddSingleton<ExtensionService>(
(provider) =>
{
var extensionService = new ExtensionService(
provider.GetService<PowerShellContextService>(),
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>());
extensionService.InitializeAsync(
serviceProvider: provider,
editorOperations: provider.GetService<EditorOperationsService>())
.Wait();
return extensionService;
})
.AddSingleton<AnalysisService>(
(provider) =>
{
return AnalysisService.Create(
provider.GetService<ConfigurationService>(),
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
provider.GetService<ILoggerFactory>().CreateLogger<AnalysisService>());
});
}

public static IServiceCollection AddPsesDebugServices(
this IServiceCollection collection,
IServiceProvider languageServiceProvider,
PsesDebugServer psesDebugServer,
bool useTempSession)
{
return collection.AddSingleton(languageServiceProvider.GetService<PowerShellContextService>())
.AddSingleton(languageServiceProvider.GetService<WorkspaceService>())
.AddSingleton(languageServiceProvider.GetService<RemoteFileManagerService>())
.AddSingleton<PsesDebugServer>(psesDebugServer)
.AddSingleton<DebugService>()
.AddSingleton<DebugStateService>(new DebugStateService
{
OwnsEditorSession = useTempSession
})
.AddSingleton<DebugEventHandlerService>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ public ConfigurationDoneHandler(
_workspaceService = workspaceService;
}

public Task<ConfigurationDoneResponse> Handle(ConfigurationDoneArguments request, CancellationToken cancellationToken)
public async Task<ConfigurationDoneResponse> Handle(ConfigurationDoneArguments request, CancellationToken cancellationToken)
{
_debugService.IsClientAttached = true;

if (_debugStateService.OwnsEditorSession)
{
// If this is a debug-only session, we need to start
// the command loop manually
_powerShellContextService.ConsoleReader.StartCommandLoop();
}

if (!string.IsNullOrEmpty(_debugStateService.ScriptToLaunch))
{
if (_powerShellContextService.SessionState == PowerShellContextState.Ready)
Expand All @@ -63,14 +70,6 @@ public Task<ConfigurationDoneResponse> Handle(ConfigurationDoneArguments request

if (_debugStateService.IsInteractiveDebugSession)
{
if (_debugStateService.OwnsEditorSession)
{
// If this is a debug-only session, we need to start
// the command loop manually
// TODO: Bring this back
//_editorSession.HostInput.StartCommandLoop();
}

if (_debugService.IsDebuggerStopped)
{
if (_debugService.CurrentDebuggerStoppedEventArgs != null)
Expand All @@ -88,7 +87,7 @@ public Task<ConfigurationDoneResponse> Handle(ConfigurationDoneArguments request
}
}

return Task.FromResult(new ConfigurationDoneResponse());
return new ConfigurationDoneResponse();
}

private async Task LaunchScriptAsync(string scriptToLaunch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,7 @@ private void OnExecutionStatusChanged(

private void PowerShellContext_RunspaceChangedAsync(object sender, RunspaceChangedEventArgs e)
{
_languageServer.SendNotification(
_languageServer?.SendNotification(
"powerShell/runspaceChanged",
new MinifiedRunspaceDetails(e.NewRunspace));
}
Expand Down Expand Up @@ -1831,7 +1831,7 @@ public MinifiedRunspaceDetails(RunspaceDetails eventArgs)
/// <param name="e">details of the execution status change</param>
private void PowerShellContext_ExecutionStatusChangedAsync(object sender, ExecutionStatusChangedEventArgs e)
{
_languageServer.SendNotification(
_languageServer?.SendNotification(
"powerShell/executionStatusChanged",
e);
}
Expand Down