Skip to content

[CI] Setup generate_report to describe ninja failures #152621

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

135 changes: 118 additions & 17 deletions .ci/generate_test_report_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,57 @@
"https://github.com/llvm/llvm-project/issues and add the "
"`infrastructure` label."
)
# The maximum number of lines to pull from a ninja failure.
NINJA_LOG_SIZE_THRESHOLD = 500


def _parse_ninja_log(ninja_log: list[str]) -> list[tuple[str, str]]:
"""Parses an individual ninja log."""
failures = []
index = 0
while index < len(ninja_log):
while index < len(ninja_log) and not ninja_log[index].startswith("FAILED:"):
index += 1
if index == len(ninja_log):
# We hit the end of the log without finding a build failure, go to
# the next log.
return failures
failing_action = ninja_log[index - 1].split("] ")[1]
failure_log = []
while (
index < len(ninja_log)
and not ninja_log[index].startswith("[")
and not ninja_log[index].startswith(
"ninja: build stopped: subcommand failed"
)
and len(failure_log) < NINJA_LOG_SIZE_THRESHOLD
):
failure_log.append(ninja_log[index])
index += 1
failures.append((failing_action, "\n".join(failure_log)))
return failures


def find_failure_in_ninja_logs(ninja_logs: list[list[str]]) -> list[tuple[str, str]]:
"""Extracts failure messages from ninja output.

This patch takes stdout/stderr from ninja in the form of a list of files
represented as a list of lines. This function then returns tuples containing
the name of the target and the error message.

Args:
ninja_logs: A list of files in the form of a list of lines representing the log
files captured from ninja.

Returns:
A list of tuples. The first string is the name of the target that failed. The
second string is the error message.
"""
failures = []
for ninja_log in ninja_logs:
log_failures = _parse_ninja_log(ninja_log)
failures.extend(log_failures)
return failures


# Set size_limit to limit the byte size of the report. The default is 1MB as this
Expand All @@ -24,6 +75,7 @@ def generate_report(
title,
return_code,
junit_objects,
ninja_logs: list[list[str]],
size_limit=1024 * 1024,
list_failures=True,
):
Expand Down Expand Up @@ -61,15 +113,46 @@ def generate_report(
]
)
else:
report.extend(
[
"The build failed before running any tests.",
"",
SEE_BUILD_FILE_STR,
"",
UNRELATED_FAILURES_STR,
]
)
ninja_failures = find_failure_in_ninja_logs(ninja_logs)
if not ninja_failures:
report.extend(
[
"The build failed before running any tests. Detailed "
"information about the build failure could not be "
"automatically obtained.",
"",
SEE_BUILD_FILE_STR,
"",
UNRELATED_FAILURES_STR,
]
)
else:
report.extend(
[
"The build failed before running any tests. Click on the "
"failure below to see the details.",
"",
]
)
for build_failure in ninja_failures:
failed_action, failure_message = build_failure
report.extend(
[
"<details>",
f"<summary>{failed_action}</summary>",
"",
"```",
failure_message,
"```",
"</details>",
]
)
report.extend(
[
"",
UNRELATED_FAILURES_STR,
]
)
return "\n".join(report)

tests_passed = tests_run - tests_skipped - tests_failed
Expand Down Expand Up @@ -114,14 +197,32 @@ def plural(num_tests):
elif return_code != 0:
# No tests failed but the build was in a failed state. Bring this to the user's
# attention.
report.extend(
[
"",
"All tests passed but another part of the build **failed**.",
"",
SEE_BUILD_FILE_STR,
]
)
ninja_failures = find_failure_in_ninja_logs(ninja_logs)
if not ninja_failures:
report.extend(
[
"",
"All tests passed but another part of the build **failed**. "
"Detailed information about the build failure could not be "
"automatically obtained.",
"",
SEE_BUILD_FILE_STR,
]
)
else:
for build_failure in ninja_failures:
failed_action, failure_message = build_failure
report.extend(
[
"<details>",
f"<summary>{failed_action}</summary>",
"",
"```",
failure_message,
"```",
"</details>",
]
)

if failures or return_code != 0:
report.extend(["", UNRELATED_FAILURES_STR])
Expand Down
Loading