From bb4c284c3c82635d4d4f70cc163f716aecd05ecd Mon Sep 17 00:00:00 2001 From: carlos-alm Date: Thu, 16 Jul 2026 00:46:10 -0600 Subject: [PATCH] fix(ci): extract reusable workflow for the native cross-compilation matrix build-native.yml and publish.yml each maintained their own copy of the same 6-target cross-compilation matrix and toolchain setup, with only a one-step difference (Cargo.toml version sync). Extract build-native-matrix.yml as a workflow_call reusable workflow, parameterized by an optional version input, so both callers share one definition instead of two that can silently drift. --- .github/workflows/build-native-matrix.yml | 140 ++++++++++++++++++++++ .github/workflows/build-native.yml | 110 +---------------- .github/workflows/publish.yml | 122 +------------------ 3 files changed, 144 insertions(+), 228 deletions(-) create mode 100644 .github/workflows/build-native-matrix.yml diff --git a/.github/workflows/build-native-matrix.yml b/.github/workflows/build-native-matrix.yml new file mode 100644 index 000000000..612db6cb5 --- /dev/null +++ b/.github/workflows/build-native-matrix.yml @@ -0,0 +1,140 @@ +name: Build Native Matrix + +# Reusable workflow shared by build-native.yml (PR validation) and +# publish.yml (release/dev builds) — see #2110. Keeps the 6-target +# cross-compilation matrix and toolchain setup in one place instead of two +# copies that can silently drift apart. + +on: + workflow_call: + inputs: + version: + description: 'Version to sync into Cargo.toml before building. Leave unset for PR validation builds that should not bump the crate version.' + required: false + type: string + default: '' + +jobs: + build: + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu + node_arch: x64 + node_os: linux + artifact_key: linux-x64 + - os: ubuntu-latest + target: aarch64-unknown-linux-gnu + node_arch: arm64 + node_os: linux + artifact_key: linux-arm64 + cross: true + - os: ubuntu-latest + target: x86_64-unknown-linux-musl + node_arch: x64 + node_os: linux + artifact_key: linux-x64-musl + cross: true + - os: macos-latest + target: aarch64-apple-darwin + node_arch: arm64 + node_os: darwin + artifact_key: darwin-arm64 + - os: macos-14 + target: x86_64-apple-darwin + node_arch: x64 + node_os: darwin + artifact_key: darwin-x64 + - os: windows-latest + target: x86_64-pc-windows-msvc + node_arch: x64 + node_os: win32 + artifact_key: win32-x64 + + runs-on: ${{ matrix.os }} + name: Build ${{ matrix.target }} + permissions: + contents: read + + steps: + - uses: actions/checkout@v7 + + - name: Setup Node.js + uses: actions/setup-node@v6 + with: + node-version: 22 + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + with: + targets: ${{ matrix.target }} + + - name: Verify cargo resolves to rustup proxy (macOS shim workaround) + if: runner.os == 'macOS' + shell: bash + run: | + # macOS runners (most often `macos-14` ARM, but also observed on + # `macos-latest`/aarch64 builds — see #1136) intermittently expose + # a stale `cargo` on PATH that resolves to `rustup-init`, breaking + # `cargo metadata` invocations from napi-rs. Force the rustup + # proxy bin dir to the front of PATH and fail fast if cargo is + # unhealthy. Applied to all macOS matrix entries as a safety net. + echo "$HOME/.cargo/bin" >> "$GITHUB_PATH" + "$HOME/.cargo/bin/cargo" --version + "$HOME/.cargo/bin/rustc" --version + + - name: Rust cache + uses: Swatinem/rust-cache@v2 + with: + workspaces: crates/codegraph-core + + - name: Install cross-compilation tools (aarch64-gnu) + if: matrix.target == 'aarch64-unknown-linux-gnu' + run: | + sudo apt-get update + sudo apt-get install -y gcc-aarch64-linux-gnu + echo "CC=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV" + echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV" + + - name: Install cross-compilation tools (x86_64-musl) + if: matrix.target == 'x86_64-unknown-linux-musl' + run: | + sudo apt-get update + sudo apt-get install -y musl-tools + # Create linker script so -lgcc_s resolves to static libgcc + MUSL_LIB_DIR="$(dirname "$(musl-gcc -print-libgcc-file-name)")" + echo 'INPUT(-lgcc)' | sudo tee "${MUSL_LIB_DIR}/libgcc_s.so" > /dev/null + echo "CC_x86_64_unknown_linux_musl=musl-gcc" >> "$GITHUB_ENV" + echo "CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=musl-gcc" >> "$GITHUB_ENV" + # Symlink libgcc_s into Rust sysroot so rust-lld can find it for host builds + GNU_LIB="$(rustc --print sysroot)/lib/rustlib/x86_64-unknown-linux-gnu/lib" + sudo ln -sf /lib/x86_64-linux-gnu/libgcc_s.so.1 "${GNU_LIB}/libgcc_s.so" + sudo ln -sf /lib/x86_64-linux-gnu/libgcc_s.so.1 "${GNU_LIB}/libgcc_s.so.1" + + - name: Sync Cargo.toml version + if: inputs.version != '' + env: + VERSION: ${{ inputs.version }} + shell: bash + run: | + [[ -n "$VERSION" ]] || { echo "::error::VERSION is empty despite input check"; exit 1; } + CARGO="crates/codegraph-core/Cargo.toml" + awk -v v="$VERSION" '!done && /^version =/{$0="version = \""v"\""; done=1}1' "$CARGO" > "${CARGO}.tmp" + mv "${CARGO}.tmp" "$CARGO" + + - name: Install napi-rs CLI + timeout-minutes: 5 + run: npm install -g @napi-rs/cli@3 + + - name: Build native addon + working-directory: crates/codegraph-core + run: napi build --release --target ${{ matrix.target }} + + - name: Upload native binary + uses: actions/upload-artifact@v7 + with: + name: native-${{ matrix.artifact_key }} + path: crates/codegraph-core/*.node + if-no-files-found: error diff --git a/.github/workflows/build-native.yml b/.github/workflows/build-native.yml index f57830524..f16b31d0d 100644 --- a/.github/workflows/build-native.yml +++ b/.github/workflows/build-native.yml @@ -12,114 +12,6 @@ permissions: {} jobs: build: - strategy: - fail-fast: false - matrix: - include: - - os: ubuntu-latest - target: x86_64-unknown-linux-gnu - node_arch: x64 - node_os: linux - artifact_key: linux-x64 - - os: ubuntu-latest - target: aarch64-unknown-linux-gnu - node_arch: arm64 - node_os: linux - artifact_key: linux-arm64 - cross: true - - os: ubuntu-latest - target: x86_64-unknown-linux-musl - node_arch: x64 - node_os: linux - artifact_key: linux-x64-musl - cross: true - - os: macos-latest - target: aarch64-apple-darwin - node_arch: arm64 - node_os: darwin - artifact_key: darwin-arm64 - - os: macos-14 - target: x86_64-apple-darwin - node_arch: x64 - node_os: darwin - artifact_key: darwin-x64 - - os: windows-latest - target: x86_64-pc-windows-msvc - node_arch: x64 - node_os: win32 - artifact_key: win32-x64 - - runs-on: ${{ matrix.os }} - name: Build ${{ matrix.target }} + uses: ./.github/workflows/build-native-matrix.yml permissions: contents: read - - steps: - - uses: actions/checkout@v7 - - - name: Setup Node.js - uses: actions/setup-node@v6 - with: - node-version: 22 - - - name: Setup Rust - uses: dtolnay/rust-toolchain@stable - with: - targets: ${{ matrix.target }} - - - name: Verify cargo resolves to rustup proxy (macOS shim workaround) - if: runner.os == 'macOS' - shell: bash - run: | - # macOS runners (most often `macos-14` ARM, but also observed on - # `macos-latest`/aarch64 builds — see #1136) intermittently expose - # a stale `cargo` on PATH that resolves to `rustup-init`, breaking - # `cargo metadata` invocations from napi-rs. Force the rustup - # proxy bin dir to the front of PATH and fail fast if cargo is - # unhealthy. Applied to all macOS matrix entries as a safety net. - echo "$HOME/.cargo/bin" >> "$GITHUB_PATH" - "$HOME/.cargo/bin/cargo" --version - "$HOME/.cargo/bin/rustc" --version - - - name: Rust cache - uses: Swatinem/rust-cache@v2 - with: - workspaces: crates/codegraph-core - - - name: Install cross-compilation tools (aarch64-gnu) - if: matrix.target == 'aarch64-unknown-linux-gnu' - run: | - sudo apt-get update - sudo apt-get install -y gcc-aarch64-linux-gnu - echo "CC=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV" - echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV" - - - name: Install cross-compilation tools (x86_64-musl) - if: matrix.target == 'x86_64-unknown-linux-musl' - run: | - sudo apt-get update - sudo apt-get install -y musl-tools - # Create linker script so -lgcc_s resolves to static libgcc - MUSL_LIB_DIR="$(dirname "$(musl-gcc -print-libgcc-file-name)")" - echo 'INPUT(-lgcc)' | sudo tee "${MUSL_LIB_DIR}/libgcc_s.so" > /dev/null - echo "CC_x86_64_unknown_linux_musl=musl-gcc" >> "$GITHUB_ENV" - echo "CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=musl-gcc" >> "$GITHUB_ENV" - # Symlink libgcc_s into Rust sysroot so rust-lld can find it for host builds - GNU_LIB="$(rustc --print sysroot)/lib/rustlib/x86_64-unknown-linux-gnu/lib" - sudo ln -sf /lib/x86_64-linux-gnu/libgcc_s.so.1 "${GNU_LIB}/libgcc_s.so" - sudo ln -sf /lib/x86_64-linux-gnu/libgcc_s.so.1 "${GNU_LIB}/libgcc_s.so.1" - - - name: Install napi-rs CLI - timeout-minutes: 5 - run: npm install -g @napi-rs/cli@3 - - - name: Build native addon - working-directory: crates/codegraph-core - run: napi build --release --target ${{ matrix.target }} - - - name: Upload artifact - uses: actions/upload-artifact@v7 - with: - name: native-${{ matrix.artifact_key }} - path: crates/codegraph-core/*.node - if-no-files-found: error diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index e937e8404..151a40964 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -121,128 +121,12 @@ jobs: build-native: needs: [preflight, compute-version] - strategy: - fail-fast: false - matrix: - include: - - os: ubuntu-latest - target: x86_64-unknown-linux-gnu - node_arch: x64 - node_os: linux - artifact_key: linux-x64 - - os: ubuntu-latest - target: aarch64-unknown-linux-gnu - node_arch: arm64 - node_os: linux - artifact_key: linux-arm64 - cross: true - - os: ubuntu-latest - target: x86_64-unknown-linux-musl - node_arch: x64 - node_os: linux - artifact_key: linux-x64-musl - cross: true - - os: macos-latest - target: aarch64-apple-darwin - node_arch: arm64 - node_os: darwin - artifact_key: darwin-arm64 - - os: macos-14 - target: x86_64-apple-darwin - node_arch: x64 - node_os: darwin - artifact_key: darwin-x64 - - os: windows-latest - target: x86_64-pc-windows-msvc - node_arch: x64 - node_os: win32 - artifact_key: win32-x64 - - runs-on: ${{ matrix.os }} - name: Build ${{ matrix.target }} + uses: ./.github/workflows/build-native-matrix.yml + with: + version: ${{ needs.compute-version.outputs.version }} permissions: contents: read - steps: - - uses: actions/checkout@v7 - - - name: Setup Node.js - uses: actions/setup-node@v6 - with: - node-version: 22 - - - name: Setup Rust - uses: dtolnay/rust-toolchain@stable - with: - targets: ${{ matrix.target }} - - - name: Verify cargo resolves to rustup proxy (macOS shim workaround) - if: runner.os == 'macOS' - shell: bash - run: | - # macOS runners (most often `macos-14` ARM, but also observed on - # `macos-latest`/aarch64 builds — see #1136) intermittently expose - # a stale `cargo` on PATH that resolves to `rustup-init`, breaking - # `cargo metadata` invocations from napi-rs. Force the rustup - # proxy bin dir to the front of PATH and fail fast if cargo is - # unhealthy. Applied to all macOS matrix entries as a safety net. - echo "$HOME/.cargo/bin" >> "$GITHUB_PATH" - "$HOME/.cargo/bin/cargo" --version - "$HOME/.cargo/bin/rustc" --version - - - name: Rust cache - uses: Swatinem/rust-cache@v2 - with: - workspaces: crates/codegraph-core - - - name: Install cross-compilation tools (aarch64-gnu) - if: matrix.target == 'aarch64-unknown-linux-gnu' - run: | - sudo apt-get update - sudo apt-get install -y gcc-aarch64-linux-gnu - echo "CC=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV" - echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV" - - - name: Install cross-compilation tools (x86_64-musl) - if: matrix.target == 'x86_64-unknown-linux-musl' - run: | - sudo apt-get update - sudo apt-get install -y musl-tools - # Create linker script so -lgcc_s resolves to static libgcc - MUSL_LIB_DIR="$(dirname "$(musl-gcc -print-libgcc-file-name)")" - echo 'INPUT(-lgcc)' | sudo tee "${MUSL_LIB_DIR}/libgcc_s.so" > /dev/null - echo "CC_x86_64_unknown_linux_musl=musl-gcc" >> "$GITHUB_ENV" - echo "CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=musl-gcc" >> "$GITHUB_ENV" - # Symlink libgcc_s into Rust sysroot so rust-lld can find it for host builds - GNU_LIB="$(rustc --print sysroot)/lib/rustlib/x86_64-unknown-linux-gnu/lib" - sudo ln -sf /lib/x86_64-linux-gnu/libgcc_s.so.1 "${GNU_LIB}/libgcc_s.so" - sudo ln -sf /lib/x86_64-linux-gnu/libgcc_s.so.1 "${GNU_LIB}/libgcc_s.so.1" - - - name: Sync Cargo.toml version - env: - VERSION: ${{ needs.compute-version.outputs.version }} - shell: bash - run: | - [[ -n "$VERSION" ]] || { echo "::error::VERSION is empty — compute-version output missing"; exit 1; } - CARGO="crates/codegraph-core/Cargo.toml" - awk -v v="$VERSION" '!done && /^version =/{$0="version = \""v"\""; done=1}1' "$CARGO" > "${CARGO}.tmp" - mv "${CARGO}.tmp" "$CARGO" - - - name: Install napi-rs CLI - timeout-minutes: 5 - run: npm install -g @napi-rs/cli@3 - - - name: Build native addon - working-directory: crates/codegraph-core - run: napi build --release --target ${{ matrix.target }} - - - name: Upload native binary - uses: actions/upload-artifact@v7 - with: - name: native-${{ matrix.artifact_key }} - path: crates/codegraph-core/*.node - if-no-files-found: error - # ── Pre-publish benchmark gate (stable releases only) ── # # Measures the just-built native artifact against the local source, writes