Fix Write-Host for IDictionary objects#24676
Fix Write-Host for IDictionary objects#24676brendandburns wants to merge 2 commits intoPowerShell:masterfrom
Conversation
|
This PR will change the current way of rendering for |
| @{ Name = '-NoNewline:$true and colors'; Command = "Write-Host a,b -NoNewline:`$true -ForegroundColor Red -BackgroundColor Green;Write-Host a,b"; returnCount = 2; returnValue = @("Red:Green:a b:NoNewLine", "White:Black:a b:NewLine"); returnInfo = @("a b", "a b") } | ||
| @{ Name = '-NoNewline:$false and colors'; Command = "Write-Host a,b -NoNewline:`$false -ForegroundColor Red -BackgroundColor Green;Write-Host a,b"; returnCount = 2; returnValue = @("Red:Green:a b:NewLine","White:Black:a b:NewLine"); returnInfo = @("a b", "a b") } | ||
| @{ Name = 'XMLElement'; Command = "Write-Host ([xml] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") } | ||
| @{ Name = 'XMLDocument'; Command = "Write-Host ([system.xml.xmldocument] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") } |
|
The @PowerShell/wg-powershell-cmdlets reviewed this and we agree that this PR addresses an inconsistent behavior with |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
There was a problem hiding this comment.
Pull request overview
This PR updates Write-Host’s stringification logic so IDictionary inputs are rendered consistently (using ToString()), addressing the inconsistency reported in #24671 and refactoring the object-type handling to a switch pattern match.
Changes:
- Refactor
WriteHostCommand.ProcessObjectfromif/elsetoswitchpattern matching. - Special-case
IDictionaryso it’s not treated as anIEnumerablefor unrolling, and is instead rendered viaToString(). - Add a CI test case asserting the new
IDictionaryrendering behavior forWrite-Host @{...}.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WriteConsoleCmdlet.cs |
Adjusts Write-Host object rendering to treat IDictionary as a scalar (use ToString()), and refactors to a switch. |
test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 |
Adds a test case for IDictionary output formatting (type name via ToString()). |
| @{ Name = 'XMLElement'; Command = "Write-Host ([xml] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") } | ||
| @{ Name = 'XMLDocument'; Command = "Write-Host ([system.xml.xmldocument] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") } | ||
| @{ Name = 'XMLDocument'; Command = "Write-Host ([system.xml.xmldocument] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") } | ||
| @{ Name = 'IDictionary'; Command = "Write-Host @{Key1='Value1';Key2='Value2'}"; returnCount = 1; returnValue = @("White:Black:System.Collections.Hashtable:NewLine"); returnInfo = @("System.Collections.Hashtable") } |
There was a problem hiding this comment.
The new 'IDictionary' test case only covers argument-binding (e.g. Write-Host @{...}), but the reported regression is specifically about pipeline input (@{...} | Write-Host / [Dictionary[...]] piped). Please add a test case that exercises the pipeline form and asserts it matches the non-pipeline output, ideally using a System.Collections.Generic.Dictionary[string,string] to mirror the issue repro.
PR Summary
Fix Write-Host to consistently serialize using
ToString, fixed #24671PR Context
In the fixing of a bug related to XMLNode, IDictionary was also called out as an example of inconsistency. Following discussion in #24669 this code was also refactored to use
switchinstead ofif .. elsePR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.- [ ] Issue filed:
(which runs in a different PS Host).