|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +"""Run cargo clippy with appropriate flags and emit GitHub Actions annotations.""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | + |
| 10 | + |
| 11 | +def get_utility_list(features: str) -> list[str]: |
| 12 | + """Get list of utilities via show-utils.sh.""" |
| 13 | + if features == "all": |
| 14 | + cmd = ["./util/show-utils.sh", "--all-features"] |
| 15 | + else: |
| 16 | + cmd = ["./util/show-utils.sh", "--features", features] |
| 17 | + result = subprocess.run(cmd, capture_output=True, text=True, check=True) |
| 18 | + return result.stdout.split() |
| 19 | + |
| 20 | + |
| 21 | +def build_clippy_command( |
| 22 | + features: str, workspace: bool, target: str | None |
| 23 | +) -> list[str]: |
| 24 | + """Build the cargo clippy command line.""" |
| 25 | + cmd = ["cargo", "clippy"] |
| 26 | + |
| 27 | + extra = [] |
| 28 | + if features == "all": |
| 29 | + extra.append("--all-features") |
| 30 | + else: |
| 31 | + extra.extend(["--features", features]) |
| 32 | + |
| 33 | + if workspace: |
| 34 | + extra.append("--workspace") |
| 35 | + |
| 36 | + if target: |
| 37 | + extra.extend(["--no-default-features", "--target", target]) |
| 38 | + # For cross-compilation targets, just check -pcoreutils |
| 39 | + # (show-utils.sh over-resolves due to default features) |
| 40 | + extra.append("-pcoreutils") |
| 41 | + else: |
| 42 | + extra.extend(["--all-targets", "--tests", "--benches", "-pcoreutils"]) |
| 43 | + utilities = get_utility_list(features) |
| 44 | + for u in utilities: |
| 45 | + extra.append(f"-puu_{u}") |
| 46 | + |
| 47 | + cmd.extend(extra) |
| 48 | + cmd.extend(["--", "-D", "warnings"]) |
| 49 | + return cmd |
| 50 | + |
| 51 | + |
| 52 | +# Pattern to match clippy/rustc errors for GHA annotations |
| 53 | +ERROR_PATTERN = re.compile(r"^error:\s+(.*)\n\s+-->\s+(.*):(\d+):(\d+)", re.MULTILINE) |
| 54 | + |
| 55 | + |
| 56 | +def emit_annotations(output: str, fault_type: str) -> None: |
| 57 | + """Emit GitHub Actions annotations from cargo clippy errors.""" |
| 58 | + fault_prefix = fault_type.upper() |
| 59 | + for m in ERROR_PATTERN.finditer(output): |
| 60 | + message, file, line, col = m.groups() |
| 61 | + print( |
| 62 | + f"::{fault_type} file={file},line={line},col={col}" |
| 63 | + f"::{fault_prefix}: `cargo clippy`: {message} (file:'{file}', line:{line})" |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def main() -> int: |
| 68 | + parser = argparse.ArgumentParser(description="Run cargo clippy for CI") |
| 69 | + parser.add_argument("--features", required=True, help="Feature set to use") |
| 70 | + parser.add_argument( |
| 71 | + "--workspace", action="store_true", help="Include --workspace flag" |
| 72 | + ) |
| 73 | + parser.add_argument("--target", default=None, help="Cross-compilation target") |
| 74 | + parser.add_argument( |
| 75 | + "--fault-type", |
| 76 | + default="warning", |
| 77 | + choices=["warning", "error"], |
| 78 | + help="GHA annotation type", |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "--fail-on-fault", |
| 82 | + action="store_true", |
| 83 | + help="Exit with error code on clippy failures", |
| 84 | + ) |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + cmd = build_clippy_command(args.features, args.workspace, args.target) |
| 88 | + print(f"Running: {' '.join(cmd)}", file=sys.stderr) |
| 89 | + |
| 90 | + result = subprocess.run(cmd, capture_output=True, text=True) |
| 91 | + output = result.stdout + result.stderr |
| 92 | + |
| 93 | + # Always print the full output |
| 94 | + print(output) |
| 95 | + |
| 96 | + if result.returncode != 0: |
| 97 | + emit_annotations(output, args.fault_type) |
| 98 | + if args.fail_on_fault: |
| 99 | + return 1 |
| 100 | + |
| 101 | + return 0 |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + sys.exit(main()) |
0 commit comments