Files
app__bitwarden-clients/scripts/prepare-env-rust.mjs
Andreas Coroiu b92de117da [PM-38767] Adopt cargo-run-bin for binary tool version pinning in desktop_native (#21169)
* [PM-38767] Adopt cargo-run-bin for binary tool version pinning in desktop_native

Align desktop_native's Rust tooling with bitwarden/sdk-internal.

Pin the desktop_native workspace's binary cargo tools (cargo-deny, cargo-sort,
cargo-udeps, cargo-llvm-cov, cargo-xwin) in [workspace.metadata.bin] and run
them via cargo-run-bin (`cargo bin <tool>`), bootstrapped once with
`cargo install cargo-run-bin --locked`. Replaces the cargo-tool-versions file
and the ad-hoc per-tool `cargo install` calls in CI and build.js. A
check-no-binstall guard enforces source-only installs (VULN-613).

Includes the PM-24643 improvement: a unified lint runner (--fix / --only) that
mirrors CI, exposed as `npm run lint:rust`. The lint.yml rust job is now a
per-check x OS matrix with per-check .bin caching and a stable `Rust lint`
aggregator job.

Pin the nightly toolchain the same way the SDK does, via a non-standard
`nightly-channel` key in rust-toolchain.toml. The shared Renovate preset this
repo already extends (github>bitwarden/renovate-config) matches that key in any
rust-toolchain.toml and updates it from a custom rust-nightly datasource, so no
renovate.json5 change is needed. This closes a real gap rather than a cosmetic
one: rustfmt.toml enables unstable options (wrap_comments, group_imports,
imports_granularity) whose output changes between nightlies, so a rolling local
nightly could format code that CI's pinned nightly then rejects. Both the stable
and nightly toolchains are now read from that one file by CI, prepare-env-rust,
lint-rust and the pre-commit hooks, so a Renovate bump can no longer leave a
stale literal behind in a workflow.

Dev/CI scripts are portable Node rather than the duplicated bash + PowerShell
pair, and rather than the SDK's bash equivalents: our Rust lint matrix includes
macos-14, so the SDK's `grep -oP` toolchain extraction and its Linux-only
component suffix can't be reused.
- scripts/prepare-env-rust.mjs   (was prepare-env-unix-rust.sh + -windows-rust.ps1)
- scripts/lint-rust.mjs          (the unified runner)
- scripts/check-no-binstall.mjs  (binstall guard)
- scripts/rust-toolchain.mjs     (reads channel / nightly-channel)

The cargo-deny (0.20.0) and cargo-xwin (0.23.0) bumps that landed on main while
this branch was open are carried into [workspace.metadata.bin].

NOTE: branch protection required-status-check names change with the matrix, from
`Run Rust lint on <os>` (3 checks) to `Rust <check> (<os>)` (15 checks); point
them at the new `Rust lint` aggregator job instead.

* [PM-38767] Keep cargo-xwin on PATH for the napi cross-build

cargoBuild() reaches cargo-xwin through `cargo bin`, but buildNapiModule() hands
off to the napi CLI, which spawns `cargo xwin` as a PATH cargo-subcommand we
can't redirect. Dropping the global `cargo install cargo-xwin` therefore broke
Windows cross-builds from macOS/Linux -- a path CI never exercises, since each
platform builds natively on its own runner.

Reinstate the global install, but read its version from [workspace.metadata.bin]
via a small pinnedBinVersion() helper, so the two call sites share one pin.

Also drop the cargo-xwin precheck in appx-cross-build.ps1: it carried its own
copy of 0.23.0 (covered by neither Renovate custom manager) and hard-errored
before `npm run build-native` could perform the install that satisfies it.

Fix a stale comment in Cargo.toml that pointed at the SDK's scripts/lint.sh
instead of this repo's scripts/lint-rust.mjs.

* [PM-38767] Fail the toolchain-resolve step when the reader errors

Match bitwarden/sdk-internal's shape for the resolve step. Its intermediate shell
variable is load-bearing, not stylistic: under `bash -e` an assignment adopts the
command substitution's exit status, so a failing reader fails the step. Inlining
`$(...)` into `echo` makes the status `tee`'s, which is always 0, so the step went
green while writing an empty toolchain -- dtolnay/rust-toolchain would then have
silently resolved something other than the pin, the exact drift this PR removes.

Also rename the step outputs to RUST_TOOLCHAIN / RUST_NIGHTLY_TOOLCHAIN to match
the SDK and the env var already consumed by lint-rust.mjs. The remaining
divergence, reading via Node instead of `grep -oP`, stays: this job also runs on
macos-14, whose BSD grep has no -P.

* [PM-38767] Correct the cargo-xwin subcommand-token rationale

The instruction was right but the reason was wrong. The `xwin` token is omitted
not because clap fails to strip it, but because cargo-run-bin supplies it: the
raw binary accepts both `cargo-xwin build` and `cargo-xwin xwin build`, while
`cargo bin cargo-xwin xwin build` becomes `xwin xwin` and fails with
"error: unrecognized subcommand 'xwin'".

Verified by cross-compiling for real on macOS with the exact form build.js uses:
  cargo bin cargo-xwin build --bin desktop_proxy --target=x86_64-pc-windows-msvc
produces target/x86_64-pc-windows-msvc/debug/desktop_proxy.exe, a PE32+ MS
Windows executable.

* [PM-38767] Address review: drop duplicate cargo-xwin build, last toolchain literal

cargoBuild() now dispatches `cargo xwin build ...` instead of `cargo bin
cargo-xwin build ...`. installTarget() already installs the version pinned in
[workspace.metadata.bin] globally, because the napi CLI spawns `cargo xwin`
itself and we can't redirect that, so the `cargo bin` route was building a second
copy of the same pinned version for no extra guarantee. It also removes a
security wrinkle: `cargo install --version --locked` is source-only, whereas
`cargo bin` falls through to cargo-binstall's prebuilt binaries when binstall is
on PATH (VULN-613).

test.yml no longer hardcodes `toolchain: 1.96.0`; it resolves from
rust-toolchain.toml like lint.yml, so that was the last place a stale literal
could survive a Renovate bump.

lint-rust.mjs now rejects `--only` with no value instead of silently running
every check.

Add root-level `lint:rust` / `lint:rust:fix` aliases: the runner lives in the
repo root's scripts/ and CI invokes it from there, so requiring a cd into
apps/desktop was surprising.

Verified by cross-compiling for real on macOS through the new dispatch form:
  cargo xwin build --bin desktop_proxy --target=x86_64-pc-windows-msvc
produces a PE32+ MS Windows executable.

* [PM-38767] Drop the now-unused cargo-run-bin bootstrap from build.js

Fallout from 11b9a8e090: once cargoBuild() switched from `cargo bin cargo-xwin`
to dispatching `cargo xwin`, nothing reachable from build.js invoked `cargo bin`
any more, so every Windows cross-build paid for a from-source
`cargo install cargo-run-bin` whose output was never used.

Remove it, and fold the "reached two different ways" comment back to one now that
both call sites reach cargo-xwin via PATH.

build.js consequently pins no cargo-run-bin version, so drop it from the Renovate
custom manager's fileMatch, along with the JS-array matchString whose only match
in the repo was that line (prepare-env-rust.mjs uses the CARGO_RUN_BIN_VERSION
const form, which has its own pattern). Verified the manager still matches
lint.yml, test.yml, prepare-env-rust.mjs and the devcontainer script, and no
longer matches build.js.

* [PM-38767] Address review: drop the comment about the absent cargo-xwin precheck

The neighbouring makemsix and osslsigncode prechecks exist because nothing
installs those tools for you; cargo-xwin needs no check because build-native
installs it. Documenting the absence of a check we never wrote is noise.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-11 13:50:25 +02:00

119 lines
4.8 KiB
JavaScript

#!/usr/bin/env node
// Prepares the environment for developing in Rust for the clients repo,
// specifically Desktop Native. Used by both developers locally and in CI, on
// every platform (replaces the former prepare-env-unix-rust.sh / -windows-rust.ps1).
//
// It installs the toolchains and bootstraps cargo-run-bin. The individual binary
// cargo tools (cargo-deny, cargo-sort, cargo-udeps, etc.) are pinned in
// apps/desktop/desktop_native/Cargo.toml under [workspace.metadata.bin] and built
// lazily on first use via `cargo bin <tool>` (see scripts/lint-rust.mjs).
import { execFileSync, spawnSync } from "node:child_process";
import { existsSync, unlinkSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { delimiter, dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { nightlyToolchain } from "./rust-toolchain.mjs";
// Pinned bootstrap version of cargo-run-bin. Renovate-managed; keep in sync with
// the CI workflows and build.js.
const CARGO_RUN_BIN_VERSION = "1.7.4";
const __dirname = dirname(fileURLToPath(import.meta.url));
const DESKTOP_NATIVE = resolve(__dirname, "../apps/desktop/desktop_native");
// Run a command inheriting stdio; throws on failure.
function run(cmd, args, opts = {}) {
execFileSync(cmd, args, { stdio: "inherit", ...opts });
}
// Capture stdout of a command; returns "" on failure.
function capture(cmd, args, opts = {}) {
const res = spawnSync(cmd, args, { encoding: "utf8", ...opts });
return res.status === 0 && res.stdout ? res.stdout.trim() : "";
}
function hasCommand(cmd) {
const probe = spawnSync(cmd, ["--version"], { stdio: "ignore" });
return !probe.error && probe.status === 0;
}
async function installRustup() {
if (hasCommand("rustup")) {
return;
}
console.log("Installing Rust...");
if (process.platform === "win32") {
const arch = process.env.PROCESSOR_ARCHITECTURE === "ARM64" ? "aarch64" : "x86_64";
const initPath = join(tmpdir(), "rustup-init.exe");
const res = await fetch(`https://win.rustup.rs/${arch}`);
if (!res.ok) {
throw new Error(`Failed to download rustup-init.exe: ${res.status} ${res.statusText}`);
}
writeFileSync(initPath, Buffer.from(await res.arrayBuffer()));
try {
run(initPath, ["-y", "--default-toolchain", "stable"]);
} finally {
unlinkSync(initPath);
}
} else {
// Pipe the rustup installer through the shell, matching the documented one-liner.
const installer =
"curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable";
const res = spawnSync(installer, { stdio: "inherit", shell: true });
if (res.status !== 0) {
throw new Error("rustup installation failed");
}
}
}
// Ensure the cargo bin directory is on PATH for this process (and its children).
function ensureCargoOnPath() {
const cargoBin = join(process.env.CARGO_HOME || join(homedir(), ".cargo"), "bin");
if (existsSync(cargoBin) && !(process.env.PATH || "").split(delimiter).includes(cargoBin)) {
process.env.PATH = `${cargoBin}${delimiter}${process.env.PATH || ""}`;
}
}
function installToolchains() {
// Determine the desired toolchain (from rust-toolchain.toml) and ensure it's installed.
let activeToolchain = capture("rustup", ["show", "active-toolchain"], { cwd: DESKTOP_NATIVE });
// Keep only the first token (strip trailing parenthetical info).
activeToolchain = activeToolchain.split(/\s+/)[0] || "";
if (!activeToolchain) {
// No active toolchain yet: fall back to env override or default to stable.
activeToolchain = process.env.RUSTUP_TOOLCHAIN || "stable";
run("rustup", ["default", activeToolchain]);
}
// For building desktop_native.
run("rustup", ["toolchain", "install", activeToolchain]);
// For the checks that need nightly (fmt, udeps) in pre-commit hooks and CI.
// Pinned in rust-toolchain.toml rather than rolling `nightly`: rustfmt.toml
// enables unstable options whose output changes between nightlies, so a rolling
// toolchain would format code that CI's pinned nightly rejects.
run("rustup", ["toolchain", "install", nightlyToolchain(), "-c", "rustfmt"]);
run("rustup", ["show"]);
console.log();
}
// Bootstraps cargo-run-bin (the `cargo bin` runner) at the pinned version. The
// binary tools it runs are pinned in [workspace.metadata.bin] and built lazily.
function installCargoRunBin() {
const version = capture("cargo", ["bin", "--version"]);
if (version === `cargo-run-bin ${CARGO_RUN_BIN_VERSION}`) {
console.log(`cargo-run-bin ${CARGO_RUN_BIN_VERSION} is already installed.`);
} else {
console.log(`cargo-run-bin ${CARGO_RUN_BIN_VERSION} is not installed. Installing ...`);
run("cargo", ["install", "cargo-run-bin", "--locked", "--version", CARGO_RUN_BIN_VERSION]);
}
}
await installRustup();
ensureCargoOnPath();
installToolchains();
installCargoRunBin();