Skip to content

[Console] SymfonyStyle block output is broken on Windows #61524

@FeBe95

Description

@FeBe95

Symfony version(s) affected

7.3.2

Description

When using $symfonyStyle->block($message) or any other related method (e.g. ->error(), ->success(), etc.) the output breaks on Windows if any \r\n newline characters are present in $message.

How to reproduce

According to my research, this only occurs on Windows machines. I was able to reproduce the issue in both Windows PowerShell and Windows Command Prompt (CMD).

Reproducer Code

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;

$symfonyStyle = new SymfonyStyle(new ArrayInput([]), new ConsoleOutput());
$symfonyStyle->error("This is the first line.\r\nThis is the second line.");

Reproducer Output

Image

Possible Solution

This is likely caused by the regular expressions used in OutputWrapper::wrap().

I don't know exactly how the method works, but it appears to replace \n with PHP_EOL when called in SymfonyStyle::createBlock(). On Windows machines, PHP_EOL is \r\n. If the original message already contains \r\n, then the wrap() method replaces this with \r\r\n. Splitting it by PHP_EOL leaves a trailing \r character. This causes the output to be broken.

How can this be solved? I'm not sure. It's either that the wrapper special handling for PHP_EOL on Windows, or the block method needs to use \n instead of PHP_EOL.

I'd like to tag the original authors of these two files for their input, if that's okay: @fchris82, @kbond?

Additional Context

I first noticed this issue while using Rector PHP, so I created an issue in their repository. I resolved the issue by replacing all occurrences of \r\n with \n. However, I think this should be solved upstream.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions