Skip to content

Commit 02966c1

Browse files
committed
Fix breakpoint removal; add "close" request
The first change fixes breakpoint removal. Previously, breakpoints were being tracked per-file based on the script's file path. It was discovered that file paths were being sent through the protocol with inconsistent casing, so path normalization was needed to ensure that breakpoints were being indexed with the correct path casing. The second change adds the "close" request for closing opened files. This request was not being used by VS Code previously but was suddenly getting sent in newer builds of that editor.
1 parent 36fb629 commit 02966c1

File tree

8 files changed

+78
-13
lines changed

8 files changed

+78
-13
lines changed

src/PowerShellEditorServices.Transport.Stdio/PowerShellEditorServices.Transport.Stdio.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="Properties\AssemblyInfo.cs" />
9090
<Compile Include="Request\AttachRequest.cs" />
9191
<Compile Include="Request\ChangeFileRequest.cs" />
92+
<Compile Include="Request\CloseFileRequest.cs" />
9293
<Compile Include="Request\CompletionDetailsRequest.cs" />
9394
<Compile Include="Request\CompletionsRequest.cs" />
9495
<Compile Include="Request\ContinueRequest.cs" />
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.PowerShell.EditorServices.Session;
7+
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Message;
8+
using Nito.AsyncEx;
9+
using System.Threading.Tasks;
10+
11+
namespace Microsoft.PowerShell.EditorServices.Transport.Stdio.Request
12+
{
13+
[MessageTypeName("close")]
14+
public class CloseFileRequest : RequestBase<FileRequestArguments>
15+
{
16+
public static CloseFileRequest Create(string filePath)
17+
{
18+
return new CloseFileRequest
19+
{
20+
Arguments = new FileRequestArguments
21+
{
22+
File = filePath
23+
}
24+
};
25+
}
26+
27+
public override Task ProcessMessage(
28+
EditorSession editorSession,
29+
MessageWriter messageWriter)
30+
{
31+
// Find and close the file in the current session
32+
var fileToClose = editorSession.Workspace.GetFile(this.Arguments.File);
33+
34+
if (fileToClose != null)
35+
{
36+
editorSession.Workspace.CloseFile(fileToClose);
37+
}
38+
39+
return TaskConstants.Completed;
40+
}
41+
}
42+
}

