summaryrefslogtreecommitdiff
path: root/mesonbuild/mlog.py
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz93@gmail.com>2024-01-24 22:16:49 -0500
committerEli Schwartz <eschwartz93@gmail.com>2024-09-15 19:25:10 -0400
commit2b80d4cca174916a4b6d0c47c54ad56795741bb6 (patch)
tree7324c1db490c54bd78de354cb00c413c3c6810a7 /mesonbuild/mlog.py
parent74dd77ed81c90e3655b3dff5bfe98410a85dd4f0 (diff)
downloadmeson-2b80d4cca174916a4b6d0c47c54ad56795741bb6.tar.gz
When configuring fails in Github Actions, print folded logs
A common, and challenging, issue in CI runners is debugging issues when you know the information you want to check, but it's in the log file which you don't have because remote CI machines. There are various edge cases where this is especially hard to solve, such as inside of `pip install` where the build directory with the log file is automatically cleaned up. But it's never really *easy* when you don't expect it, and the best case scenario is your iteration time gets cut in half as you hurriedly go add some `cat`s to your CI scripts. Meson can, at least sometimes, detect platforms where text can be emitted inside of "folds", which are auto-collapsed and don't obscure the general output, but when clicked will expand the logfile contents. Hook this up. We start off with a Github Actions implementation. We had some internal code used by our own project tests runner, which can be utilized. Also permit forcing it via an environment variable, in case autodetection fails and you just want to force *something*, especially when meson is called a couple layers deep inside some other tool.
Diffstat (limited to 'mesonbuild/mlog.py')
-rw-r--r--mesonbuild/mlog.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py
index a8b018537..bc8faeba7 100644
--- a/mesonbuild/mlog.py
+++ b/mesonbuild/mlog.py
@@ -22,8 +22,9 @@ from dataclasses import dataclass, field
from pathlib import Path
if T.TYPE_CHECKING:
- from ._typing import StringProtocol, SizedStringProtocol
+ from typing_extensions import Literal
+ from ._typing import StringProtocol, SizedStringProtocol
from .mparser import BaseNode
TV_Loggable = T.Union[str, 'AnsiDecorator', StringProtocol]
@@ -75,6 +76,7 @@ def setup_console() -> None:
pass
_in_ci = 'CI' in os.environ
+_ci_is_github = 'GITHUB_ACTIONS' in os.environ
class _Severity(enum.Enum):
@@ -540,3 +542,30 @@ def code_line(text: str, line: str, colno: int) -> str:
:return: A formatted string of the text, line, and a caret
"""
return f'{text}\n{line}\n{" " * colno}^'
+
+@T.overload
+def ci_fold_file(fname: T.Union[str, os.PathLike], banner: str, force: Literal[True] = True) -> str: ...
+
+@T.overload
+def ci_fold_file(fname: T.Union[str, os.PathLike], banner: str, force: Literal[False] = False) -> T.Optional[str]: ...
+
+def ci_fold_file(fname: T.Union[str, os.PathLike], banner: str, force: bool = False) -> T.Optional[str]:
+ if not _in_ci and not force:
+ return None
+
+ if _ci_is_github:
+ header = f'::group::==== {banner} ===='
+ footer = '::endgroup::'
+ elif force:
+ header = banner
+ footer = ''
+ elif 'MESON_FORCE_SHOW_LOGS' in os.environ:
+ header = f'==== Forcing display of logs for {os.path.basename(fname)} ===='
+ footer = ''
+ else:
+ # only github is implemented
+ return None
+
+ with open(fname, 'r', encoding='utf-8') as f:
+ data = f.read()
+ return f'{header}\n{data}\n{footer}\n'