Skip to content

completions of [validateset] parameters fails to escape special characters #10218

@msftrncs

Description

@msftrncs

Steps to reproduce

function test ([validateset('$test', 'test$', 'test test', '#', '# hello', '#requires more', '<# hello #>')]$test) {echo $test}

test <ALT-SPACE>   # value `$test` will not be escaped or quoted due to `$`, nor will the completions that start with `#`, except `#requires more` gets quoted
# also, `<# hello #>` fails to be quoted
test "<ALT-SPAC>"  # values `$test` and `test$` will not have the `$` escaped

Expected behavior

All special characters (those affecting tokenizing the argument correct intentions) would force quoting or escaping.

Actual behavior

Some characters do, some do not.

This is due to a reliance of using the parser (via CompletionRequiresQuotes() method) to determine if there is more than 1 token, or if the parser has an error parsing the tokens contained in the completion. In the case of $test the parser sees exactly the one successful token and no more. I've also determined the same is happening for #

I understand why the parser is being used, but I think the idea is flawed because the parser doesn't know the full context of the completion, and can be swayed, as I have done here with the $test argument value.

Environment data

PowerShell 7.0.0-preview.2

Considerations

This is partially related to other completion bugs I have been reporting. I'd like to consider writing a more specific helper function/method for handling argument completions that can correctly determine if an argument needs quoted, and/or (if already quoted) properly escaped, and would not rely directly on the parser. I have covered this idea in #9881, and I am working on converting the PowerShell code example to C#. Using this helper, all argument type completions will be funneled through a single method for proper quoting and escaping. This does mean that making tweaks to the parser that might affect how a completion needs to be quoted/escaped would require tweaking this helper as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issueIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions