Skip to content

Commit 4ee2af5

Browse files
committed
Merge pull request #205 from rkeithhill/rkeithhill/is195-bkpts-fail-nonexisting-files
Fixes #195 - set breakpoint in non-existing file doesn't crash
2 parents 7a32fa4 + da2c87e commit 4ee2af5

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/PowerShellEditorServices.Protocol/DebugAdapter/Breakpoint.cs

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.PowerShell.EditorServices.Utility;
7+
68
namespace Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter
79
{
810
public class Breakpoint
@@ -32,6 +34,8 @@ private Breakpoint()
3234
public static Breakpoint Create(
3335
BreakpointDetails breakpointDetails)
3436
{
37+
Validate.IsNotNull(nameof(breakpointDetails), breakpointDetails);
38+
3539
return new Breakpoint
3640
{
3741
Verified = breakpointDetails.Verified,
@@ -45,10 +49,31 @@ public static Breakpoint Create(
4549
public static Breakpoint Create(
4650
CommandBreakpointDetails breakpointDetails)
4751
{
52+
Validate.IsNotNull(nameof(breakpointDetails), breakpointDetails);
53+
4854
return new Breakpoint {
4955
Verified = breakpointDetails.Verified,
5056
Message = breakpointDetails.Message
5157
};
5258
}
59+
60+
public static Breakpoint Create(
61+
SourceBreakpoint sourceBreakpoint,
62+
string source,
63+
string message,
64+
bool verified = false)
65+
{
66+
Validate.IsNotNull(nameof(sourceBreakpoint), sourceBreakpoint);
67+
Validate.IsNotNull(nameof(source), source);
68+
Validate.IsNotNull(nameof(message), message);
69+
70+
return new Breakpoint {
71+
Verified = verified,
72+
Message = message,
73+
Source = source,
74+
Line = sourceBreakpoint.Line,
75+
Column = sourceBreakpoint.Column
76+
};
77+
}
5378
}
5479
}

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

+26-3
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,32 @@ protected async Task HandleSetBreakpointsRequest(
211211
SetBreakpointsRequestArguments setBreakpointsParams,
212212
RequestContext<SetBreakpointsResponseBody> requestContext)
213213
{
214-
ScriptFile scriptFile =
215-
editorSession.Workspace.GetFile(
216-
setBreakpointsParams.Source.Path);
214+
ScriptFile scriptFile;
215+
216+
// Fix for issue #195 - user can change name of file outside of VSCode in which case
217+
// VSCode sends breakpoint requests with the original filename that doesn't exist anymore.
218+
try
219+
{
220+
scriptFile = editorSession.Workspace.GetFile(setBreakpointsParams.Source.Path);
221+
}
222+
catch (FileNotFoundException)
223+
{
224+
Logger.Write(
225+
LogLevel.Warning,
226+
$"Attempted to set breakpoints on a non-existing file: {setBreakpointsParams.Source.Path}");
227+
228+
var srcBreakpoints = setBreakpointsParams.Breakpoints
229+
.Select(srcBkpt => Protocol.DebugAdapter.Breakpoint.Create(
230+
srcBkpt, setBreakpointsParams.Source.Path, "Source does not exist, breakpoint not set."));
231+
232+
// Return non-verified breakpoint message.
233+
await requestContext.SendResult(
234+
new SetBreakpointsResponseBody {
235+
Breakpoints = srcBreakpoints.ToArray()
236+
});
237+
238+
return;
239+
}
217240

218241
var breakpointDetails = new BreakpointDetails[setBreakpointsParams.Breakpoints.Length];
219242
for (int i = 0; i < breakpointDetails.Length; i++)

0 commit comments

Comments
 (0)