Skip to content

Commit f639b3c

Browse files
committed
Caught another case where we need to unescape a path (cwd) sent via the launch request from vscode.
Also replaced the PowerShell code to execute Set-Location, with a call to SessionStateProxy.Path.SetLocation. I think this should be safer.
1 parent 5757058 commit f639b3c

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ protected async Task HandleLaunchRequest(
8585
// In case that is null, use the the folder of the script to be executed. If the resulting
8686
// working dir path is a file path then extract the directory and use that.
8787
string workingDir = launchParams.Cwd ?? launchParams.Program;
88+
workingDir = PowerShellContext.UnescapePath(workingDir);
8889
try
8990
{
9091
if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
@@ -94,16 +95,11 @@ protected async Task HandleLaunchRequest(
9495
}
9596
catch (Exception ex)
9697
{
97-
Logger.Write(LogLevel.Error, "cwd path is bad: " + ex.Message);
98+
Logger.Write(LogLevel.Error, "cwd path is invalid: " + ex.Message);
9899
workingDir = Environment.CurrentDirectory;
99100
}
100101

101-
var setWorkingDirCommand = new PSCommand();
102-
setWorkingDirCommand.AddCommand(@"Microsoft.PowerShell.Management\Set-Location")
103-
.AddParameter("LiteralPath", workingDir);
104-
105-
await editorSession.PowerShellContext.ExecuteCommand(setWorkingDirCommand);
106-
102+
editorSession.PowerShellContext.SetWorkingDirectory(workingDir);
107103
Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir);
108104

109105
// Prepare arguments to the script - if specified

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,23 @@ internal void ReleaseRunspaceHandle(RunspaceHandle runspaceHandle)
577577
}
578578
}
579579

580+
/// <summary>
581+
/// Sets the current working directory of the powershell context. The path should be
582+
/// unescaped before calling this method.
583+
/// </summary>
584+
/// <param name="path"></param>
585+
public void SetWorkingDirectory(string path)
586+
{
587+
this.currentRunspace.SessionStateProxy.Path.SetLocation(path);
588+
}
589+
580590
/// <summary>
581591
/// Returns the passed in path with the [ and ] characters escaped. Escaping spaces is optional.
582592
/// </summary>
583593
/// <param name="path">The path to process.</param>
584594
/// <param name="escapeSpaces">Specify True to escape spaces in the path, otherwise False.</param>
585595
/// <returns>The path with [ and ] escaped.</returns>
586-
internal static string EscapePath(string path, bool escapeSpaces)
596+
public static string EscapePath(string path, bool escapeSpaces)
587597
{
588598
string escapedPath = Regex.Replace(path, @"(?<!`)\[", "`[");
589599
escapedPath = Regex.Replace(escapedPath, @"(?<!`)\]", "`]");
@@ -602,7 +612,7 @@ internal static string EscapePath(string path, bool escapeSpaces)
602612
/// </summary>
603613
/// <param name="path">The path to unescape.</param>
604614
/// <returns>The path with the ` character before [, ] and spaces removed.</returns>
605-
internal static string UnescapePath(string path)
615+
public static string UnescapePath(string path)
606616
{
607617
if (!path.Contains("`"))
608618
{

0 commit comments

Comments
 (0)