give docker a tty output when expecting color#3122
Conversation
|
I haven't looked at the tests (quite obviously!) — I'm waiting on a opinion on this change before taking care of that :-) |
|
this won't work because one can enable color without a tty and docker will crash |
|
never heard of docker crashing because of that I ran a few tests to be sure: ~/s/pre-commit (docker-tty|✔) $ docker run --tty --rm debian grep --color Debian /etc/issue 2>&1
Debian GNU/Linux 12 \n \l
~/s/pre-commit (docker-tty|✔) $ docker run --tty --rm debian grep --color Debian /etc/issue 2>&1 | cat
Debian GNU/Linux 12 \n \l
~/s/pre-commit (docker-tty|✔) $ docker run --rm debian grep --color Debian /etc/issue 2>&1 | cat
Debian GNU/Linux 12 \n \lno color in that case and just to make sure that the ~/s/pre-commit (docker-tty|✔) $ python -c "import sys; print(sys.stdout.isatty())" 2>&1 | cat
False
~/s/pre-commit (docker-tty|✔) $ python -c "import sys; print(sys.stdout.isatty())" 2>&1
True
~/s/pre-commit (docker-tty|✔) $ python -c "import sys; print(sys.stderr.isatty())" 2>&1 | cat
False
~/s/pre-commit (docker-tty|✔) $ python -c "import sys; print(sys.stderr.isatty())" 2>&1
TrueI may be missing something though. Could you be more explicit on how docker will be crashing? |
|
it drops the tty ability on stdout but not on stdin |
~/s/pre-commit (docker-tty|✔) $ echo | docker run --tty --rm debian grep --color Debian /etc/issue 2>&1 | cat
Debian GNU/Linux 12 \n \lstill no crash (but some color) ~/s/pre-commit (docker-tty|✔) $ echo | python -c "import sys; print(sys.stdin.isatty()); print(sys.stdout.isatty()); print(sys.stderr.isatty()); " 2>&1 | cat
False
False
Falseand no tty anywhere docker makes the program running in the container that there is a tty though which is good to have some color. It may cause some problems with the programs that expect some interactivity, but I think |
|
hmm perhaps we can try this then. there's definitely easily reproducible cases where |
|
I've been using it for two weeks and haven't seen any problem. Does it look good enough for you to merge it? |
asottile
left a comment
There was a problem hiding this comment.
needs a test demonstrating your change
pre_commit/languages/docker.py
Outdated
| '-v', f'{_get_docker_path(os.getcwd())}:/src:rw,Z', | ||
| '--workdir', '/src', | ||
| ) | ||
| ) + (('--tty',) if color else ()) |
There was a problem hiding this comment.
rather than use + it would probably be clearer if you used something like get_docker_user above -- maybe write a little function for that?
this makes the behavior more consistent with the system language and would help the executable run in a docker container to produce a colored output.
|
Thanks! |

this makes the behavior more consistent with the system language
and would help the executable run in a docker container to
produce a colored output.