From a08b7e3b0c52d25b86b658ca21cf2ffb600a9b2f Mon Sep 17 00:00:00 2001 From: Simon Friis Vindum Date: Wed, 2 Dec 2026 12:26:10 +0100 Subject: [PATCH 1/4] CI: Rename Clippy CI to Rust checks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a2140248-0a6d-4c90-82bd-db163885d135 --- .../{cargo-clippy.yml => rust-checks.yml} | 18 +++++++++--------- .../rust_checks.py} | 4 ++-- .../test_rust_checks.py} | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) rename .github/workflows/{cargo-clippy.yml => rust-checks.yml} (78%) rename misc/scripts/{cargo-clippy/cargo_clippy.py => rust-checks/rust_checks.py} (98%) rename misc/scripts/{cargo-clippy/test_cargo_clippy.py => rust-checks/test_rust_checks.py} (96%) diff --git a/.github/workflows/cargo-clippy.yml b/.github/workflows/rust-checks.yml similarity index 78% rename from .github/workflows/cargo-clippy.yml rename to .github/workflows/rust-checks.yml index d3b40974f6da..7c3ce2964380 100644 --- a/.github/workflows/cargo-clippy.yml +++ b/.github/workflows/rust-checks.yml @@ -1,4 +1,4 @@ -name: Run Clippy on changed Rust code +name: Rust checks # Run on every PR; package selection below skips lint setup for unrelated changes. on: @@ -14,11 +14,11 @@ permissions: concurrency: # Cancel superseded PR checks; each push gets its own group so GitHub cannot # replace an already-pending push run with a newer one. - group: cargo-clippy-${{ github.event_name }}-${{ github.event.pull_request.number || github.run_id }} + group: rust-checks-${{ github.event_name }}-${{ github.event.pull_request.number || github.run_id }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - clippy: + rust-checks: runs-on: ubuntu-latest env: CARGO_TERM_COLOR: always @@ -29,14 +29,14 @@ jobs: # history to compare the event's before SHA across multi-commit pushes. fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 0 }} - name: Test package selection - run: python3 -m unittest discover -s misc/scripts/cargo-clippy + run: python3 -m unittest discover -s misc/scripts/rust-checks - name: Select affected packages id: select env: # Use the PR merge base's actual first parent, or the push's before SHA. BASE: ${{ github.event_name == 'pull_request' && 'HEAD^1' || github.event.before }} run: | - packages=$(python3 misc/scripts/cargo-clippy/cargo_clippy.py select --base "$BASE") + packages=$(python3 misc/scripts/rust-checks/rust_checks.py select --base "$BASE") echo "packages=$packages" >> "$GITHUB_OUTPUT" echo "Selected packages: $packages" | tee -a "$GITHUB_STEP_SUMMARY" - name: Restore Cargo cache @@ -48,9 +48,9 @@ jobs: ~/.cargo/registry ~/.cargo/git target - key: cargo-clippy-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }}-${{ github.sha }} + key: rust-checks-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }}-${{ github.sha }} restore-keys: | - cargo-clippy-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }}- + rust-checks-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }}- - name: Show stable toolchain and cache if: steps.select.outputs.packages != '[]' env: @@ -62,11 +62,11 @@ jobs: - name: Inject ast-generator sources if: contains(fromJSON(steps.select.outputs.packages), 'ast-generator') run: bazel run //rust/ast-generator:inject-sources - - name: Clippy + - name: Run Clippy if: steps.select.outputs.packages != '[]' env: PACKAGES: ${{ steps.select.outputs.packages }} # For the unified extractor: skip linking the native Swift shim and # only check Rust code. SWIFT_SYNTAX_RS_CHECK_ONLY: "1" - run: python3 misc/scripts/cargo-clippy/cargo_clippy.py lint "$PACKAGES" + run: python3 misc/scripts/rust-checks/rust_checks.py lint "$PACKAGES" diff --git a/misc/scripts/cargo-clippy/cargo_clippy.py b/misc/scripts/rust-checks/rust_checks.py similarity index 98% rename from misc/scripts/cargo-clippy/cargo_clippy.py rename to misc/scripts/rust-checks/rust_checks.py index 5221e9091db5..702e9d1296ea 100644 --- a/misc/scripts/cargo-clippy/cargo_clippy.py +++ b/misc/scripts/rust-checks/rust_checks.py @@ -17,8 +17,8 @@ "clippy.toml", ".clippy.toml", ".cargo/", - ".github/workflows/cargo-clippy.yml", - "misc/scripts/cargo-clippy/", + ".github/workflows/rust-checks.yml", + "misc/scripts/rust-checks/", ) # Inputs read by build scripts outside their package's directory. diff --git a/misc/scripts/cargo-clippy/test_cargo_clippy.py b/misc/scripts/rust-checks/test_rust_checks.py similarity index 96% rename from misc/scripts/cargo-clippy/test_cargo_clippy.py rename to misc/scripts/rust-checks/test_rust_checks.py index 78f04fb02666..6b6c54a6eb8e 100644 --- a/misc/scripts/cargo-clippy/test_cargo_clippy.py +++ b/misc/scripts/rust-checks/test_rust_checks.py @@ -1,7 +1,7 @@ import unittest from pathlib import Path -from cargo_clippy import changed_files, select, workspace +from rust_checks import changed_files, select, workspace ROOT = Path(__file__).resolve().parents[3] From 6f3a4657ecfc5cab6f67467ee5dd2c478c4c5776 Mon Sep 17 00:00:00 2001 From: Simon Friis Vindum Date: Wed, 2 Dec 2026 12:57:13 +0100 Subject: [PATCH 2/4] CI: Check formatting and run tests in Rust checks CI Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a2140248-0a6d-4c90-82bd-db163885d135 --- .github/workflows/rust-checks.yml | 14 ++++- misc/scripts/rust-checks/rust_checks.py | 50 +++++++++++++++- misc/scripts/rust-checks/test_rust_checks.py | 61 ++++++++++++++++++-- 3 files changed, 116 insertions(+), 9 deletions(-) diff --git a/.github/workflows/rust-checks.yml b/.github/workflows/rust-checks.yml index 7c3ce2964380..e78347fec07b 100644 --- a/.github/workflows/rust-checks.yml +++ b/.github/workflows/rust-checks.yml @@ -1,6 +1,6 @@ name: Rust checks -# Run on every PR; package selection below skips lint setup for unrelated changes. +# Run on every PR; package selection below skips checks for unrelated changes. on: pull_request: push: @@ -62,6 +62,11 @@ jobs: - name: Inject ast-generator sources if: contains(fromJSON(steps.select.outputs.packages), 'ast-generator') run: bazel run //rust/ast-generator:inject-sources + - name: Check formatting + if: steps.select.outputs.packages != '[]' + env: + PACKAGES: ${{ steps.select.outputs.packages }} + run: python3 misc/scripts/rust-checks/rust_checks.py format "$PACKAGES" - name: Run Clippy if: steps.select.outputs.packages != '[]' env: @@ -70,3 +75,10 @@ jobs: # only check Rust code. SWIFT_SYNTAX_RS_CHECK_ONLY: "1" run: python3 misc/scripts/rust-checks/rust_checks.py lint "$PACKAGES" + - name: Run tests + if: steps.select.outputs.packages != '[]' + env: + PACKAGES: ${{ steps.select.outputs.packages }} + # Skips the unified extractor crates, whose tests need a Swift toolchain, + # and ast-generator, which has no tests of its own. + run: python3 misc/scripts/rust-checks/rust_checks.py test "$PACKAGES" diff --git a/misc/scripts/rust-checks/rust_checks.py b/misc/scripts/rust-checks/rust_checks.py index 702e9d1296ea..529650bf60b0 100644 --- a/misc/scripts/rust-checks/rust_checks.py +++ b/misc/scripts/rust-checks/rust_checks.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Select root-workspace packages affected by a PR or push, and lint them with Clippy.""" +"""Select root-workspace packages affected by a PR or push, runs Rust checks on them.""" import argparse import json @@ -16,6 +16,8 @@ "rust-toolchain.toml", "clippy.toml", ".clippy.toml", + "rustfmt.toml", + ".rustfmt.toml", ".cargo/", ".github/workflows/rust-checks.yml", "misc/scripts/rust-checks/", @@ -30,6 +32,14 @@ ".bazelversion": "rust/ast-generator", } +# We skip running tests for: +# - The unified extractor and swift-syntax-rs: the tests link the swift-syntax +# parser, which needs a Swift toolchain that this workflow does not install. +# - The ast-generator: its only test comes from the injected rust-analyzer +# sources. They make no sense to run and expect the rust-analyzer repository +# layout. +UNTESTED_PACKAGES = {"codeql-extractor-unified", "swift-syntax-rs", "ast-generator"} + # Read workspace packages and map each package to its path dependents. def workspace(root): @@ -111,7 +121,28 @@ def lint(packages): return subprocess.run([*args, "--", "-D", "warnings"]).returncode -# Parse the command and dispatch selection or linting. +# Run rustfmt on the selected packages and return its exit code. +def check_format(packages): + if not packages: + return 0 + args = ["cargo", "fmt"] + for package in packages: + args.extend(["--package", package]) + return subprocess.run([*args, "--", "--check"]).returncode + + +# Run tests for the selected packages and return Cargo's exit code. +def test(packages): + packages = [package for package in packages if package not in UNTESTED_PACKAGES] + if not packages: + return 0 + args = ["cargo", "test", "--locked", "--no-fail-fast"] + for package in packages: + args.extend(["--package", package]) + return subprocess.run(args).returncode + + +# Parse the command and dispatch selection or checks. def main(): parser = argparse.ArgumentParser(description=__doc__) commands = parser.add_subparsers(dest="command", required=True) @@ -122,14 +153,27 @@ def main(): "--base", required=True, help="PR merge's first parent, or push event's before SHA (zero for a new branch)", ) + lint_parser = commands.add_parser("lint", help="run Clippy on the given packages") lint_parser.add_argument("packages", help="JSON list printed by the select command") + + format_parser = commands.add_parser("format", help="check formatting of the given packages") + format_parser.add_argument("packages", help="JSON list printed by the select command") + + test_parser = commands.add_parser("test", help="run tests for the given packages") + test_parser.add_argument("packages", help="JSON list printed by the select command") + args = parser.parse_args() root = Path(__file__).resolve().parents[3] + if args.command == "select": print(json.dumps(select(root, changed_files(root, args.base)))) - else: + elif args.command == "lint": raise SystemExit(lint(json.loads(args.packages))) + elif args.command == "format": + raise SystemExit(check_format(json.loads(args.packages))) + else: + raise SystemExit(test(json.loads(args.packages))) if __name__ == "__main__": main() diff --git a/misc/scripts/rust-checks/test_rust_checks.py b/misc/scripts/rust-checks/test_rust_checks.py index 6b6c54a6eb8e..3f05010ca96c 100644 --- a/misc/scripts/rust-checks/test_rust_checks.py +++ b/misc/scripts/rust-checks/test_rust_checks.py @@ -1,7 +1,14 @@ import unittest from pathlib import Path +from unittest.mock import patch -from rust_checks import changed_files, select, workspace +from rust_checks import ( + changed_files, + check_format, + select, + test, + workspace, +) ROOT = Path(__file__).resolve().parents[3] @@ -12,11 +19,15 @@ def test_unrelated_changes_select_nothing(self): self.assertEqual(select(ROOT, [ "README.md", "rust/ql/test/example/test.rs", + "rust/ql/test/example/rustfmt.toml", "python/extractor/tsg-python/src/main.rs", + "python/extractor/tsg-python/tsp/bindings/rust/lib.rs", + "python/extractor/tsg-python/.rustfmt.toml", ]), []) def test_nested_member_and_reverse_dependencies(self): self.assertEqual(select(ROOT, ["rust/extractor/src/main.rs"]), ["codeql-rust"]) + self.assertEqual(select(ROOT, ["rust/extractor/.rustfmt.toml"]), ["codeql-rust"]) self.assertEqual( select(ROOT, ["ql/buramu/tree-sitter-blame/src/parser.c"]), ["codeql-extractor-ql", "tree-sitter-blame"], @@ -35,10 +46,18 @@ def test_external_inputs(self): def test_global_inputs_select_everything(self): packages, _ = workspace(ROOT) - self.assertEqual( - select(ROOT, ["Cargo.lock"]), - sorted(package["package"]["name"] for package in packages.values()), - ) + for path in ( + "Cargo.lock", + "rustfmt.toml", + ".rustfmt.toml", + ".github/workflows/rust-checks.yml", + "misc/scripts/rust-checks/rust_checks.py", + ): + with self.subTest(path=path): + self.assertEqual( + select(ROOT, [path]), + sorted(package["package"]["name"] for package in packages.values()), + ) def test_missing_base_selects_everything(self): packages, _ = workspace(ROOT) @@ -48,5 +67,37 @@ def test_missing_base_selects_everything(self): ) +class FormatTests(unittest.TestCase): + @patch("rust_checks.subprocess.run") + def test_checks_selected_packages_and_propagates_failure(self, run): + run.return_value.returncode = 1 + self.assertEqual(check_format(["one", "two"]), 1) + run.assert_called_once_with([ + "cargo", "fmt", "--package", "one", "--package", "two", "--", "--check", + ]) + + @patch("rust_checks.subprocess.run") + def test_empty_selection_skips_cargo(self, run): + self.assertEqual(check_format([]), 0) + run.assert_not_called() + + +class TestTests(unittest.TestCase): + @patch("rust_checks.subprocess.run") + def test_skips_untested_packages(self, run): + run.return_value.returncode = 1 + self.assertEqual( + test(["ast-generator", "codeql-extractor-unified", "swift-syntax-rs", "yeast"]), 1, + ) + run.assert_called_once_with( + ["cargo", "test", "--locked", "--no-fail-fast", "--package", "yeast"], + ) + + @patch("rust_checks.subprocess.run") + def test_only_untested_packages_skips_cargo(self, run): + self.assertEqual(test(["codeql-extractor-unified", "swift-syntax-rs"]), 0) + run.assert_not_called() + + if __name__ == "__main__": unittest.main() From 521414bc951032299e616e4e7c8426225355ade1 Mon Sep 17 00:00:00 2001 From: Simon Friis Vindum Date: Wed, 2 Dec 2026 12:57:38 +0100 Subject: [PATCH 3/4] CI: Remove Cargo checks subsumed by Rust checks CI Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a2140248-0a6d-4c90-82bd-db163885d135 --- .github/workflows/cpp-bmn.yml | 14 ------ .github/workflows/ql-for-ql-tests.yml | 2 - .github/workflows/rust.yml | 34 -------------- .../workflows/tree-sitter-extractor-test.yml | 45 ------------------- .github/workflows/yeast-test.yml | 32 ------------- 5 files changed, 127 deletions(-) delete mode 100644 .github/workflows/tree-sitter-extractor-test.yml delete mode 100644 .github/workflows/yeast-test.yml diff --git a/.github/workflows/cpp-bmn.yml b/.github/workflows/cpp-bmn.yml index 066db1bdca96..a12ce1070e34 100644 --- a/.github/workflows/cpp-bmn.yml +++ b/.github/workflows/cpp-bmn.yml @@ -24,20 +24,6 @@ permissions: contents: read jobs: - cargo: - runs-on: ubuntu-latest - defaults: - run: - working-directory: cpp/extractor/bmn - steps: - - uses: actions/checkout@v5 - - name: Check formatting - run: cargo fmt --check - - name: Check - run: cargo check - - name: Test - run: cargo test - bazel: runs-on: ubuntu-latest steps: diff --git a/.github/workflows/ql-for-ql-tests.yml b/.github/workflows/ql-for-ql-tests.yml index 1942fd62939e..db2395dfc40b 100644 --- a/.github/workflows/ql-for-ql-tests.yml +++ b/.github/workflows/ql-for-ql-tests.yml @@ -39,8 +39,6 @@ jobs: ~/.cargo/git target key: ${{ runner.os }}-${{ steps.os_version.outputs.version }}-qltest-cargo-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }} - - name: Check formatting - run: cd ql; cargo fmt -p codeql-extractor-ql -p buramu -p tree-sitter-blame -- --check - name: Build extractor run: | cd ql; diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 89d4d91f561c..4bb1cc829159 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -29,40 +29,6 @@ permissions: contents: read jobs: - rust-ast-generator: - runs-on: ubuntu-latest - defaults: - run: - working-directory: rust/ast-generator - steps: - - name: Checkout - uses: actions/checkout@v5 - - name: Inject sources - shell: bash - run: | - bazel run //rust/ast-generator:inject-sources - - name: Format - shell: bash - run: | - cargo fmt --check - - name: Compilation - shell: bash - run: cargo check - rust-code: - runs-on: ubuntu-latest - defaults: - run: - working-directory: rust/extractor - steps: - - name: Checkout - uses: actions/checkout@v5 - - name: Format - shell: bash - run: | - cargo fmt --check - - name: Compilation - shell: bash - run: cargo check rust-codegen: runs-on: ubuntu-latest steps: diff --git a/.github/workflows/tree-sitter-extractor-test.yml b/.github/workflows/tree-sitter-extractor-test.yml deleted file mode 100644 index 97ae51ff7c15..000000000000 --- a/.github/workflows/tree-sitter-extractor-test.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: Test tree-sitter-extractor - -on: - push: - paths: &tree_sitter_paths - - "shared/extractor-common/**" - - "shared/tree-sitter-extractor/**" - - Cargo.toml - - Cargo.lock - - rust-toolchain.toml - - .github/workflows/tree-sitter-extractor-test.yml - branches: - - main - - "rc/*" - pull_request: - paths: *tree_sitter_paths - branches: - - main - - "rc/*" - -env: - CARGO_TERM_COLOR: always - -defaults: - run: - working-directory: shared/tree-sitter-extractor - -permissions: - contents: read - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v5 - - name: Check formatting - run: cargo fmt -p extractor-common -p codeql-extractor -- --check - - name: Run tests - run: cargo test -p extractor-common -p codeql-extractor --verbose - fmt: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v5 - - name: Check formatting - run: cargo fmt -p extractor-common -p codeql-extractor --check diff --git a/.github/workflows/yeast-test.yml b/.github/workflows/yeast-test.yml deleted file mode 100644 index 48e0ca01498f..000000000000 --- a/.github/workflows/yeast-test.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Test Yeast - -on: - push: - paths: - - "shared/yeast/**" - - "shared/yeast-macros/**" - - "shared/yeast-schema/**" - - .github/workflows/yeast-test.yml - branches: - - main - - "rc/*" - pull_request: - paths: - - "shared/yeast/**" - - "shared/yeast-macros/**" - - "shared/yeast-schema/**" - - .github/workflows/yeast-test.yml - -env: - CARGO_TERM_COLOR: always - -permissions: - contents: read - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v5 - - name: Run tests - run: cargo test --verbose --package codeql-extractor --package yeast --package yeast-macros --package yeast-schema From 325f251b7b9d485a9b7e1466d54e7f205e71a8b3 Mon Sep 17 00:00:00 2001 From: Simon Friis Vindum Date: Wed, 2 Dec 2026 13:55:01 +0100 Subject: [PATCH 4/4] CI: Don't run tests for Rust check script in CI --- .github/workflows/rust-checks.yml | 2 -- misc/scripts/rust-checks/rust_checks.py | 6 +++++- misc/scripts/rust-checks/test_rust_checks.py | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust-checks.yml b/.github/workflows/rust-checks.yml index e78347fec07b..6b90cebf667c 100644 --- a/.github/workflows/rust-checks.yml +++ b/.github/workflows/rust-checks.yml @@ -28,8 +28,6 @@ jobs: # PRs compare the merge commit with its first parent; pushes need full # history to compare the event's before SHA across multi-commit pushes. fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 0 }} - - name: Test package selection - run: python3 -m unittest discover -s misc/scripts/rust-checks - name: Select affected packages id: select env: diff --git a/misc/scripts/rust-checks/rust_checks.py b/misc/scripts/rust-checks/rust_checks.py index 529650bf60b0..73ec2bab58f8 100644 --- a/misc/scripts/rust-checks/rust_checks.py +++ b/misc/scripts/rust-checks/rust_checks.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 -"""Select root-workspace packages affected by a PR or push, runs Rust checks on them.""" +"""Select root-workspace packages affected by a PR or push, then run Rust checks. + +When changing this script, run its tests with: + python3 -m unittest discover -s misc/scripts/rust-checks +""" import argparse import json diff --git a/misc/scripts/rust-checks/test_rust_checks.py b/misc/scripts/rust-checks/test_rust_checks.py index 3f05010ca96c..0cd6acc715bd 100644 --- a/misc/scripts/rust-checks/test_rust_checks.py +++ b/misc/scripts/rust-checks/test_rust_checks.py @@ -1,3 +1,8 @@ +"""Tests for rust_checks.py. Run from the repository root with: + + python3 -m unittest discover -s misc/scripts/rust-checks +""" + import unittest from pathlib import Path from unittest.mock import patch