From c6581eb22cb8f7cc9382e935e736697e00b796cd Mon Sep 17 00:00:00 2001 From: Nathanael Huffman Date: Fri, 6 Feb 2026 15:05:13 -0500 Subject: [PATCH] Fix change detection reference for PRs that have merged- ie CI running on main should use HEAD-1 as the reference because HEAD is the current change --- .github/scripts/parse-affected-targets.py | 14 ++++++++---- .github/workflows/build.yml | 26 +++++++++++++++++------ .github/workflows/simulation.yml | 26 +++++++++++++++++------ 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/.github/scripts/parse-affected-targets.py b/.github/scripts/parse-affected-targets.py index c0122ed5..66208d82 100755 --- a/.github/scripts/parse-affected-targets.py +++ b/.github/scripts/parse-affected-targets.py @@ -196,11 +196,12 @@ def generate_matrix_json(targets: List[str]) -> str: def main(): if len(sys.argv) < 3: - print(f"Usage: {sys.argv[0]} ", file=sys.stderr) + print(f"Usage: {sys.argv[0]} [--fallback]", file=sys.stderr) sys.exit(1) btd_json = sys.argv[1] output_dir = Path(sys.argv[2]) + fallback = '--fallback' in sys.argv output_dir.mkdir(parents=True, exist_ok=True) # Get all targets from BXL/cquery @@ -211,10 +212,15 @@ def main(): print("Parsing BTD output...", file=sys.stderr) affected_targets = parse_btd_output(btd_json) - # If BTD returned no targets (fallback case), run everything - if len(affected_targets) == 0: - print("No affected targets from BTD (fallback mode) - running all targets", file=sys.stderr) + # Only fall back to running everything when BTD itself failed + # (indicated by --fallback); zero targets from a successful BTD + # run means nothing was affected. + if fallback: + print("BTD failed (--fallback) - running all targets", file=sys.stderr) affected_by_category = all_targets + affected_targets = set() + for targets in all_targets.values(): + affected_targets.update(targets) else: # Filter to affected only affected_by_category = filter_affected(all_targets, affected_targets) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 49395985..d636b159 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -36,8 +36,13 @@ jobs: - name: Get changed files and base commit id: changed-files run: | - # Always compare against origin/main to detect all changes in the branch - BASE_COMMIT="origin/main" + # On main (post-merge), compare against the previous commit; + # on branches, compare against origin/main to detect all changes. + if [ "${{ github.ref }}" = "refs/heads/main" ]; then + BASE_COMMIT="HEAD~1" + else + BASE_COMMIT="origin/main" + fi # Generate list of changed files in BTD format: "M path/to/file" # BTD expects Mercurial-like format with status code (M/A/D) and space separator @@ -95,6 +100,7 @@ jobs: echo "Found $TARGETS affected targets" echo "BTD output preview:" head -20 /tmp/btd_output.json || true + echo "succeeded=true" >> "$GITHUB_OUTPUT" else EXIT_CODE=$? echo "::error::BTD command failed with exit code $EXIT_CODE" @@ -103,13 +109,14 @@ jobs: echo "BTD stderr:" cat /tmp/btd_stderr.txt || echo "No stderr file generated" echo "::warning::BTD failed, will run all targets" - echo '{"targets": []}' > /tmp/btd_output.json + echo '[]' > /tmp/btd_output.json + echo "succeeded=false" >> "$GITHUB_OUTPUT" fi else echo "::warning::supertd not installed, will run all targets" echo "PATH: $PATH" - # Fallback: empty targets means run everything - echo '{"targets": []}' > /tmp/btd_output.json + echo '[]' > /tmp/btd_output.json + echo "succeeded=false" >> "$GITHUB_OUTPUT" fi - name: Parse affected targets @@ -117,10 +124,15 @@ jobs: run: | mkdir -p /tmp/btd_results - # Run parser script + # Run parser script; --fallback means BTD failed so run everything + FALLBACK_FLAG="" + if [ "${{ steps.btd.outputs.succeeded }}" != "true" ]; then + FALLBACK_FLAG="--fallback" + fi + python3 .github/scripts/parse-affected-targets.py \ /tmp/btd_output.json \ - /tmp/btd_results || { + /tmp/btd_results $FALLBACK_FLAG || { echo "::warning::Parser failed, creating empty matrices" # Create empty matrices on failure for matrix in vunit_matrix bsv_test_matrix ice40_matrix ecp5_matrix vivado_matrix; do diff --git a/.github/workflows/simulation.yml b/.github/workflows/simulation.yml index 1055d62c..be1f5aed 100644 --- a/.github/workflows/simulation.yml +++ b/.github/workflows/simulation.yml @@ -33,8 +33,13 @@ jobs: - name: Get changed files and base commit id: changed-files run: | - # Always compare against origin/main to detect all changes in the branch - BASE_COMMIT="origin/main" + # On main (post-merge), compare against the previous commit; + # on branches, compare against origin/main to detect all changes. + if [ "${{ github.ref }}" = "refs/heads/main" ]; then + BASE_COMMIT="HEAD~1" + else + BASE_COMMIT="origin/main" + fi # Generate list of changed files in BTD format: "M path/to/file" # BTD expects Mercurial-like format with status code (M/A/D) and space separator @@ -92,6 +97,7 @@ jobs: echo "Found $TARGETS affected targets" echo "BTD output preview:" head -20 /tmp/btd_output.json || true + echo "succeeded=true" >> "$GITHUB_OUTPUT" else EXIT_CODE=$? echo "::error::BTD command failed with exit code $EXIT_CODE" @@ -100,13 +106,14 @@ jobs: echo "BTD stderr:" cat /tmp/btd_stderr.txt || echo "No stderr file generated" echo "::warning::BTD failed, will run all targets" - echo '{"targets": []}' > /tmp/btd_output.json + echo '[]' > /tmp/btd_output.json + echo "succeeded=false" >> "$GITHUB_OUTPUT" fi else echo "::warning::supertd not installed, will run all targets" echo "PATH: $PATH" - # Fallback: empty targets means run everything - echo '{"targets": []}' > /tmp/btd_output.json + echo '[]' > /tmp/btd_output.json + echo "succeeded=false" >> "$GITHUB_OUTPUT" fi - name: Parse affected targets @@ -114,10 +121,15 @@ jobs: run: | mkdir -p /tmp/btd_results - # Run parser script + # Run parser script; --fallback means BTD failed so run everything + FALLBACK_FLAG="" + if [ "${{ steps.btd.outputs.succeeded }}" != "true" ]; then + FALLBACK_FLAG="--fallback" + fi + python3 .github/scripts/parse-affected-targets.py \ /tmp/btd_output.json \ - /tmp/btd_results || { + /tmp/btd_results $FALLBACK_FLAG || { echo "::warning::Parser failed, creating empty matrices" # Create empty matrices on failure for matrix in vunit_matrix bsv_test_matrix ice40_matrix ecp5_matrix vivado_matrix; do