Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

ci: stop rebuilding Rust crates between test steps; prebuilt wasm-bindgen (#5827)

Francesco Bonacci61cf27719f37
diff --git a/.github/actions/wasm-bindgen/action.yml b/.github/actions/wasm-bindgen/action.yml
index 8c544ba606..1524374e75 100644
--- a/.github/actions/wasm-bindgen/action.yml
+++ b/.github/actions/wasm-bindgen/action.yml
@@ -1,8 +1,10 @@
name: "Install wasm-bindgen"
description: >-
- Install wasm-bindgen-cli at the version the cua crates pin. The binaries are
- cached per OS and version (compiling the CLI takes 3-5 minutes on a hosted
- runner), and a cache miss builds from source.
+ Install wasm-bindgen-cli at the version the cua crates pin, as the project's
+ prebuilt release binaries (wasm-bindgen, wasm-bindgen-test-runner and
+ wasm2es6js) with their checksums verified. A source build (`cargo install`)
+ takes 2 to 5 minutes on a hosted runner and whenever the Actions cache lost
+ its binary; the download takes seconds and has no cache entry to evict.
inputs:
version:
description: "wasm-bindgen-cli version (must match the wasm-bindgen crate in Cargo.lock)."
@@ -10,21 +12,6 @@ inputs:
runs:
using: composite
steps:
- - id: paths
- shell: bash
- run: |
- ext=""; [ "$RUNNER_OS" = Windows ] && ext=".exe"
- echo "bin=$HOME/.cargo/bin/wasm-bindgen$ext" >> "$GITHUB_OUTPUT"
- echo "runner=$HOME/.cargo/bin/wasm-bindgen-test-runner$ext" >> "$GITHUB_OUTPUT"
- echo "es6=$HOME/.cargo/bin/wasm2es6js$ext" >> "$GITHUB_OUTPUT"
- - id: cache
- uses: actions/cache@v4
+ - uses: taiki-e/install-action@1e29709c0ec63babbe8f266397a465f29a825f91 # v2.87.31
with:
- path: |
- ${{ steps.paths.outputs.bin }}
- ${{ steps.paths.outputs.runner }}
- ${{ steps.paths.outputs.es6 }}
- key: wasm-bindgen-cli-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
- - if: steps.cache.outputs.cache-hit != 'true'
- shell: bash
- run: cargo install wasm-bindgen-cli --version "${{ inputs.version }}" --locked
+ tool: wasm-bindgen@${{ inputs.version }}
diff --git a/.github/workflows/ci-cua-spaces.yml b/.github/workflows/ci-cua-spaces.yml
index b3c56b65cd..0b4748c56d 100644
--- a/.github/workflows/ci-cua-spaces.yml
+++ b/.github/workflows/ci-cua-spaces.yml
@@ -142,12 +142,14 @@ jobs:
node-version: 22
cache: pnpm
cache-dependency-path: apps/cua-spaces/pnpm-lock.yaml
- - name: Install Rust, the wasm32 target and wasm-bindgen
+ - name: Install Rust and the wasm32 target
shell: bash
run: |
rustup show active-toolchain || rustup toolchain install
rustup target add wasm32-unknown-unknown
- cargo install wasm-bindgen-cli --version 0.2.126 --locked
+ # The prebuilt release binary: `cargo install wasm-bindgen-cli` here took
+ # 1.7 minutes (median of 25 runs) on every run.
+ - uses: ./.github/actions/wasm-bindgen
- name: Install protoc
uses: ./.github/actions/setup-protoc
- name: Install Linux build dependencies
diff --git a/libs/cua-driver/rust/crates/cua-driver/build.rs b/libs/cua-driver/rust/crates/cua-driver/build.rs
index e26ecf9bbc..a61ac0d083 100644
--- a/libs/cua-driver/rust/crates/cua-driver/build.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/build.rs
@@ -41,32 +41,61 @@ fn embed_git_sha() {
git(&["rev-parse", "HEAD"]).unwrap_or_default()
};
// Rebuild when the checkout moves: HEAD itself (worktrees keep it in
- // their own git dir), the branch ref it names, and packed refs.
+ // their own git dir), its reflog (appended by every commit, checkout and
+ // reset, so it also follows a branch whose ref is packed), the branch ref
+ // it names, and packed refs. Only paths that exist are watched: Cargo
+ // treats a missing `rerun-if-changed` path as always changed, which
+ // rebuilt this crate and everything above it on every cargo invocation of
+ // a CI checkout (it has no packed-refs file).
if let Some(git_dir) = git(&["rev-parse", "--absolute-git-dir"]) {
let git_dir = std::path::PathBuf::from(git_dir);
- println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
+ rerun_if_exists(&git_dir.join("HEAD"));
+ rerun_if_exists(&git_dir.join("logs").join("HEAD"));
if let Some(common) = git(&["rev-parse", "--git-common-dir"]) {
+ // git prints a relative path relative to the directory it ran in.
let common = std::path::PathBuf::from(common);
let common = if common.is_absolute() {
common
} else {
- git_dir.join(common)
+ std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default())
+ .join(common)
};
- println!(
- "cargo:rerun-if-changed={}",
- common.join("packed-refs").display()
- );
+ rerun_if_exists(&common.join("packed-refs"));
if let Some(reference) = git(&["symbolic-ref", "-q", "HEAD"]) {
- println!(
- "cargo:rerun-if-changed={}",
- common.join(reference).display()
- );
+ rerun_if_ref_changes(&common, &reference);
}
}
}
println!("cargo:rustc-env=CUA_DRIVER_GIT_SHA={sha}");
}
+fn rerun_if_exists(path: &std::path::Path) {
+ if path.exists() {
+ println!("cargo:rerun-if-changed={}", path.display());
+ }
+}
+
+/// Watches the branch ref `reference` (for example `refs/heads/main`). While
+/// it is a loose file, watch that file. When it is packed, git writes a new
+/// loose file at the next commit without touching HEAD, the reflog (it may be
+/// disabled) or packed-refs, so watch the nearest existing directory above it
+/// instead: creating the file changes that directory. Paths that do not exist
+/// are never watched (Cargo would treat them as always changed).
+fn rerun_if_ref_changes(common: &std::path::Path, reference: &str) {
+ let refs = common.join("refs");
+ let mut path = common.join(reference);
+ loop {
+ if path.exists() {
+ println!("cargo:rerun-if-changed={}", path.display());
+ return;
+ }
+ match path.parent() {
+ Some(parent) if parent.starts_with(&refs) => path = parent.to_path_buf(),
+ _ => return,
+ }
+ }
+}
+
fn git(args: &[&str]) -> Option<String> {
let dir = std::env::var("CARGO_MANIFEST_DIR").ok()?;
let out = std::process::Command::new("git")
diff --git a/libs/cua-spacesd/crates/cua-spacesd/build.rs b/libs/cua-spacesd/crates/cua-spacesd/build.rs
index d21c09f509..f975c16d2c 100644
--- a/libs/cua-spacesd/crates/cua-spacesd/build.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd/build.rs
@@ -10,6 +10,33 @@
use std::path::PathBuf;
use std::process::Command;
+fn rerun_if_exists(path: &std::path::Path) {
+ if path.exists() {
+ println!("cargo:rerun-if-changed={}", path.display());
+ }
+}
+
+/// Watches the branch ref `reference` (for example `refs/heads/main`). While
+/// it is a loose file, watch that file. When it is packed, git writes a new
+/// loose file at the next commit without touching HEAD, the reflog (it may be
+/// disabled) or packed-refs, so watch the nearest existing directory above it
+/// instead: creating the file changes that directory. Paths that do not exist
+/// are never watched (Cargo would treat them as always changed).
+fn rerun_if_ref_changes(common: &std::path::Path, reference: &str) {
+ let refs = common.join("refs");
+ let mut path = common.join(reference);
+ loop {
+ if path.exists() {
+ println!("cargo:rerun-if-changed={}", path.display());
+ return;
+ }
+ match path.parent() {
+ Some(parent) if parent.starts_with(&refs) => path = parent.to_path_buf(),
+ _ => return,
+ }
+ }
+}
+
fn git(args: &[&str]) -> Option<String> {
let dir = std::env::var("CARGO_MANIFEST_DIR").ok()?;
let out = Command::new("git")
@@ -47,25 +74,26 @@ fn main() {
} else {
git(&["rev-parse", "HEAD"]).unwrap_or_default()
};
+ // Only paths that exist are watched: Cargo treats a missing
+ // `rerun-if-changed` path as always changed, which rebuilt this crate on
+ // every cargo invocation of a CI checkout (it has no packed-refs file).
+ // The HEAD reflog is appended by every commit, checkout and reset, so it
+ // follows the checkout even when the branch ref is packed.
if let Some(git_dir) = git(&["rev-parse", "--absolute-git-dir"]) {
let git_dir = PathBuf::from(git_dir);
- println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
+ rerun_if_exists(&git_dir.join("HEAD"));
+ rerun_if_exists(&git_dir.join("logs").join("HEAD"));
if let Some(common) = git(&["rev-parse", "--git-common-dir"]) {
+ // git prints a relative path relative to the directory it ran in.
let common = PathBuf::from(common);
let common = if common.is_absolute() {
common
} else {
- git_dir.join(common)
+ PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default()).join(common)
};
- println!(
- "cargo:rerun-if-changed={}",
- common.join("packed-refs").display()
- );
+ rerun_if_exists(&common.join("packed-refs"));
if let Some(reference) = git(&["symbolic-ref", "-q", "HEAD"]) {
- println!(
- "cargo:rerun-if-changed={}",
- common.join(reference).display()
- );
+ rerun_if_ref_changes(&common, &reference);
}
}
}
diff --git a/libs/cua/crates/cua-media-protocol/build.rs b/libs/cua/crates/cua-media-protocol/build.rs
index ca5c75224b..94e6f07d08 100644
--- a/libs/cua/crates/cua-media-protocol/build.rs
+++ b/libs/cua/crates/cua-media-protocol/build.rs
@@ -1,28 +1,78 @@
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.
+use std::path::{Path, PathBuf};
use std::process::Command;
fn main() {
- println!("cargo:rerun-if-changed=../../.git/HEAD");
- if let Ok(head) = std::fs::read_to_string("../../.git/HEAD") {
- if let Some(reference) = head.trim().strip_prefix("ref: ") {
- println!("cargo:rerun-if-changed=../../.git/{reference}");
+ // The repository root is four levels up from this crate, not two: the old
+ // `../../.git/HEAD` path never existed, and Cargo treats a missing
+ // `rerun-if-changed` path as always changed, so this crate and the five
+ // that depend on it rebuilt on every cargo invocation. Ask git for the
+ // real directories and watch only paths that exist.
+ if let Some(git_dir) = git(&["rev-parse", "--absolute-git-dir"]) {
+ let git_dir = PathBuf::from(git_dir);
+ rerun_if_exists(&git_dir.join("HEAD"));
+ rerun_if_exists(&git_dir.join("logs").join("HEAD"));
+ if let Some(common) = git(&["rev-parse", "--git-common-dir"]) {
+ let common = PathBuf::from(common);
+ let common = if common.is_absolute() {
+ common
+ } else {
+ PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default()).join(common)
+ };
+ rerun_if_exists(&common.join("packed-refs"));
+ if let Some(reference) = git(&["symbolic-ref", "-q", "HEAD"]) {
+ rerun_if_ref_changes(&common, &reference);
+ }
}
}
println!("cargo:rerun-if-env-changed=CUA_ENV_BUILD_REVISION");
if std::env::var_os("CUA_ENV_BUILD_REVISION").is_some() {
return;
}
- let revision = Command::new("git")
- .args(["rev-parse", "--short=12", "HEAD"])
- .output()
- .ok()
- .filter(|output| output.status.success())
- .and_then(|output| String::from_utf8(output.stdout).ok())
- .map(|revision| revision.trim().to_owned())
- .filter(|revision| !revision.is_empty());
+ let revision = git(&["rev-parse", "--short=12", "HEAD"]);
if let Some(revision) = revision {
println!("cargo:rustc-env=CUA_ENV_BUILD_REVISION={revision}");
}
}
+
+fn rerun_if_exists(path: &Path) {
+ if path.exists() {
+ println!("cargo:rerun-if-changed={}", path.display());
+ }
+}
+
+/// Watches the branch ref `reference` (for example `refs/heads/main`). While
+/// it is a loose file, watch that file. When it is packed, git writes a new
+/// loose file at the next commit without touching HEAD, the reflog (it may be
+/// disabled) or packed-refs, so watch the nearest existing directory above it
+/// instead: creating the file changes that directory. Paths that do not exist
+/// are never watched (Cargo would treat them as always changed).
+fn rerun_if_ref_changes(common: &Path, reference: &str) {
+ let refs = common.join("refs");
+ let mut path = common.join(reference);
+ loop {
+ if path.exists() {
+ println!("cargo:rerun-if-changed={}", path.display());
+ return;
+ }
+ match path.parent() {
+ Some(parent) if parent.starts_with(&refs) => path = parent.to_path_buf(),
+ _ => return,
+ }
+ }
+}
+
+/// Runs git in the crate directory; `None` when it is missing or fails.
+fn git(args: &[&str]) -> Option<String> {
+ let dir = std::env::var("CARGO_MANIFEST_DIR").ok()?;
+ let output = Command::new("git")
+ .args(args)
+ .current_dir(dir)
+ .output()
+ .ok()
+ .filter(|output| output.status.success())?;
+ let text = String::from_utf8(output.stdout).ok()?.trim().to_owned();
+ (!text.is_empty()).then_some(text)
+}