Skip to content

The FileSystem provider does not reliably report invalid wildcard patterns #6733

Closed

Description

See also: #12168

Note: This is an obscure edge case.

For the most part, malformed wildcard patterns are quietly ignored and fall back to literal treatment (e.g., Get-Item a].txt matches a file literally named a].txt) - that inconsistency could be considered problematic in itself, however.

Update: A pattern such as a].txt is actually valid, because the ] is not special unless preceded by [. However, patterns such as a[].txt and a[.txt and a[foo.txt are invalid, and the problem is that they are not consistently reported as such. The rest of this post has been revised accordingly.

When an invalid wildcard pattern - e.g., one that ends in [] before the filename extension - is passed to Get-Item or Get-ChildItem, the error that should occur occurs only if at least two files exists that have the same matching prefix (the exact logic is not clear to me).

(Bruce Payette (@BrucePay) states that the source of the error is the FileSystem provider itself, not the cmdlets mentioned.)

Steps to reproduce

# Assume that no `tmp*.txt` files exist.
Get-Item tmp[].txt
'---'
$null = New-Item -Force tmp.txt; Get-Item tmp[].txt

Expected behavior

Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. 
The specified wildcard character pattern is not valid: tmp[].txt
---
Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. 
The specified wildcard character pattern is not valid: tmp[].txt

That is, two instances of the same The specified wildcard character pattern is not valid error should occur, irrespective of whether any tmp*.txt files exist or not.

Actual behavior

---
Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. 
The specified wildcard character pattern is not valid: tmp[].txt

That is, in the absence of any tmp*.txt files, the invalid wildcard expression was not reported as invalid.

Environment data

Originally reported for 6.0.2, updated for:

PowerShell Core v7.0.0

Activity

  1. BrucePay commented on Apr 26, 2018

    @BrucePay
    Collaborator

    Michael Klement (@mklement0) Just FYI, there is a single wildcard library that is used by everything handling wildcards so the behaviour will be the same everywhere. As a consequence, there is no need to report -like as a separate case.

    WRT your first issue, the character ']' is only special if preceded by '[' which is fine and by design. Regular expressions behave the same way

    PS[1] (351) > "abc]def" -match "abc]"
    True
    PS[1] (353) > "abc]def" -like "abc]*"
    True
    

    Similarly, empty character classes are (and should be) an error both in wildcards and in regular expressions:

    PS[1] (354) > "abc]def" -like "abc[]*"
    The specified wildcard character pattern is not valid: abc[]*
    At line:1 char:1
    + "abc]def" -like "abc[]*"
    + ~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordExcepti
       on
        + FullyQualifiedErrorId : RuntimeException
    
    PS[1] (355) > "abc]def" -match "abc[]"
    parsing "abc[]" - Unterminated [] set.
    At line:1 char:1
    + "abc]def" -match "abc[]"
    + ~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException
    

    The patterns are well-formed but invalid.

    As with other malformed wildcard patterns

    What other malformed wildcard patterns? The only other wildcard metacharacters are * and ? which, tautologically, cannot be malformed.

  2. added
    Issue-Question
    Resolution-Answered
    on Apr 26, 2018
  3. mklement0 commented on Apr 26, 2018

    @mklement0
    ContributorAuthor

    a single wildcard library that is used by everything handling wildcards so the behaviour will be the same everywhere.

    That's good to know.
    (A notable exception is the FileSystem provider's -Filter parameter.)

    What other malformed wildcard patterns?

    I meant any use of wildcard metacharacters that do not participate in a valid expression.

    As you point out, * and ?, due to their being single-character constructs, are by definition valid, and ] without a preceding [ is treated as a literal.

    That leaves us with [ variations: any [-initiated construct that is either immediately followed by ] or has no closing ] is invalid; to give a few examples, all of which fail with The specified wildcard character pattern is not valid:

    'a' -like 'a[]' 
    'a' -like 'a[' 
    'a' -like 'a[whatever' 

    Note, however, that while these patterns cause errors with -like, they do not do so (unconditionally) with Get-Item -Path / Get-ChildItem -Path.

    For instance, Get-Item a[.txt and Get-Item a[].txt do not generally fail, despite the patterns being invalid - and, as it turns out, that's the true problem here.

    (Contrast this with my original example, Get-Item a].txt, in which, based on your explanation, a].txt is rightfully considered a literal name and matched accordingly).

  4. mklement0 commented on Apr 26, 2018

    @mklement0
    ContributorAuthor

    Bruce Payette (@BrucePay):

    On a tangential note:

    The patterns [ 'abc[]*' and 'abc[]' ] are well-formed but invalid.

    I'm curious what you mean by well-formed in this context, given that these patterns result in a syntax error.

    At least in the context of XML, well-formed refers to syntactically valid input (whereas valid refers to well-formed input that additionally conforms to a prescribed structure), which is not the case here.

  5. BrucePay commented on Apr 26, 2018

    @BrucePay
    Collaborator

    I'm curious what you mean by well-formed in this context, given that these patterns result in a syntax error.

    No - they result in a WildcardPatternException which is documented as "Thrown when a wildcard pattern is invalid."

  6. mklement0 commented on Apr 26, 2018

    @mklement0
    ContributorAuthor

    Thanks; I spoke loosely: I meant a wildcard syntax error, not a PowerShell syntax error, or more informally, "something that isn't accepted as a wildcard pattern for formal reasons".

    So, to return to the question, what does well-formed mean in this context?

    And please don't forget to re-label this issue a bug (to recap: in the absence of tmp-prefixed filenames, Get-Item tmp[].txt neglects to complain about the invalid wildcard pattern).

  7. changed the title [-]Wildcard expression with [] sometimes causes error during globbing with Get-Item / Get-ChildItem[/-] [+]Get-Item / Get-ChildItem situationally neglect to report an invalid wildcard expression as such[/+] on Apr 26, 2018
  8. BrucePay commented on Apr 26, 2018

    @BrucePay
    Collaborator

    For instance, Get-Item a[.txt and Get-Item a[].txt do not generally fail, despite the patterns being invalid - and, as it turns out, that's the true problem here.

    For the core cmdlets, wildcard processing is handled by the underlying providers. Providers are funny beasts because they try to use the much faster native globbing capabilities of the underlying store (e.g. on Windows, where the files system API directly understands '*'). However these native capabilities don't match our wildcards so there are inconsistencies. Also quoting in the file system provider is fiercely broken - so much so that we added -Literalpath in V2 as a workaround for these issues after giving up on trying to fix the providers (ok - and probably concerns about breaking changes.) So in summary, it's a problem with the filesystem provider, not the cmdlets. Perhaps the bug should be re-titled "The Filesystem provider does not reliably report invalid wildcard patterns." or some such. And while actually fixing the existing provider's general behaviour seems unlikely but it should be possible to make it error out consistently (though that is a breaking change.) Out of curiosity - have you seen similar inconsistencies in the other providers e.g. the registry ?

    Anyway, if you'll update the title (and description?) , I'll mark it appropriately. Thanks.

    So, to return to the question, what does well-formed mean in this context?

    Syntactically valid but semantically useless.

  9. added
    Issue-Bug
    Breaking-Change
    WG-Engine-Providers
    and removed
    Issue-Question
    Resolution-Answered
    on Apr 26, 2018
  10. changed the title [-]Get-Item / Get-ChildItem situationally neglect to report an invalid wildcard expression as such[/-] [+]The FileSystem provider does not reliably report invalid wildcard patterns[/+] on Apr 26, 2018
  11. mklement0 commented on Apr 26, 2018

    @mklement0
    ContributorAuthor

    Thanks, Bruce Payette (@BrucePay), I've updated the title as you've suggested.

    Out of curiosity - have you seen similar inconsistencies in the other providers e.g. the registry ?

    A quick test suggests that the other providers are not affected:

    Get-PsDrive | ? { $_.Provider.Name -ne 'FileSystem' } | Group-Object Provider | 
      ForEach-Object { $oldpwd = $PWD } { 
        $drive = ($_.Group[0].Name + ':'); 
        Write-Verbose -Verbose $drive; Set-location $drive; 
        try { Get-Item 'zzzz[]' } catch { $_ } 
      } { Set-Location $oldPwd }

    The above yields:

    VERBOSE: Alias:
    Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: zzzz[]
    At C:\Users\jdoe\Desktop\pg\pg.ps1:5 char:11
    +     try { Get-Item 'zzzz[]' } catch { $_ }
    +           ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
        + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand
    
    VERBOSE: Cert:
    Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: zzzz[]
    At C:\Users\jdoe\Desktop\pg\pg.ps1:5 char:11
    +     try { Get-Item 'zzzz[]' } catch { $_ }
    +           ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
        + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand
    
    VERBOSE: Env:
    Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: zzzz[]
    At C:\Users\jdoe\Desktop\pg\pg.ps1:5 char:11
    +     try { Get-Item 'zzzz[]' } catch { $_ }
    +           ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
        + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand
    
    VERBOSE: Function:
    Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: zzzz[]
    At C:\Users\jdoe\Desktop\pg\pg.ps1:5 char:11
    +     try { Get-Item 'zzzz[]' } catch { $_ }
    +           ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
        + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand
    
    VERBOSE: HKCU:
    Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: zzzz[]
    At C:\Users\jdoe\Desktop\pg\pg.ps1:5 char:11
    +     try { Get-Item 'zzzz[]' } catch { $_ }
    +           ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
        + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand
    
    VERBOSE: Variable:
    Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: zzzz[]
    At C:\Users\jdoe\Desktop\pg\pg.ps1:5 char:11
    +     try { Get-Item 'zzzz[]' } catch { $_ }
    +           ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
        + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand
    
    VERBOSE: WSMan:
    Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: zzzz[]
    At C:\Users\jdoe\Desktop\pg\pg.ps1:5 char:11
    +     try { Get-Item 'zzzz[]' } catch { $_ }
    +           ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
        + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand
    
  12. mklement0 commented on Apr 26, 2018

    @mklement0
    ContributorAuthor

    As for the change being breaking:

    I don't think that's too much of a concern, because something like Get-Item tmp[].txt never did anything useful: When it doesn't complain about the invalid pattern, it just fails quietly - it doesn't even match a file literally named tmp[].txt.

  13. added a commit that references this issue on Nov 9, 2019
  14. SteveL-MSFT commented on Mar 25, 2020

    @SteveL-MSFT
    Member

    The topic of a square bracket in filenames and paths keeps coming up. I'm wondering if the following proposal makes sense:

    1. if the path exists, then use it before analyzing for any wildcard characters
    2. if the path contains invalid wildcard characters, treat as literal rather than returning an error about invalid wildcards
    3. otherwise, current behavior
  15. vexx32 commented on Mar 26, 2020

    @vexx32
    Collaborator

    Yeah, I think that makes the most sense, Steve Lee (@SteveL-MSFT).

  16. mklement0 commented on Mar 26, 2020

    @mklement0
    ContributorAuthor

    Please see the discussion in #12168 (comment)

  17. added
    Area-FileSystem-Provider
    on Nov 30, 2021
  18. 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.

  19. 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.

  20. added
    Resolution-No Activity
    on Nov 16, 2023
  21. 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.

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

    Area-FileSystem-ProviderBreaking-ChangeIssue-BugResolution-No ActivityWG-Engine-Providers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions