Skip to content

Commit da43937

Browse files
committed
Avoid mangling paths given to ExecuteScriptWithArgsAsync
Some extant code was using PowerShell to get the current working directory and then was hardcoded to prepend that to the given path, in order to make an "absolute path." This was buggy, so we fixed it by only prefixing non-rooted paths with the CWD, and do so with `Path.Combine`.
1 parent 66ba44e commit da43937

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

+22-20
Original file line numberDiff line numberDiff line change
@@ -1040,26 +1040,28 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
10401040

10411041
if (arguments != null)
10421042
{
1043-
// Need to determine If the script string is a path to a script file.
1044-
string scriptAbsPath = string.Empty;
1045-
try
1046-
{
1047-
// Assume we can only debug scripts from the FileSystem provider
1048-
string workingDir = (await ExecuteCommandAsync<PathInfo>(
1049-
new PSCommand()
1050-
.AddCommand("Microsoft.PowerShell.Management\\Get-Location")
1051-
.AddParameter("PSProvider", "FileSystem"),
1052-
sendOutputToHost: false,
1053-
sendErrorToHost: false).ConfigureAwait(false))
1054-
.FirstOrDefault()
1055-
.ProviderPath;
1056-
1057-
workingDir = workingDir.TrimEnd(Path.DirectorySeparatorChar);
1058-
scriptAbsPath = workingDir + Path.DirectorySeparatorChar + script;
1059-
}
1060-
catch (System.Management.Automation.DriveNotFoundException e)
1043+
// Add CWD from PowerShell if not an absolute path
1044+
if (!Path.IsPathRooted(script))
10611045
{
1062-
this.logger.LogHandledException("Could not determine current filesystem location", e);
1046+
try
1047+
{
1048+
// Assume we can only debug scripts from the FileSystem provider
1049+
string workingDir = (await ExecuteCommandAsync<PathInfo>(
1050+
new PSCommand()
1051+
.AddCommand("Microsoft.PowerShell.Management\\Get-Location")
1052+
.AddParameter("PSProvider", "FileSystem"),
1053+
sendOutputToHost: false,
1054+
sendErrorToHost: false).ConfigureAwait(false))
1055+
.FirstOrDefault()
1056+
.ProviderPath;
1057+
1058+
this.logger.LogTrace($"Prepending working directory {workingDir} to script path {script}");
1059+
script = Path.Combine(workingDir, script);
1060+
}
1061+
catch (System.Management.Automation.DriveNotFoundException e)
1062+
{
1063+
this.logger.LogHandledException("Could not determine current filesystem location", e);
1064+
}
10631065
}
10641066

10651067
var strBld = new StringBuilder();
@@ -1071,7 +1073,7 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
10711073
// If the provided path is already quoted, then File.Exists will not find it.
10721074
// This keeps us from quoting an already quoted path.
10731075
// Related to issue #123.
1074-
if (File.Exists(script) || File.Exists(scriptAbsPath))
1076+
if (File.Exists(script))
10751077
{
10761078
// Dot-source the launched script path and single quote the path in case it includes
10771079
strBld.Append(". ").Append(QuoteEscapeString(script));

0 commit comments

Comments
 (0)