Skip to content

Commit 063b782

Browse files
author
Robert Holt
committed
Correctly escape path to script
1 parent ce20e65 commit 063b782

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ protected async Task HandleLaunchRequest(
259259
// the path exists and is a directory.
260260
if (!string.IsNullOrEmpty(workingDir))
261261
{
262-
workingDir = PowerShellContext.UnescapeWildcardEscapedPath(workingDir);
263262
try
264263
{
265264
if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
@@ -303,7 +302,16 @@ protected async Task HandleLaunchRequest(
303302
string arguments = null;
304303
if ((launchParams.Args != null) && (launchParams.Args.Length > 0))
305304
{
306-
arguments = string.Join(" ", launchParams.Args);
305+
var sb = new StringBuilder();
306+
for (int i = 0; i < launchParams.Args.Length; i++)
307+
{
308+
sb.Append(PowerShellContext.QuoteEscapeString(launchParams.Args[i]));
309+
if (i < launchParams.Args.Length - 1)
310+
{
311+
sb.Append(' ');
312+
}
313+
}
314+
arguments = sb.ToString();
307315
Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
308316
}
309317

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,8 @@ await this.ExecuteCommand<object>(
769769
/// <returns>A Task that can be awaited for completion.</returns>
770770
public async Task ExecuteScriptWithArgs(string script, string arguments = null, bool writeInputToHost = false)
771771
{
772-
string launchedScript = script;
772+
string reportedScript = script;
773+
string escapedScriptPath = PowerShellContext.WildcardEscapePath(script);
773774
PSCommand command = new PSCommand();
774775

775776
if (arguments != null)
@@ -797,21 +798,22 @@ public async Task ExecuteScriptWithArgs(string script, string arguments = null,
797798
if (File.Exists(script) || File.Exists(scriptAbsPath))
798799
{
799800
// Dot-source the launched script path
800-
script = ". " + QuoteEscapeString(script);
801+
escapedScriptPath = ". " + QuoteEscapeString(escapedScriptPath);
801802
}
802803

803-
launchedScript = script + " " + arguments;
804-
command.AddScript(launchedScript, false);
804+
reportedScript += " " + arguments;
805+
escapedScriptPath = escapedScriptPath + " " + arguments;
806+
command.AddScript(escapedScriptPath, false);
805807
}
806808
else
807809
{
808-
command.AddCommand(script, false);
810+
command.AddCommand(escapedScriptPath, false);
809811
}
810812

811813
if (writeInputToHost)
812814
{
813815
this.WriteOutput(
814-
launchedScript + Environment.NewLine,
816+
script + Environment.NewLine,
815817
true);
816818
}
817819

@@ -1183,6 +1185,7 @@ internal static string WildcardEscapePath(string path, bool escapeSpaces = false
11831185
case ']':
11841186
case '*':
11851187
case '?':
1188+
case '`':
11861189
sb.Append('`').Append(curr);
11871190
break;
11881191

0 commit comments

Comments
 (0)