Skip to content

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

Closed

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.

Activity

  1. added
    Issue-Question
    on Jul 24, 2019
  2. added a commit that references this issue on Jul 24, 2019
    397988b
  3. added
    WG-Engine
    on Jul 24, 2019
  4. msftrncs commented on Jul 27, 2019

    @msftrncs
    ContributorAuthor

    Additionally, and this technically affects all argument completions but is more likely to occur in ValidateSet, argument completion values that start with a dash and follow with a valid identifier start character, or are just dash dash (--) should be quoted or escaped, to prevent them being parsed as a parameter name.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  8. added
    Resolution-No Activity
    on Nov 16, 2023
  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  10. added
    In-PR
    on Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIssue-QuestionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions