Files
app__bitwarden-clients/scripts
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
..