-
Notifications
You must be signed in to change notification settings - Fork 8.2k
The FileSystem provider does not reliably report invalid wildcard patterns #6733
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).
(@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[].txtExpected 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