src/PowerShellEditorServices.Transport.Stdio/Request/SetBreakpointsRequest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public override async Task ProcessMessage(
1919
EditorSession editorSession,
2020
MessageWriter messageWriter)
2121
{
22-
// TODO: Catch FileNotFoundException
2322
ScriptFile scriptFile =
2423
editorSession.Workspace.GetFile(
2524
this.Arguments.Source.Path);

src/PowerShellEditorServices/Console/PowerShellSession.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,16 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
192192
if (Thread.CurrentThread.ManagedThreadId != this.pipelineThreadId &&
193193
this.pipelineExecutionTask != null)
194194
{
195+
PipelineExecutionRequest<TResult> executionRequest =
196+
new PipelineExecutionRequest<TResult>(
197+
this, psCommand, sendOutputToHost);
198+
195199
// Send the pipeline execution request to the pipeline thread
196200
this.pipelineResultTask = new TaskCompletionSource<IPipelineExecutionRequest>();
197-
this.pipelineExecutionTask.SetResult(
198-
new PipelineExecutionRequest<TResult>(
199-
this, psCommand, sendOutputToHost));
201+
this.pipelineExecutionTask.SetResult(executionRequest);
200202

201203
await this.pipelineResultTask.Task;
204+
return executionRequest.Results;
202205
}
203206
else
204207
{

src/PowerShellEditorServices/DebugService.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private async Task ClearBreakpointsInFile(ScriptFile scriptFile)
203203
List<Breakpoint> breakpoints = null;
204204

205205
// Get the list of breakpoints for this file
206-
if (this.breakpointsPerFile.TryGetValue(scriptFile.FilePath, out breakpoints))
206+
if (this.breakpointsPerFile.TryGetValue(scriptFile.Id, out breakpoints))
207207
{
208208
if (breakpoints.Count > 0)
209209
{
@@ -212,6 +212,9 @@ private async Task ClearBreakpointsInFile(ScriptFile scriptFile)
212212
psCommand.AddParameter("Breakpoint", breakpoints.ToArray());
213213

214214
await this.powerShellSession.ExecuteCommand<object>(psCommand);
215+
216+
// Clear the existing breakpoints list for the file
217+
breakpoints.Clear();
215218
}
216219
}
217220
}
@@ -275,12 +278,15 @@ private void OnBreakpointUpdated(object sender, BreakpointUpdatedEventArgs e)
275278
{
276279
List<Breakpoint> breakpoints = null;
277280

281+
// Normalize the script filename for proper indexing
282+
string normalizedScriptName = e.Breakpoint.Script.ToLower();
283+
278284
// Get the list of breakpoints for this file
279-
if (!this.breakpointsPerFile.TryGetValue(e.Breakpoint.Script, out breakpoints))
285+
if (!this.breakpointsPerFile.TryGetValue(normalizedScriptName, out breakpoints))
280286
{
281287
breakpoints = new List<Breakpoint>();
282288
this.breakpointsPerFile.Add(
283-
e.Breakpoint.Script,
289+
normalizedScriptName,
284290
breakpoints);
285291
}
286292

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public async Task<CompletionResults> GetCompletionsInFile(
102102

103103
// save state of most recent completion
104104
mostRecentCompletions = completionResults;
105-
mostRecentRequestFile = scriptFile.FilePath;
105+
mostRecentRequestFile = scriptFile.Id;
106106
mostRecentRequestLine = lineNumber;
107107
mostRecentRequestOffest = columnNumber;
108108

@@ -124,7 +124,7 @@ public CompletionDetails GetCompletionDetailsInFile(
124124
string entryName)
125125
{
126126
// Makes sure the most recent completions request was the same line and column as this request
127-
if (file.FilePath.Equals(mostRecentRequestFile) &&
127+
if (file.Id.Equals(mostRecentRequestFile) &&
128128
lineNumber == mostRecentRequestLine &&
129129
columnNumber == mostRecentRequestOffest)
130130
{

src/PowerShellEditorServices/Session/ScriptFile.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ public class ScriptFile
2828
#region Properties
2929

3030
/// <summary>
31-
/// Gets the path at which this file resides.
31+
/// Gets a unique string that identifies this file. At this time,
32+
/// this property returns a normalized version of the value stored
33+
/// in the FilePath property.
34+
/// </summary>
35+
public string Id
36+
{
37+
get { return this.FilePath.ToLower(); }
38+
}
39+
40+
/// <summary>
41+
/// Gets the path at which this file resides.
3242
/// </summary>
3343
public string FilePath { get; private set; }
3444

src/PowerShellEditorServices/Session/Workspace.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void CloseFile(ScriptFile scriptFile)
7373
{
7474
Validate.IsNotNull("scriptFile", scriptFile);
7575

76-
this.workspaceFiles.Remove(scriptFile.FilePath);
76+
this.workspaceFiles.Remove(scriptFile.Id);
7777
}
7878

7979
/// <summary>
@@ -89,12 +89,12 @@ public ScriptFile[] ExpandScriptReferences(ScriptFile scriptFile)
8989
List<ScriptFile> expandedReferences = new List<ScriptFile>();
9090

9191
// add original file so it's not searched for, then find all file references
92-
referencedScriptFiles.Add(scriptFile.FilePath, scriptFile);
92+
referencedScriptFiles.Add(scriptFile.Id, scriptFile);
9393
RecursivelyFindReferences(scriptFile, referencedScriptFiles);
9494

9595
// remove original file from referened file and add it as the first element of the
9696
// expanded referenced list to maintain order so the original file is always first in the list
97-
referencedScriptFiles.Remove(scriptFile.FilePath);
97+
referencedScriptFiles.Remove(scriptFile.Id);
9898
expandedReferences.Add(scriptFile);
9999

100100
if (referencedScriptFiles.Count > 0)
@@ -132,6 +132,10 @@ private void RecursivelyFindReferences(
132132
{
133133
// Get the referenced file if it's not already in referencedScriptFiles
134134
referencedFile = this.GetFile(resolvedScriptPath);
135+
136+
// Normalize the resolved script path and add it to the
137+
// referenced files list if it isn't there already
138+
resolvedScriptPath = resolvedScriptPath.ToLower();
135139
if (!referencedScriptFiles.ContainsKey(resolvedScriptPath))
136140
{
137141
referencedScriptFiles.Add(resolvedScriptPath, referencedFile);

0 commit comments

Comments
 (0)