question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Out-String -Stream` unexpectedly does not split multi-line input strings into individual lines too

See original GitHub issue

Note: Originally submitted as a suggestion, now reclassified as as bug and rewritten accordingly.

Due to a presumed oversight, input objects to Out-String -Stream that happen to be multi-line strings are not split into individual lines, even though doing so is the purpose of the -Stream switch - see this Stack Overflow answer for background information.

Steps to reproduce

# Pass a single 3-line string, whose lines should be emitted individually.
"a`nb`nc" | Out-String -Stream | Should -Be a, b, c

Note: As with Get-Content, CR, CRLF, and LF newlines should be recognized interchangeably.

Expected behavior

The test should succeed.

Actual behavior

The test fails:

Expected @('a', 'b', 'c'), but got 'a b c'

That is, the multi-line input string is passed through as-is (Pester converts it to a single-line representation for the sake of a concise error message).

Environment data

PowerShell Core 7.2.0-preview.2

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mklement0commented, Jan 21, 2021

Thanks, @iSazonov.

Yes, the behavior is consistent with Windows PowerShell, but all that means is that the bug is present there too. While there is buggy / questionable legacy behavior that users indeed either (a) have taken advantage of or (b) have employed workarounds for that a fix would break, neither applies in this case:

  • The bug results in unequivocally unhelpful behavior.
  • Existing workarounds would not break if we fix this (the workaround is to apply line-splitting manually, which would simple be a (then-unneeded) no-op).

Therefore, I think a fix for 7.2 is perfectly appropriate.

To save others having to follow the link in my previous comment about the intent behind and current behavior of -Stream:

  • Out-String by default emits a single multi-line string that is the formatted string representation of all of its input, as you would see it in the console, including a trailing newline (which arguably shouldn’t be added, but that’s a separate discussion - see https://github.com/PowerShell/PowerShell/issues/14444).

  • If you add -Stream, the lines of this string are output one by one - except if multi-line strings happen to be among the input objects, which are not split into their lines (they are output as is); this is the bug at hand.

That is, even the lines that make up the formatting-system’s string representation of a single object are output individually; e.g.:

# Use Out-String -Stream and enclose all resulting lines in [...]
PS> Get-Item / | Out-String -Stream | ForEach-Object { "[$($_.TrimEnd())]" }
[]
[]
[    Directory:]
[]
[]
[Mode                 LastWriteTime         Length Name]
[----                 -------------         ------ ----]
[d--hs-         1/21/2021   9:40 AM                C:\]
[]
[]
2reactions
mklement0commented, Jan 20, 2021

Good point, @iSazonov - can you please relabel the issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Read and split multiple lines into individual strings
Read each line and put into a List<String> and as soon as a - occurs, copy it to the instance of List<List<String>> and...
Read more >
Sliting multiple lines - Power Platform Community - Microsoft
Solved: Intialize a variable named "A" of type string and enter a couple of lines of text. Then use Compose to split it....
Read more >
JavaScript Multiline String – How to Create Multi Line ...
In this article, you will learn three different ways to create multiline strings in JavaScript. I will first explain the basics of strings...
Read more >
Manage multiline messages | Filebeat Reference [8.9]
After the specified timeout, Filebeat sends the multiline event even if no new pattern is found to start a new event. The default...
Read more >
Batch files: Build a string across multiple lines WITHOUT ...
The answer is explaining how to generate an actual multi-line string from a single-line parameter. I am wanting to do sort of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found