Repository navigation
Closed
Labels
Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.WG-Enginecore PowerShell engine, interpreter, and runtime
Milestone
Description
Steps to reproduce
PS> Add-Type -TypeDefinition @'
using System;
using System.Management.Automation;
[Cmdlet("Test", "Compiled")]
public class TestCompiled : PSCmdlet {
[Parameter(Position = 0)]
public DateTime DT;
protected override void EndProcessing() {
WriteObject(DT);
}
}
'@ -PassThru | Select-Object -First 1 -ExpandProperty Assembly | Import-Module
PS> function Test-Script {
param(
[DateTime]$DT
)
$DT
}
PS> @(
[CultureInfo]::CurrentCulture = 'ru-RU'
Test-Compiled 1/11/1111
Test-Script 1/11/1111
[CultureInfo]::CurrentCulture = 'en-US'
)
Wednesday, November 1, 1111 12:00:00 AM
Wednesday, January 11, 1111 12:00:00 AMExpected behavior
Same dates in both cases.
Actual behavior
Strings get parsed with different culture in mind.
Environment data
> $PSVersionTable
Name Value
---- -----
GitCommitId v6.0.0-alpha.17-18-gedecf7e04d65f7183cd1d278daa5ebed2970813f
PSRemotingProtocolVersion 2.3
PSEdition Core
WSManStackVersion 3.0
CLRVersion
BuildVersion 3.0.0.0
PSVersion 6.0.0-alpha
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0}
SerializationVersion 1.1.0.1My thoughts about the problem
I think the problem happens due to PowerShell automatically adds ArgumentTypeConverterAttribute to typed parameters for script cmdlets:
PS> (Get-Command Test-Script).Parameters.DT.Attributes | % GetType | % FullName
System.Management.Automation.ParameterAttribute
System.Management.Automation.ArgumentTypeConverterAttributebut not doing the same for compiled cmdlets:
PS> (Get-Command Test-Compiled).Parameters.DT.Attributes | % GetType | % FullName
System.Management.Automation.ParameterAttributeThat attribute make conversion use InvariantCulture, while compiled cmdlets use CurrentCulture for conversion.
Reacted by Ilya, Laurent Dardenne, Michael Klement and Bart van den Donk
Metadata
Metadata
Assignees
Labels
Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.WG-Enginecore PowerShell engine, interpreter, and runtime
Activity
iSazonov commentedon Oct 8, 2017
Steve Lee (@SteveL-MSFT) I think we should fix this before RTM.
daxian-dbw commentedon Oct 13, 2017
PetSerAl Thanks for the analysis!
ArgumentTypeConverterAttributeusesInvariantCulturewhen callingLanguagePrimitives.ConvertTo, whileParameterBinderBase.CoerceTypeAsNeededusesCurrentCulturewhen callingLanguagePrimitives.ConvertTo.I will submit a PR to change
ArgumentTypeConverterAttributeto useCurrentCultureas well.PetSerAl commentedon Oct 13, 2017
Dongbo Wang (@daxian-dbw)
Should it be other way around? Since PowerShell explicit cast use
InvariantCulture:I would expect that implicit cast to parameter type should also use
InvariantCulture.daxian-dbw commentedon Oct 13, 2017
PetSerAl That's a good point. I will change
ParameterBinderBase.CoerceTypeAsNeededto useInvariantCulture.iSazonov commentedon Oct 13, 2017
It seems strange that PowerShell ignores Culture.
We need more discussion - it is breaking change.
daxian-dbw commentedon Oct 13, 2017
Ilya (@iSazonov) I guess it's for the scripts to be more portable. I added the breaking change label.
iSazonov commentedon Oct 13, 2017
I think it is not documented. We need good description for this "portability" with InvariantCulture in argument convertion and parameter binding. Otherwise we'll have a big misunderstanding and many negative feedbacks.
Also we need @powershell-committee approvement for a breaking change.
Both my comment is for your PR.
daxian-dbw commentedon Oct 19, 2017
@PowerShell/powershell-committee has rejected the PR #5106. Quote the decision here:
I will close this issue as 'Won't Fix'.
iSazonov commentedon Oct 19, 2017
Does anyone know how many (module) breaking changes we've already made? It seems so much that talking about these changes is no longer relevant.
Also if we already is InvariantCulture for arguments and casting in scripts where is entry points where customers can catch problems?
daxian-dbw commentedon Oct 20, 2017
Ilya (@iSazonov) One example I got from the committee was the C# modules running on, for example, Japanese machines. After this change, it may happen that some cmdlets are broken with the same input arguments because the conversion has changed.
But I do agree with you that this inconsistency is unpleasant 🤕
iSazonov commentedon Oct 20, 2017
I have another example - EMS (Microsoft Exchange server Management Shell).
If you is not on English culture session it's always a huge mystery for you to make a search request! The situation is even worse if you try to make such a request dynamically in a script. In essence, you cannot create a script that you can trust. 😞
I think we should consider this Issue more closely.
daxian-dbw commentedon Oct 20, 2017
Interesting, I presume the
EMSexample is where the currentCurrentCulture conversionbehavior causes a usability issue, right? So in this case, it's preferable to haveInvariantCulture conversionso that things can work on non-english culture environment?lzybkr commentedon Oct 20, 2017
Proxies certainly make this issue more complicated - something may work when calling the cmdlet, but a proxy is introduced (e.g. for RBAC) and now it doesn't.
jhoneill commentedon Jan 19, 2022
To unify some of the stuff which I raised in #16767.
With non-US, culture selected
get-date "9/1/2022"returns "09 January 2022 00:00:00" which is what most users would expect and therefore good. If the culture is US it returns "Thursday, September 1, 2022 12:00:00 AM", still what users would expect However usingGet-Datemakes a non-portable script because the output for a given input isn't consistent, and therefore bad.CASTING with
[datetime]$xor[string]$xor[double]$xalways uses invariant culture.If you are on EN-US
[string][datetime]::nowshows the time using 24 hour clock where the EN-US locale uses 12 hour with "AM/PM" If you are in France and use "," instead of "." for your decimal point [double]"123,45" might give an unexpected result because it uses invariant.[Convert],.Tostring()andParse()use local (or specified) culture. Using these in a script with "whatever the local culture happens to be" can also produce non portable scripts. If numbers will be in French format, it is better to state French culture explicitly than assume the script will only ever run with culture set to French.Staying with French we can see that the
-ASoperator sometimes supports local culture and sometimes invariant.Compiled cmdlet parameters seem to follow
-ASso with French selectedGet-Date "19 janvier 2022"understands that "janvier" is the first month of the year in this culture butSelect -first "1 000"fails because a space is not accepted as a separator.Select -first "1,000"works . As a side note,[int]::parse()won't process either formI'm not sure how
-ASlinks toCorceTypeAsNeededin what Dongbo Wang (@daxian-dbw) said above :When we write
param ( [datetime]$d, )it looks like a cast, although it is really adding an ArgumentTypeConverterAttribute that will do a cast. If would be odd if the result were not the same asIt could have been created to do the same as
but people wouldn't expect this version.
This is the solution for the proxy-function case, not to specify a type in the param block and to the use convert inside the function.
The inconsistency of -As and the different handling of date time parameters in different kinds of commands is annoying, but the problems that would result from making everything consistent are far greater. It is worth ensuring that help for datetime parameters says whether they accept local format or require invariant.
iSazonov commentedon Jan 19, 2022
I think the operator uses the same code path in both cases. So I guess it could be PowerShell parser issue. /cc Rain Sallow (/u/ta11ow) (@vexx32)
vexx32 commentedon Jan 19, 2022
Don't think it's necessarily a parsing issue, but it could be a compiler issue. Patrick Meinecke (@SeeminglyScience) might have more insight here.
SeeminglyScience commentedon Jan 19, 2022
Nah it's a compiler issue. Also I think James O'Neill (@jhoneill) meant to link #16593 which goes into it in more detail.
jhoneill commentedon Jan 19, 2022
-AS [datetime]uses local culture-As [double]-As [int32]use invariantjhoneill commentedon Jan 19, 2022
I set the culture to French. You can see that a double displays with "," as a decimal separator, and the date uses French names.
.ToString()always uses the local culture[string]always uses invariant-As [string]converts numbers with invariant, and dates with local.