build icon indicating copy to clipboard operation
build copied to clipboard

`colorama` disables colors in GitHub Actions

Open abitrolly opened this issue 3 years ago • 4 comments

GitHub Actions provide no TTY, so colorama import wraps stdout and strips all colors https://github.com/tartley/colorama/issues/209#issuecomment-465119302 so even forcing colors doesn't work.

2022-06-30T17:00:45.4633499Z Successfully installed build-0.8.0 pep517-0.12.0 tomli-2.0.1
2022-06-30T17:00:46.1506025Z ##[group]Run python -m build
2022-06-30T17:00:46.1506287Z [36;1mpython -m build[0m
2022-06-30T17:00:46.1506499Z [36;1mgit status --ignored[0m
2022-06-30T17:00:46.1559508Z shell: /usr/bin/bash -e {0}
2022-06-30T17:00:46.1559700Z env:
2022-06-30T17:00:46.1559871Z   FORCE_COLOR: 1
2022-06-30T17:00:46.1560056Z ##[endgroup]
2022-06-30T17:00:46.6361795Z * Creating venv isolated environment...
2022-06-30T17:00:49.6232262Z * Installing packages in isolated environment... (setuptools >= 40.8.0, wheel)
2022-06-30T17:00:52.0028343Z * Getting dependencies for sdist...

Took me a while to debug this. Added some debug statements.

--- a/src/build/__main__.py
+++ b/src/build/__main__.py
@@ -49,6 +49,7 @@ def _init_colors() -> Dict[str, str]:
 _STYLES = _init_colors()
 print(_STYLES)
 
+print('{red}ERROR{reset} {}'.format("msg", **_STYLES))
 
 def _showwarning(
     message: Union[Warning, str],
@@ -65,6 +64,7 @@ def _showwarning(
 
 
 def _setup_cli() -> None:
+    print('{red}ERROR{reset} {}'.format("precolorama", **_STYLES))
     warnings.showwarning = _showwarning
 
     try:
@@ -73,6 +73,7 @@ def _setup_cli() -> None:
         pass
     else:
         colorama.init()  # fix colors on windows
+    print('{red}ERROR{reset} {}'.format("postcolorama", **_STYLES))
 
 
 def _error(msg: str, code: int = 1) -> NoReturn:  # pragma: no cover

And got this.

image

The solution is to enable colorama only on Windows.

abitrolly avatar Jun 30 '22 19:06 abitrolly

That seems pretty reasonable to do, but just curious, why is colorama even present? It should not be installed on linux/macOS generally.

henryiii avatar Jul 01 '22 03:07 henryiii

colorama is needed on Windows, because its terminal is unable to render ANSI sequences by itself. Why it was installed in Linux needs further investigation.

abitrolly avatar Jul 01 '22 05:07 abitrolly

colorama is present by default on ubuntu-latest GHA runners.

https://github.com/yakshaveinc/python/runs/7144602632?check_suite_focus=true

image

But I haven't found why it is here. Software list doesn't include it https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2204-Readme.md and I couldn't find build logs for runner images to trace down who installs it. pip also doesn't seem to include the feature to track who needed a dependency.

abitrolly avatar Jul 01 '22 06:07 abitrolly

Asked why the deps here https://github.com/actions/virtual-environments/issues/5851

abitrolly avatar Jul 01 '22 07:07 abitrolly