Skip to content

Commit 846045e

Browse files
committed
Fix miscellaneous logging, typos, cancellation tokens, and notes
1 parent a430513 commit 846045e

File tree

10 files changed

+30
-40
lines changed

10 files changed

+30
-40
lines changed

src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public Task LoadAndRunEditorServicesAsync()
208208
#endif
209209

210210
// Add the bundled modules to the PSModulePath
211+
// TODO: Why do we do this in addition to passing the bundled module path to the host?
211212
UpdatePSModulePath();
212213

213214
// Check to see if the configuration we have is valid

src/PowerShellEditorServices/Hosting/EditorServicesServerFactory.cs

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public PsesDebugServer CreateDebugServerForTempSession(
152152
.AddSerilog()
153153
.SetMinimumLevel(LogLevel.Trace)) // TODO: Why randomly set to trace?
154154
.AddSingleton<ILanguageServerFacade>(_ => null)
155+
// TODO: Why add these for a debug server?!
155156
.AddPsesLanguageServices(hostStartupInfo)
156157
// For a Temp session, there is no LanguageServer so just set it to null
157158
.AddSingleton(

src/PowerShellEditorServices/Server/PsesDebugServer.cs

+6-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.IO;
6-
using System.Threading;
76
using System.Threading.Tasks;
87
using Microsoft.Extensions.DependencyInjection;
98
using Microsoft.Extensions.Logging;
@@ -23,15 +22,10 @@ internal class PsesDebugServer : IDisposable
2322
private readonly Stream _inputStream;
2423
private readonly Stream _outputStream;
2524
private readonly TaskCompletionSource<bool> _serverStopped;
26-
2725
private DebugAdapterServer _debugAdapterServer;
28-
2926
private PsesInternalHost _psesHost;
30-
3127
private bool _startedPses;
32-
3328
private readonly bool _isTemp;
34-
3529
protected readonly ILoggerFactory _loggerFactory;
3630

3731
public PsesDebugServer(
@@ -91,10 +85,12 @@ public async Task StartAsync()
9185
.WithHandler<DebugEvaluateHandler>()
9286
// The OnInitialize delegate gets run when we first receive the _Initialize_ request:
9387
// https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Initialize
94-
.OnInitialize(async (server, _, _) =>
88+
.OnInitialize(async (server, _, cancellationToken) =>
9589
{
96-
// We need to make sure the host has been started
97-
_startedPses = !await _psesHost.TryStartAsync(new HostStartOptions(), CancellationToken.None).ConfigureAwait(false);
90+
// Start the host if not already started, and enable debug mode (required
91+
// for remote debugging).
92+
_startedPses = !await _psesHost.TryStartAsync(new HostStartOptions(), cancellationToken).ConfigureAwait(false);
93+
_psesHost.DebugContext.EnableDebugMode();
9894

9995
// We need to give the host a handle to the DAP so it can register
10096
// notifications (specifically for sendKeyPress).
@@ -103,11 +99,8 @@ public async Task StartAsync()
10399
_psesHost.DebugServer = server;
104100
}
105101

106-
// Ensure the debugger mode is set correctly - this is required for remote debugging to work
107-
_psesHost.DebugContext.EnableDebugMode();
108-
102+
// Clear any existing breakpoints before proceeding.
109103
BreakpointService breakpointService = server.GetService<BreakpointService>();
110-
// Clear any existing breakpoints before proceeding
111104
await breakpointService.RemoveAllBreakpointsAsync().ConfigureAwait(false);
112105
})
113106
// The OnInitialized delegate gets run right before the server responds to the _Initialize_ request:

src/PowerShellEditorServices/Server/PsesLanguageServer.cs

-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ namespace Microsoft.PowerShell.EditorServices.Server
2424
internal class PsesLanguageServer
2525
{
2626
internal ILoggerFactory LoggerFactory { get; }
27-
2827
internal ILanguageServer LanguageServer { get; private set; }
29-
3028
private readonly LogLevel _minimumLogLevel;
3129
private readonly Stream _inputStream;
3230
private readonly Stream _outputStream;
3331
private readonly HostStartupInfo _hostDetails;
3432
private readonly TaskCompletionSource<bool> _serverStart;
35-
3633
private PsesInternalHost _psesHost;
3734

3835
/// <summary>
@@ -156,7 +153,6 @@ public async Task StartAsync()
156153
/// <returns>A task that completes when the server is shut down.</returns>
157154
public async Task WaitForShutdown()
158155
{
159-
Log.Logger.Debug("Shutting down OmniSharp Language Server");
160156
await _serverStart.Task.ConfigureAwait(false);
161157
await LanguageServer.WaitForExit.ConfigureAwait(false);
162158

src/PowerShellEditorServices/Server/PsesServiceCollectionExtensions.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ public static IServiceCollection AddPsesLanguageServices(
4444
provider.GetService<EditorOperationsService>(),
4545
provider.GetService<IInternalPowerShellExecutionService>());
4646

47-
// This is where we create the $psEditor variable
48-
// so that when the console is ready, it will be available
49-
// TODO: Improve the sequencing here so that:
50-
// - The variable is guaranteed to be initialized when the console first appears
51-
// - Any errors that occur are handled rather than lost by the unawaited task
47+
// This is where we create the $psEditor variable so that when the console
48+
// is ready, it will be available. NOTE: We cannot await this because it
49+
// uses a lazy initialization to avoid a race with the dependency injection
50+
// framework, see the EditorObject class for that!
5251
extensionService.InitializeAsync();
5352

5453
return extensionService;

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public PsesInternalHost(
102102
// Respect a user provided bundled module path.
103103
if (Directory.Exists(hostInfo.BundledModulePath))
104104
{
105-
_logger.LogTrace("Using new bundled module path: {}", hostInfo.BundledModulePath);
105+
_logger.LogTrace($"Using new bundled module path: {hostInfo.BundledModulePath}");
106106
s_bundledModulePath = hostInfo.BundledModulePath;
107107
}
108108

@@ -236,7 +236,7 @@ public override void SetShouldExit(int exitCode)
236236
/// <returns>A task that resolves when the host has finished startup, with the value true if the caller started the host, and false otherwise.</returns>
237237
public async Task<bool> TryStartAsync(HostStartOptions startOptions, CancellationToken cancellationToken)
238238
{
239-
_logger.LogInformation("Host starting");
239+
_logger.LogDebug("Starting host...");
240240
if (!_isRunningLatch.TryEnter())
241241
{
242242
_logger.LogDebug("Host start requested after already started.");
@@ -248,13 +248,16 @@ public async Task<bool> TryStartAsync(HostStartOptions startOptions, Cancellatio
248248

249249
if (startOptions.LoadProfiles)
250250
{
251+
_logger.LogDebug("Loading profiles...");
251252
await LoadHostProfilesAsync(cancellationToken).ConfigureAwait(false);
252-
_logger.LogInformation("Profiles loaded");
253+
_logger.LogDebug("Profiles loaded!");
253254
}
254255

255256
if (startOptions.InitialWorkingDirectory is not null)
256257
{
257-
await SetInitialWorkingDirectoryAsync(startOptions.InitialWorkingDirectory, CancellationToken.None).ConfigureAwait(false);
258+
_logger.LogDebug($"Setting InitialWorkingDirectory to {startOptions.InitialWorkingDirectory}...");
259+
await SetInitialWorkingDirectoryAsync(startOptions.InitialWorkingDirectory, cancellationToken).ConfigureAwait(false);
260+
_logger.LogDebug("InitialWorkingDirectory set!");
258261
}
259262

260263
await _started.Task.ConfigureAwait(false);
@@ -269,6 +272,7 @@ public Task StopAsync()
269272

270273
public void TriggerShutdown()
271274
{
275+
_logger.LogDebug("Shutting down host...");
272276
if (Interlocked.Exchange(ref _shuttingDown, 1) == 0)
273277
{
274278
_cancellationContext.CancelCurrentTaskStack();
@@ -680,7 +684,7 @@ private void RunTopLevelExecutionLoop()
680684
return;
681685
}
682686

683-
_logger.LogInformation("PSES pipeline thread loop shutting down");
687+
_logger.LogDebug("PSES pipeline thread loop shutting down");
684688
_stopped.SetResult(true);
685689
}
686690

src/PowerShellEditorServices/Services/Symbols/Vistors/AstOperations.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,7 @@ public static async Task<CommandCompletion> GetCompletionsAsync(
7676
{
7777
IScriptPosition cursorPosition = s_clonePositionWithNewOffset(scriptAst.Extent.StartScriptPosition, fileOffset);
7878

79-
logger.LogTrace(
80-
string.Format(
81-
"Getting completions at offset {0} (line: {1}, column: {2})",
82-
fileOffset,
83-
cursorPosition.LineNumber,
84-
cursorPosition.ColumnNumber));
79+
logger.LogTrace($"Getting completions at offset {fileOffset} (line: {cursorPosition.LineNumber}, column: {cursorPosition.ColumnNumber})");
8580

8681
Stopwatch stopwatch = new();
8782

@@ -103,7 +98,7 @@ await executionService.ExecuteDelegateAsync(
10398
.ConfigureAwait(false);
10499

105100
stopwatch.Stop();
106-
logger.LogTrace($"IntelliSense completed in {stopwatch.ElapsedMilliseconds}ms.");
101+
logger.LogTrace($"IntelliSense completed in {stopwatch.ElapsedMilliseconds}ms: {commandCompletion}");
107102

108103
return commandCompletion;
109104
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeLensHandlers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override Task<CodeLensContainer> Handle(CodeLensParams request, Cancellat
4747

4848
public override Task<CodeLens> Handle(CodeLens request, CancellationToken cancellationToken)
4949
{
50-
// TODO: Catch deserializtion exception on bad object
50+
// TODO: Catch deserialization exception on bad object
5151
CodeLensData codeLensData = request.Data.ToObject<CodeLensData>();
5252

5353
ICodeLensProvider originalProvider = _symbolsService

src/PowerShellEditorServices/Services/TextDocument/Handlers/CompletionHandler.cs

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ internal async Task<IEnumerable<CompletionItem>> GetCompletionsInFileAsync(
144144
for (int i = 0; i < result.CompletionMatches.Count; i++)
145145
{
146146
completionItems[i] = CreateCompletionItem(result.CompletionMatches[i], replacedRange, i + 1);
147+
_logger.LogTrace("Created completion item: " + completionItems[i] + " with " + completionItems[i].TextEdit);
147148
}
148149
return completionItems;
149150
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentHighlightHandler.cs

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

4+
using System.Collections.Generic;
5+
using System.Threading;
6+
using System.Threading.Tasks;
47
using Microsoft.Extensions.Logging;
58
using Microsoft.PowerShell.EditorServices.Services;
69
using Microsoft.PowerShell.EditorServices.Services.Symbols;
@@ -9,9 +12,6 @@
912
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1013
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
1114
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
12-
using System.Collections.Generic;
13-
using System.Threading;
14-
using System.Threading.Tasks;
1515

1616
namespace Microsoft.PowerShell.EditorServices.Handlers
1717
{
@@ -27,7 +27,6 @@ public PsesDocumentHighlightHandler(
2727
{
2828
_logger = loggerFactory.CreateLogger<PsesDocumentHighlightHandler>();
2929
_workspaceService = workspaceService;
30-
_logger.LogInformation("highlight handler loaded");
3130
}
3231

3332
protected override DocumentHighlightRegistrationOptions CreateRegistrationOptions(DocumentHighlightCapability capability, ClientCapabilities clientCapabilities) => new()
@@ -46,7 +45,7 @@ public override Task<DocumentHighlightContainer> Handle(
4645
request.Position.Line + 1,
4746
request.Position.Character + 1);
4847

49-
if (symbolOccurrences == null)
48+
if (symbolOccurrences is null)
5049
{
5150
return Task.FromResult(s_emptyHighlightContainer);
5251
}
@@ -60,6 +59,7 @@ public override Task<DocumentHighlightContainer> Handle(
6059
Range = symbolOccurrences[i].ScriptRegion.ToRange()
6160
};
6261
}
62+
_logger.LogDebug("Highlights: " + highlights);
6363

6464
return Task.FromResult(new DocumentHighlightContainer(highlights));
6565
}

0 commit comments

Comments
 (0)