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
tab completion of arguments (file paths, variable names, etc) fails to escape some special characters #9881
Comments
/cc @kwkam |
@iSazonov It looks like the parser also treats non-ascii quote symbols as quoting characters. Is this and expected behaviour in PowerShell? or should the quoting be limited to the ascii |
It's by design. The behaviour was introduced to minimise annoyances when copying code via some other application that may want to use smart quotes for whatever reason. |
If we look the error output: Get-Content C:\temp\‘hello’.txt
Get-Content : Cannot find path 'C:\temp\hello.txt' because it does not exist.
At line:1 char:1
+ Get-Content C:\temp\‘hello’.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\temp\hello.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand we can see that PowerShell eats the quote chars - If the chars worked as quotes follow would works but do not: Get-Content .\‘‘hello’’.txt
Get-Content : Cannot find path 'C:\temp\hello.txt' because it does not exist.
At line:1 char:1
+ Get-Content .\‘‘hello’’.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\temp\hello.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand and again we see that PowerShell search If we use another Unicode chars this works as expected: echo hello >"`u{2020}hello`u{2021}.txt"
Get-Content .\†hello‡.txt
hello |
This issue is about the tab completer, not the parser. The parser is working as it should. The tab completer failed to escape the special characters in the path when it was accepted for completion of an argument. |
The same for single quotes: Get-Content .\'hello'.txt
Get-Content : Cannot find path 'C:\temp\hello.txt' because it does not exist.
At line:1 char:1
+ Get-Content .\'hello'.txt |
I think the issue is in PowerShell/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs Line 6702 in 981c990
|
@msftrncs That's interesting, would you please create a file, for example, Write-Host -NoNewline 'Without quote: ['
Write-Host -NoNewline ‘hello’.txt
Write-Host ']'
Write-Host -NoNewline 'Single quoted: ['
Write-Host -NoNewline '‘hello’.txt'
Write-Host ']'
Write-Host -NoNewline 'Double quoted: ['
Write-Host -NoNewline "‘hello’.txt"
Write-Host ']' and execute it to see if you get the output as below?
|
The result is as expected:
To show context (VS Code, using PowerShell/EditorSyntax#156) : The fancy quotes ( |
I tested most of the rest of the potentially special characters with tab completion, and the only ones that seem to not be properly handled are |
If somebody want to fix this see my code and PR references above. |
@iSazonov, I might think this line(s) could cause problems: PowerShell/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs Lines 237 to 241 in 981c990
There are approximately 16 of these such constructs in this file. Also, I misstated the problem earlier when I said that only the single quotes seem affected. After realizing the issue, I can also demonstrate failure of curly double quotes. Thus the following code is also probably short of what it needs to be. PowerShell/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs Lines 4388 to 4392 in 981c990
To demonstrate, generate a file name with: echo hello > "hello`u{201c}there.txt" Then try to tab complete it while encapsulating the starting file name in doublequotes: gc "hello<TAB>“ Result is:
|
@msftrncs If it is expected that the curly single quotes are behaving as quoting character, please change the Expected behaviour to, for example, get-content '.\‘‘hello’’.txt' so others will not get confused. |
Interesting, @kwkam, that was my fault that I did not double quote the expected sample, as I was caught up on the fact that it didn't even bother to wrap the whole argument in quotes. However, a further issue is that sometimes it does wrap the argument in quotes and some times it does not, and now I will have to figure out that pattern. I think it was because I used a special quote as the first character of the file name, and I think I have this confirmed now as well, so I will revise the OP to demonstrate both scenario's. |
https://gist.github.com/msftrncs/bff8c6c5e28ff92a19efb8a5556a4238 I mention this gist here as it demonstrates a PS filter that is able to handle the basic escaping of an argument for use by tab completion. |
I've changed the title of this issue as I have determined its more widespread of a problem. It would appear that all argument type completers fail to correctly escape arguments with characters such as curly quotes ( Additional points:
I am continuing to work through understanding the completers in depth. |
I have an intention to post a PR regarding at least parts of this issue, implementing the escapers already found in the |
@msftrncs Before pull the PR please review tests. If you find any gaps in the tests we need to pull new tests before to exclude regressions. |
Enhance all argument completers to use QuoteArgument code generator to insure consistent quoting and escaping by all argument completion completers. Fixes PowerShell#9881 Fixes PowerShell#7569
Enhance all argument completers to use QuoteArgument code generator to insure consistent quoting and escaping by all argument completion completers. Fixes PowerShell#9881 Fixes PowerShell#7569
Enhance all argument completers to use QuoteArgument code generator to insure consistent quoting and escaping by all argument completion completers. Fixes PowerShell#9881 Fixes PowerShell#7569
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
2 similar comments
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
EDIT: 2019-06-25 - Additional conditions and clarifications, corrected expected behavior as requiring the curly quotes to be doubled, added second example demonstrating the tab completion when the first character of the file name is not a quote character, added third example for curly double quotes
I am not sure what is really responsible for tab completion of path names in PowerShell, but here is what I found while testing a similar function in posh-git.
Steps to reproduce
Example 1
Example 2
Example 3
Expected behavior
Example 1
Example 2
Example 3
Actual behavior
Example 1
This generates an error saying the file
hello.txt
cannot be found. Note this particular behavior is regardless of the type of quote used, as long as the quote is a legal filename character and is at the start of the file name and is a special character for PowerShell.Example 2
This will cause PSReadLine to want to continue a multiline input because the quoted string is still open.
Example 3
This will cause PSReadLine to want to continue a multiline input because the quoted string is still open.
Environment data
In dahlbyk/posh-git#683 (comment) @rkeithhill commented that it would be nice if PowerShell has a static method for escaping arguments, and it was that comment that triggered me to test this possibility. I have not tried to test all characters that could be special but found in file names to see if any more are missing escaping.
Another issue with tab completion, server/share names with spaces, #7569, but this only affects the path while it is just '\server\share with a space'...
The text was updated successfully, but these errors were encountered: