Repository navigation
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[].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
Activity
BrucePay commentedon Apr 26, 2018
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
-likeas 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
Similarly, empty character classes are (and should be) an error both in wildcards and in regular expressions:
The patterns are well-formed but invalid.
What other malformed wildcard patterns? The only other wildcard metacharacters are * and ? which, tautologically, cannot be malformed.
mklement0 commentedon Apr 26, 2018
That's good to know.
(A notable exception is the
FileSystemprovider's-Filterparameter.)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 withThe specified wildcard character pattern is not valid:Note, however, that while these patterns cause errors with
-like, they do not do so (unconditionally) withGet-Item -Path/Get-ChildItem -Path.For instance,
Get-Item a[.txtandGet-Item a[].txtdo 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].txtis rightfully considered a literal name and matched accordingly).mklement0 commentedon Apr 26, 2018
Bruce Payette (@BrucePay):
On a tangential note:
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.
BrucePay commentedon Apr 26, 2018
No - they result in a
WildcardPatternExceptionwhich is documented as "Thrown when a wildcard pattern is invalid."mklement0 commentedon Apr 26, 2018
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[].txtneglects to complain about the invalid wildcard pattern).[-]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[/+]BrucePay commentedon Apr 26, 2018
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
-Literalpathin 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.
Syntactically valid but semantically useless.
[-]Get-Item / Get-ChildItem situationally neglect to report an invalid wildcard expression as such[/-][+]The FileSystem provider does not reliably report invalid wildcard patterns[/+]mklement0 commentedon Apr 26, 2018
Thanks, Bruce Payette (@BrucePay), I've updated the title as you've suggested.
A quick test suggests that the other providers are not affected:
The above yields:
mklement0 commentedon Apr 26, 2018
As for the change being breaking:
I don't think that's too much of a concern, because something like
Get-Item tmp[].txtnever did anything useful: When it doesn't complain about the invalid pattern, it just fails quietly - it doesn't even match a file literally namedtmp[].txt.Update BackupScript.ps1
SteveL-MSFT commentedon Mar 25, 2020
The topic of a square bracket in filenames and paths keeps coming up. I'm wondering if the following proposal makes sense:
vexx32 commentedon Mar 26, 2020
Yeah, I think that makes the most sense, Steve Lee (@SteveL-MSFT).
mklement0 commentedon Mar 26, 2020
Please see the discussion in #12168 (comment)
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.