Skip to content

Commit 0d77907

Browse files
committed
Handle launched scripts not backed by a ScriptFile
Specifically, just a raw script.
1 parent 1818bf7 commit 0d77907

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/ConfigurationDoneHandler.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ public Task<ConfigurationDoneResponse> Handle(ConfigurationDoneArguments request
105105
private async Task LaunchScriptAsync(string scriptToLaunch)
106106
{
107107
PSCommand command;
108-
if (ScriptFile.IsUntitledPath(scriptToLaunch))
108+
// Script could an actual script, or a URI to a script file (or untitled document).
109+
if (!System.Uri.IsWellFormedUriString(scriptToLaunch, System.UriKind.RelativeOrAbsolute)
110+
|| ScriptFile.IsUntitledPath(scriptToLaunch))
109111
{
110-
ScriptFile untitledScript = _workspaceService.GetFile(scriptToLaunch);
111-
if (BreakpointApiUtils.SupportsBreakpointApis(_runspaceContext.CurrentRunspace))
112+
bool isScriptFile = _workspaceService.TryGetFile(scriptToLaunch, out ScriptFile untitledScript);
113+
if (isScriptFile && BreakpointApiUtils.SupportsBreakpointApis(_runspaceContext.CurrentRunspace))
112114
{
113115
// Parse untitled files with their `Untitled:` URI as the filename which will
114116
// cache the URI and contents within the PowerShell parser. By doing this, we
@@ -138,7 +140,11 @@ private async Task LaunchScriptAsync(string scriptToLaunch)
138140
// Command breakpoints and `Wait-Debugger` will work. We must wrap the script
139141
// with newlines so that any included comments don't break the command.
140142
command = PSCommandHelpers.BuildDotSourceCommandWithArguments(
141-
string.Concat("{\n", untitledScript.Contents, "\n}"), _debugStateService.Arguments);
143+
string.Concat(
144+
"{" + System.Environment.NewLine,
145+
isScriptFile ? untitledScript.Contents : scriptToLaunch,
146+
System.Environment.NewLine + "}"),
147+
_debugStateService.Arguments);
142148
}
143149
}
144150
else

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,12 @@ public ScriptFile GetFile(DocumentUri documentUri)
152152
/// </summary>
153153
/// <param name="filePath">The file path at which the script resides.</param>
154154
/// <param name="scriptFile">The out parameter that will contain the ScriptFile object.</param>
155-
public bool TryGetFile(string filePath, out ScriptFile scriptFile) =>
156-
TryGetFile(new Uri(filePath), out scriptFile);
155+
public bool TryGetFile(string filePath, out ScriptFile scriptFile)
156+
{
157+
scriptFile = null;
158+
return Uri.IsWellFormedUriString(filePath, UriKind.RelativeOrAbsolute)
159+
&& TryGetFile(new Uri(filePath), out scriptFile);
160+
}
157161

158162
/// <summary>
159163
/// Tries to get an open file in the workspace. Returns true if it succeeds, false otherwise.

test/PowerShellEditorServices.Test.E2E/DebugAdapterClientExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ namespace PowerShellEditorServices.Test.E2E
1212
{
1313
public static class DebugAdapterClientExtensions
1414
{
15-
public static async Task LaunchScript(this DebugAdapterClient debugAdapterClient, string filePath, TaskCompletionSource<object> started)
15+
public static async Task LaunchScript(this DebugAdapterClient debugAdapterClient, string script, TaskCompletionSource<object> started)
1616
{
1717
LaunchResponse launchResponse = await debugAdapterClient.Launch(
1818
new PsesLaunchRequestArguments
1919
{
2020
NoDebug = false,
21-
Script = filePath,
21+
Script = script,
2222
Cwd = "",
2323
CreateTemporaryIntegratedConsole = false
2424
}).ConfigureAwait(true);

0 commit comments

Comments
 (0)