Skip to content
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

give docker a tty output when expecting color #3122

Merged
merged 1 commit into from Mar 2, 2024

Conversation

glehmann
Copy link
Contributor

@glehmann glehmann commented Feb 7, 2024

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.

@glehmann
Copy link
Contributor Author

glehmann commented Feb 7, 2024

I haven't looked at the tests (quite obviously!) — I'm waiting on a opinion on this change before taking care of that :-)

@asottile
Copy link
Member

asottile commented Feb 7, 2024

this won't work because one can enable color without a tty and docker will crash

@asottile asottile closed this Feb 7, 2024
@glehmann
Copy link
Contributor Author

glehmann commented Feb 7, 2024

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

Debian is displayed in red in both cases. No crash.

~/s/pre-commit (docker-tty|✔) $ docker run --rm debian grep --color Debian /etc/issue 2>&1 | cat
Debian GNU/Linux 12 \n \l

no color in that case

and just to make sure that the | cat drops the tty ability:

~/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
True

I may be missing something though. Could you be more explicit on how docker will be crashing?

@asottile
Copy link
Member

asottile commented Feb 7, 2024

it drops the tty ability on stdout but not on stdin

@glehmann
Copy link
Contributor Author

glehmann commented Feb 7, 2024

~/s/pre-commit (docker-tty|✔) $ echo | docker run --tty --rm debian grep --color Debian /etc/issue 2>&1 | cat
Debian GNU/Linux 12 \n \l

still 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
False

and no tty anywhere

docker makes the program running in the container that there is a tty though

~/s/pre-commit (docker-tty|✔) $ echo | docker run --rm python python3 -c "import sys; print(sys.stdin.isatty()); print(sys.stdout.isatty()); print(sys.stderr.isatty()); " 2>&1 | cat
False
False
False
~/s/pre-commit (docker-tty|✔) $ docker run --rm python python3 -c "import sys; print(sys.stdin.isatty()); print(sys.stdout.isatty()); print(sys.stderr.isatty()); " 2>&1
False
False
False
~/s/pre-commit (docker-tty|✔) $ docker run --tty --rm python python3 -c "import sys; print(sys.stdin.isatty()); print(sys.stdout.isatty()); print(sys.stderr.isatty()); " 2>&1
True
True
True
~/s/pre-commit (docker-tty|✔) $ echo | docker run --rm python python3 -c "import sys; print(sys.stdin.isatty()); print(sys.stdout.isatty()); print(sys.stderr.isatty()); " 2>&1 | cat
False
False
False
~/s/pre-commit (docker-tty|✔) $ echo | docker run --tty --rm python python3 -c "import sys; print(sys.stdin.isatty()); print(sys.stdout.isatty()); print(sys.stderr.isatty()); " 2>&1 | cat
True
True
True

which is good to have some color. It may cause some problems with the programs that expect some interactivity, but I think pre-commit forbid that in the hooks, so it shouldn't matter much

@asottile
Copy link
Member

asottile commented Feb 7, 2024

hmm perhaps we can try this then. there's definitely easily reproducible cases where -i without a tty causes docker to halt, I recall some with --tty as well but I'll just revert this if there's a problem found later on. I don't personally use language: docker so if it breaks shrugs.

@glehmann
Copy link
Contributor Author

I've been using it for two weeks and haven't seen any problem. Does it look good enough for you to merge it?

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a test demonstrating your change

@@ -118,7 +118,7 @@ def docker_cmd() -> tuple[str, ...]: # pragma: win32 no cover
# unshared label. Only the current container can use a private volume.
'-v', f'{_get_docker_path(os.getcwd())}:/src:rw,Z',
'--workdir', '/src',
)
) + (('--tty',) if color else ())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than use + it would probably be clearer if you used something like get_docker_user above -- maybe write a little function for that?

pre_commit/languages/docker.py Outdated Show resolved Hide resolved
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.
Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asottile asottile enabled auto-merge March 2, 2024 16:54
@asottile asottile merged commit 5e11c26 into pre-commit:main Mar 2, 2024
12 checks passed
@glehmann
Copy link
Contributor Author

glehmann commented Mar 2, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants