8000 console: Ansi stripped on stdout when stderr is redirected · Issue #38981 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
cxj opened this issue Nov 4, 2020 · 3 comments
Closed

console: Ansi stripped on stdout when stderr is redirected #38981

cxj opened this issue Nov 4, 2020 · 3 comments

Comments

@cxj
Copy link
cxj commented Nov 4, 2020

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 or bash, 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.

@wouterj
Copy link
Member
wouterj commented Nov 4, 2020

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

@cxj
Copy link
Author
cxj commented Nov 5, 2020

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 --ansi is provided. Example:

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

@wouterj
Copy link
Member
wouterj commented Nov 5, 2020

I've tested both directions with the patch provided in #38991. It seems like this patch fixes it.

chalasr added a commit that referenced this issue Nov 5, 2020
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
@chalasr chalasr closed this as completed Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
0