-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Write-Error should use positional binding to -ErrorRecord and -Exception #10739
Copy link
Copy link
Closed
Labels
HacktoberfestPotential candidate to participate in HacktoberfestPotential candidate to participate in HacktoberfestIssue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.The issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility modulecmdlets in the Microsoft.PowerShell.Utility module
Metadata
Metadata
Assignees
Labels
HacktoberfestPotential candidate to participate in HacktoberfestPotential candidate to participate in HacktoberfestIssue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.The issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility modulecmdlets in the Microsoft.PowerShell.Utility module
When using the parameter sets selected by
-ErrorRecord/-ExceptionofWrite-Error, it makes sense to bind (the first) positional argument of types (derived from)System.Management.Automation.ErrorRecord/System.Exceptionto those parameters.Currently, that doesn't happen, because these parameters lack the
Position=0attribute field in their `Parameter attributes.The result is that using something like
Write-Error $_in an apparent effort to pass theSystem.Management.Automation.ErrorRecordinstance in$_through, you end up with the equivalent of:rather than the more sensible - and probably expected:
While the two resulting error records ultimately contain the same message (description), the specifics of the input error record are lost.
The same applies analogously to
-Exception.The fix is trivial: Add
Position = 0to the following two locations:PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Write.cs
Lines 235 to 236 in d2c04f3
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Write.cs
Lines 217 to 218 in d2c04f3
Steps to reproduce
Run the following tests:
Expected behavior
All tests should pass.
Actual behavior
The first two tests fail, because the positional arguments bind to
-Message, resulting in a genericMicrosoft.PowerShell.Commands.WriteErrorExceptionexception wrapper.Environment data