Skip to content

Fix Pester CodeLens run/debug by not quoting params/already quoted args #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,7 @@ protected async Task HandleLaunchRequest(
string arguments = null;
if ((launchParams.Args != null) && (launchParams.Args.Length > 0))
{
var sb = new StringBuilder();
for (int i = 0; i < launchParams.Args.Length; i++)
{
sb.Append(PowerShellContext.QuoteEscapeString(launchParams.Args[i]));
if (i < launchParams.Args.Length - 1)
{
sb.Append(' ');
}
}
arguments = sb.ToString();
arguments = string.Join(" ", launchParams.Args);
Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
}

Expand Down
31 changes: 22 additions & 9 deletions src/PowerShellEditorServices/Session/PowerShellContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,6 @@ await this.ExecuteCommand<object>(
/// <returns>A Task that can be awaited for completion.</returns>
public async Task ExecuteScriptWithArgs(string script, string arguments = null, bool writeInputToHost = false)
{
var escapedScriptPath = new StringBuilder(PowerShellContext.WildcardEscapePath(script));
PSCommand command = new PSCommand();

if (arguments != null)
Expand All @@ -791,24 +790,38 @@ public async Task ExecuteScriptWithArgs(string script, string arguments = null,
"Could not determine current filesystem location:\r\n\r\n" + e.ToString());
}

// If we don't escape wildcard characters in a path to a script file, the script can
// fail to execute if say the script filename was foo][.ps1.
var strBld = new StringBuilder();

// The script parameter can refer to either a "script path" or a "command name". If it is a
// script path, we can determine that by seeing if the path exists. If so, we always single
// quote that path in case it includes special PowerShell characters like ', &, (, ), [, ] and
// <space>. Any embedded single quotes are escaped.
// If the provided path is already quoted, then File.Exists will not find it.
// This keeps us from quoting an already quoted path.
// Related to issue #123.
if (File.Exists(script) || File.Exists(scriptAbsPath))
{
// Dot-source the launched script path
string escapedFilePath = escapedScriptPath.ToString();
escapedScriptPath = new StringBuilder(". ").Append(QuoteEscapeString(escapedFilePath));
// Dot-source the launched script path and single quote the path in case it includes
strBld.Append(". ").Append(QuoteEscapeString(script));
}
else
{
strBld.Append(script);
}

// Add arguments
escapedScriptPath.Append(' ').Append(arguments);
strBld.Append(' ').Append(arguments);

var launchedScript = strBld.ToString();
this.logger.Write(LogLevel.Verbose, $"Launch script is: {launchedScript}");

command.AddScript(escapedScriptPath.ToString(), false);
command.AddScript(launchedScript, false);
}
else
{
command.AddCommand(escapedScriptPath.ToString(), false);
// AddCommand can handle script paths including those with special chars e.g.:
// ".\foo & [bar]\foo.ps1" and it can handle arbitrary commands, like "Invoke-Pester"
command.AddCommand(script, false);
}

if (writeInputToHost)
Expand Down