Skip to content
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

Fix tilde expansion to handle implicit quotes due to escaped whitespace #21277

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -287,11 +287,10 @@ private void AppendOneNativeArgument(ExecutionContext context, CommandParameterI
if (NeedQuotes(arg))
{
_arguments.Append('"');
AddToArgumentList(parameter, arg);
PossiblyGlobArg(arg, parameter, usedQuotes: false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since everything is going to be possibly globbed, we may get some additional globbing (such as expanded wildcards and variable resolution). That may be a good thing, but it may be more aggressive than we want. should we be checking explicitly for the ~ before passing it to the globber?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the lack of globbing was a bug previously as something like:

1 > "hello world.txt"
ls hello` world.txt
ls hello` world*

The first ls works as expected as the native command parameter binder recognizes it needs quotes due to the escaped whitespace. The second case doesn't do globbing which it should and this PR also fixes that.


// need to escape all trailing backslashes so the native command receives it correctly
// according to http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESDOC
_arguments.Append(arg);
for (int i = arg.Length - 1; i >= 0 && arg[i] == '\\'; i--)
{
_arguments.Append('\\');
Expand Down Expand Up @@ -500,7 +499,7 @@ private static string GetEnumerableArgSeparator(ArrayLiteralAst arrayLiteralAst,
afterPrev -= arrayExtent.StartOffset;
beforeNext -= arrayExtent.StartOffset;

if (arrayText[afterPrev] == ',')
if (arrayText[afterPrev] == ',')
{
return ", ";
}
Expand All @@ -509,7 +508,7 @@ private static string GetEnumerableArgSeparator(ArrayLiteralAst arrayLiteralAst,
{
return " ,";
}

return " , ";
}

Expand Down
Expand Up @@ -132,4 +132,7 @@ Describe 'Native UNIX globbing tests' -tags "CI" {
/bin/echo '~/foo' | Should -BeExactly '~/foo'
/bin/echo "~/foo" | Should -BeExactly '~/foo'
}
It '~ should be expanded when used with implicit quotes' {
/bin/echo ~/a` b | Should -BeExactly "$($ExecutionContext.SessionState.Provider.Get("FileSystem").Home)/a b"
}
}
Expand Up @@ -29,4 +29,7 @@ Describe 'Native Windows tilde expansion tests' -tags "CI" {
cmd /c echo '~\foo' | Should -BeExactly '~\foo'
cmd /c echo "~\foo" | Should -BeExactly '~\foo'
}
It '~ should be expanded with implicit quotes' {
cmd /c echo ~\a` b | Should -BeExactly "`"$($ExecutionContext.SessionState.Provider.Get("FileSystem").Home)\a b`""
}
}