-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
console: Ansi stripped on stdout when stderr is redirected #38981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you @cxj for opening an issue after your tweet! I can reproduce this on Symfony 4.4 as well. Seems like at first Symfony used to only check stdout (ref #10592), as the output formatter is shared between stdout and stdin. 2 PRs were submitted to fix this behavior:
I think we should reconsider #13661. It's very unlikely AFAICS that someone overrides the output formatter with something that cannot be closed. Seems like the PR simply stalled and Composer didn't have the time to wait for the perfect solution. Status: Reviewed |
Note this also fails in the opposite way. If one redirects standard output, then the TTY receiving the standard error data also loses the ANSI color, unless an explicit bin/console somecommand > output/file # fails, stderr on tty screen has no color bin/console --ansi somecommand > output/file # works, stderr on sceen has color Thanks for looking into this. Because Unix/POSIX has a very strong model of using pipe-lined commands at the CLI, it's important to support this model of usage. For example, user sees errors, warnings, metadata and progress, while final data is in the output file: % bin/console get:data | fgrep 'target-string' | sort | uniq -c | sort -n > frequencies.dat
connected to mysql @ 127.0.0.1 via TCP/IP
123456 rows selected
123400 rows extracted
56 duplicate data rows |
I've tested both directions with the patch provided in #38991. It seems like this patch fixes it. |
This PR was merged into the 4.4 branch. Discussion ---------- [Console] Fix ANSI when stdErr is not a tty | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38981 | License | MIT | Doc PR | - Taking the @wouterj 's comment into account (#38981 (comment)) This PR prevents using the same Formatter for stdOut and stdErr when possible. When user send a custom formatter (or call `setFormatter`) the previous logic is kept. Otherwise, symfony is asked to create the Formatter, and thus is able to clone the formatter. In a future PR targeting 5.3, we could improve the constructor to let people inject 2 distinguished formatters Commits ------- f3a398b Fix ANSI when stdErr is not a tty
Symfony version(s) affected: 5.1.8
Description
ANSI color is stripped (command behaves if run with
--no-ansi
flag) from standard output when standard error is redirected.How to reproduce
Pick any console app that issues output with ANSI color, and in a POSIX shell such as
sh
orbash
, redirect standard error, e.g.:bin/console some:command 2> /dev/null
Possible Solution
I would guess the
*_isatty()
check is being done on both stdout and stderr file descriptors and if either is not a TTY, the ANSI flag is wrongly cleared for both. Solution would be to check and manage the 2 file streams or descriptors separately.Additional context
Explicitly adding the
--ansi
flag makes it work, e.g.:bin/console --ansi some:command 2> /dev/null
correctly outputs the stdout text, such as
[OK] message
, in ANSI color.The text was updated successfully, but these errors were encountered: