-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Hi,
I'm trying to create a document in a here string (YAML frontmatter for Markdown, a list of items), but I noticed extra spaces being added in unexpected places. I've paired this down to a minimal comparison of the issue, which occurs both in Windows PowerShell and the latest 7.4.6 core version.
This is my initial investigation:
# List of Items
$tags = @("Item1", "Item2", "Item3")
# List Straight - Displays as expected in subexpression
$($tags | ForEach-Object { "- $_" })
#($tags | ForEach-Object { "- $_" }) # same here as straight grouping expression
Write-Output "---"
# Want to embed that output in our Here String (to format with other info/variables); it's missing newlines now, but all items have a space between them...
Write-Output @"
$($tags | ForEach-Object { "- $_" })
"@
Write-Output "---"
# Try again with explicit newline... same issue, each line beyond the first has an extra space now due to some 'implicit' formatting...
Write-Output @"
$($tags | ForEach-Object { "- $_`n" })
"@
Outputs:
- Item1
- Item2
- Item3
---
- Item1 - Item2 - Item3
---
- Item1
- Item2
- Item3
Notice the extra spaces being added between items (or when we add the newline at the start of the line) compared to when we just run the variable itself straight in the console. It makes it impossible in the here string to format it as desired without the extra spaces, at least using intuitive practices.
If I do this trickery instead, I can get the output I want:
Write-Output @"
- $($tags -join "`n- ")
"@
But this wouldn't have been immediately obvious compared to the regular use of ForEach-Object and $_ to iterate over collections that's prevalent with PowerShell. This string/join exploitation isn't a friendly deconstruction for this scenario that would be immediately apparent to everyone.
To simplify, we can remove the ForEach-Object from the repro:
# List of Items
$tags = @("Item1", "Item2", "Item3")
# If we output tags, we nicely get one per line
Write-Output $tags
Write-Output "Actual:"
# If we then do that here, we get them only separated by space instead of at minimum clumped together
Write-Output @"
$tags
"@
Write-Output "Expected:"
# i.e. if we're not going to get a newline, fine that's probably preferred, but I would then expect no extra formatting, so it should be the same as:
Write-Output ($tags -join "")
Outputs:
Item1
Item2
Item3
Actual:
Item1 Item2 Item3
Expected:
Item1Item2Item3
Expected behavior
- Item1
- Item2
- Item3
Actual behavior
- Item1
- Item2
- Item3
Error details
No response
Environment data
Name Value
---- -----
PSVersion 7.4.6
PSEdition Core
GitCommitId 7.4.6
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
See same behavior in Windows PowerShell too:
Name Value
---- -----
PSVersion 5.1.19041.5072
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.5072
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Visuals
No response