Skip to content

give docker a tty output when expecting color#3122

Merged
asottile merged 1 commit into
pre-commit:mainfrom
glehmann:docker-tty
Mar 2, 2024
Merged

give docker a tty output when expecting color#3122
asottile merged 1 commit into
pre-commit:mainfrom
glehmann:docker-tty

Conversation

@glehmann

@glehmann glehmann commented Feb 7, 2024

Copy link
Copy Markdown
Contributor

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

glehmann commented Feb 7, 2024

Copy link
Copy Markdown
Contributor Author

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

@asottile

asottile commented Feb 7, 2024

Copy link
Copy Markdown
Member

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

@asottile asottile closed this Feb 7, 2024
@glehmann

glehmann commented Feb 7, 2024

Copy link
Copy Markdown
Contributor Author

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

asottile commented Feb 7, 2024

Copy link
Copy Markdown
Member

it drops the tty ability on stdout but not on stdin

@glehmann

glehmann commented Feb 7, 2024

Copy link
Copy Markdown
Contributor Author
~/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

asottile commented Feb 7, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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?

@asottile asottile left a comment

Copy link
Copy Markdown
Member

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

Comment thread pre_commit/languages/docker.py Outdated
'-v', f'{_get_docker_path(os.getcwd())}:/src:rw,Z',
'--workdir', '/src',
)
) + (('--tty',) if color else ())

Copy link
Copy Markdown
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?

Comment thread pre_commit/languages/docker.py Outdated
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.

@asottile asottile left a comment

Copy link
Copy Markdown
Member

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
@glehmann

glehmann commented Mar 2, 2024

Copy link
Copy Markdown
Contributor Author

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.

2 participants