You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve the error message for when a property-name / hashtable-key token cannot be parsed as such #6959
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
A token to the right of . when accessing an object property or hashtable entry is parsed as an expression if it doesn't start with a letter.
If that token is ultimately not a valid expression, the error message erroneously suggests that no token was specified, which is confusing:
$obj= [pscustomobject] @{ '1a'='one A' }
PS>$obj.1a# !! Breaks; must use $obj.'1a', due to property name starting with a *digit*
At line:1 char:6+$obj.1a+ ~
Missing property name after reference operator.
Given that the fact that 1a is not treated as a string - and instead evaluated as an expression that breaks - is not obvious, providing specific guidance in the error message along the following lines would be helpful:
At line:1 char:6
+ $obj.1a
+ ~
Invalid property-name expression; use quotes for literal interpretation.
If we didn't have a backward-compatibility concern, my vote would definitely be to fix it.
The problem is that for backward compatibility we must retain the behavior of at least first attempting to interpret a property name / hashtable key that starts with a digit as an expression, for the sake of accessing hashtables with numeric keys.
A real-world example is $Matches.0 to access the hashtable entry with integer key 0.
We could fall back to string interpretation if the expression turns out not to be valid, however; e.g.:
# Examples of surprising behaviors we CANNOT change:$obj.1e2# 1e2 is interpreted as [double] 100, so as a *property* access this is the same as: $obj.'100'$obj.1d# -> $obj.'1'$obj.1l# -> $obj.'1'$obj.1kb# -> $obj.'1024'# This we could fix by falling back to string, because 1a is not valid as an expression.$obj.1a
Given that there are cases where unexpected numeric interpretation can kick in, which results in quiet failure (relative to the user's intent), the question is whether implementing fall-back-to-string behavior is ultimately more confusing than helpful:
In the absence of knowing the underlying logic, it would be hard to infer what's going on; e.g., with fall-back-to-string in place, the user may be puzzled why .1a works, but .1d doesn't (in the sense of .'1d').
Providing guidance via the error message - i.e., use quoting if you want to force interpretation as a string - ultimately promotes safer habits.
Reacted by Ilya
microsoft-github-policy-service commented on 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 commented on 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 commented on 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.
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
iSazonov commentedon May 31, 2018
Is it necessary to use quotes in the scenario? Maybe can fix this?
mklement0 commentedon May 31, 2018
Ilya (@iSazonov):
The linked issue - #6946 - discusses a fix.
In short:
If we didn't have a backward-compatibility concern, my vote would definitely be to fix it.
The problem is that for backward compatibility we must retain the behavior of at least first attempting to interpret a property name / hashtable key that starts with a digit as an expression, for the sake of accessing hashtables with numeric keys.
A real-world example is
$Matches.0to access the hashtable entry with integer key0.We could fall back to string interpretation if the expression turns out not to be valid, however; e.g.:
Given that there are cases where unexpected numeric interpretation can kick in, which results in quiet failure (relative to the user's intent), the question is whether implementing fall-back-to-string behavior is ultimately more confusing than helpful:
In the absence of knowing the underlying logic, it would be hard to infer what's going on; e.g., with fall-back-to-string in place, the user may be puzzled why
.1aworks, but.1ddoesn't (in the sense of.'1d').Providing guidance via the error message - i.e., use quoting if you want to force interpretation as a string - ultimately promotes safer habits.
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 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.