rust-cargo-cli
v1.0.2
Published
Rust toolchain as npm CLI package — automatically downloads rustup and installs cargo, rustc, rustup, rustfmt and rustdoc for the current platform
Maintainers
Readme
rust-cargo-cli
An npm CLI package that automatically installs the Rust toolchain for your current platform and exposes it as cargo, rustc, rustup, rustfmt, rustdoc, cargo-clippy and cargo-fmt terminal commands.
Under the hood it downloads the official rustup installer from static.rust-lang.org and runs it in non-interactive mode, keeping the whole toolchain inside the package — no system-wide changes, no shell profile edits.
Supported platforms
| OS | Arch | rustup-init target | Default toolchain host |
|---------|-------|---------------------------------------|-------------------------------|
| Linux | x64 | x86_64-unknown-linux-gnu / -musl | same as rustup-init target |
| Linux | arm64 | aarch64-unknown-linux-gnu / -musl | same as rustup-init target |
| macOS | x64 | x86_64-apple-darwin | same as rustup-init target |
| macOS | arm64 | aarch64-apple-darwin | same as rustup-init target |
| Windows | x64 | x86_64-pc-windows-msvc | x86_64-pc-windows-gnu |
| Windows | arm64 | aarch64-pc-windows-msvc | aarch64-pc-windows-msvc |
On Windows x64 the x86_64-pc-windows-gnu toolchain is installed by default: it ships its own linker (rust-mingw), so Visual Studio Build Tools are not required. Set RUST_CARGO_CLI_WINDOWS_HOST=msvc if you prefer the MSVC target.
Toolchain versions are resolved from the official Rust distribution channel: https://static.rust-lang.org/dist/channel-rust-<toolchain>.toml.
Installation
Global (recommended)
npm i -g --allow-scripts=rust-cargo-cli rust-cargo-cli@latestnpm v11+: npm blocks install-time lifecycle scripts (
preinstall/install/postinstall) of registry packages by default, so they must be allowed explicitly.--allow-scripts=rust-cargo-cliallows this package'spostinstallscript to run and install the Rust toolchain into the package'sdist/folder. The flag is ignored on npm < 11, so the command works there as well.
Verify that the toolchain is installed and working:
cargo --version && rustc --versionLocal (per-project)
npm install rust-cargo-cli
npx cargo --versionFor local installs, npm v11+ also skips the postinstall script by default. Add an allowScripts entry to your project's package.json and reinstall:
{
"allowScripts": {
"rust-cargo-cli": true
}
}Alternatively, run node node_modules/rust-cargo-cli/install.js once after npm install.
Dry run
See what would be downloaded and executed, without touching the network or the disk:
node install.js --dry-run # or: npm run dry-runIDEs (rust-analyzer, CLion, …) on Windows
Terminal shims (cargo, cargo.cmd, cargo.ps1) are enough for cmd, PowerShell and Git Bash, but IDE tooling spawns cargo / rustc directly through CreateProcess, which only understands real executables — the npm shims are shell scripts, so those tools fail with %1 is not a valid Win32 application (os error 193) unless rust-analyzer.server.extraEnv is configured by hand.
For global installs on Windows the installer wires the toolchain into your user environment automatically (HKCU\Environment, no admin rights required):
<package>\dist\cargo\bin(the realcargo.exe,rustc.exe,rustup.exe, … rustup proxies) is prepended to the userPATH;CARGO_HOMEandRUSTUP_HOMEare pointed at the bundleddist/cargoanddist/rustup.
Restart VS Code — all windows, not just a window reload — and any open terminals, so they pick up the new environment: VS Code snapshots the environment when the app starts and passes it on to rust-analyzer, so reloading a window keeps the old snapshot.
Manage the integration explicitly:
rust-cargo-cli doctor # diagnose the install + what IDE tools will see
rust-cargo-cli setup # (re-)apply the user-environment integration
rust-cargo-cli setup --force # …and override a pre-existing Rust toolchain
rust-cargo-cli setup --dry-run # show what would change
rust-cargo-cli unsetup # remove the PATH entry / CARGO_HOME / RUSTUP_HOME again
rust-cargo-cli env # print the `rust-analyzer.server.extraEnv` snippetIf another Rust toolchain is already present (its cargo.exe is on PATH, or CARGO_HOME / RUSTUP_HOME are set), the integration is skipped so your setup is never hijacked — use --force to override it, or set RUST_CARGO_CLI_NO_ENV=1 to disable the integration entirely.
Local installs do not touch your environment — enable it once with npx rust-cargo-cli setup, or paste the rust-cargo-cli env snippet into your editor settings:
{
"rust-analyzer.server.extraEnv": {
"CARGO_HOME": "…\\node_modules\\rust-cargo-cli\\dist\\cargo",
"RUSTUP_HOME": "…\\node_modules\\rust-cargo-cli\\dist\\rustup",
"PATH": "…\\node_modules\\rust-cargo-cli\\dist\\cargo\\bin;${env:PATH}"
}
}Usage
After a global install:
# Check versions
cargo --version
rustc --version
rustup --version
# Create and run a project
cargo new hello && cd hello
cargo build
cargo run
cargo test
# Formatting and lints (components of the default profile)
cargo fmt
cargo clippy
# Toolchain management
rustup update
rustup default nightly
rustup component add rust-srcEnvironment variables
| Variable | Default | Meaning |
|--------------------------------|--------------------|---------------------------------------------------------------------------------------------|
| RUST_CARGO_CLI_HOME | <package>/dist | Base folder holding cargo/ (CARGO_HOME), rustup/ (RUSTUP_HOME) and rustup-init. |
| RUST_CARGO_CLI_TOOLCHAIN | stable | Toolchain passed to rustup-init --default-toolchain (e.g. stable, nightly, 1.75.0). |
| RUST_CARGO_CLI_PROFILE | default | rustup profile: minimal, default or complete. minimal is much smaller. |
| RUST_CARGO_CLI_WINDOWS_HOST | gnu | Windows x64 toolchain flavour: gnu (no Visual Studio needed) or msvc. |
| RUST_CARGO_CLI_SKIP | — | Set to 1 to skip the download entirely (useful in containers / CI). |
| RUST_CARGO_CLI_NO_ENV | — | Set to 1 to disable the Windows user-environment integration (see the IDE section above). |
| RUST_CARGO_CLI_SETUP_ENV | — | Set to 1 to force the integration from install.js for non-global installs. |
| RUST_CARGO_CLI_DRY_RUN | — | Set to 1 for a dry run (same as --dry-run). |
| HTTPS_PROXY / HTTP_PROXY | — | Passed through to rustup for downloads behind a proxy. |
When RUST_CARGO_CLI_HOME points outside the package, the chosen path is remembered in a .rust-cargo-cli-home marker file, so the cargo/rustc shims keep using it.
Project structure
rust-cargo-cli/
├── bin/
│ ├── cargo.js # `cargo` CLI wrapper
│ ├── cargo-clippy.js # `cargo clippy` backend
│ ├── cargo-fmt.js # `cargo fmt` backend
│ ├── rust-cargo-cli.js # management CLI (setup / unsetup / doctor / env)
│ ├── rustc.js # `rustc` CLI wrapper
│ ├── rustdoc.js # `rustdoc` CLI wrapper
│ ├── rustfmt.js # `rustfmt` CLI wrapper
│ └── rustup.js # `rustup` CLI wrapper
├── dist/ # Installed toolchain (created at install time)
│ ├── rustup-init[.exe]
│ ├── cargo/ # CARGO_HOME (bin/rustc, bin/cargo, registry cache…)
│ └── rustup/ # RUSTUP_HOME (toolchains/…)
├── lib/
│ ├── cli.js # implementation of the `rust-cargo-cli` management CLI
│ ├── download.js # HTTPS download with redirects, retries and progress
│ ├── env-setup.js # Windows user-environment integration (PATH / CARGO_HOME / RUSTUP_HOME)
│ ├── manifest.js # channel-rust-*.toml helpers (static.rust-lang.org)
│ ├── platform.js # Platform detection + target triples + directories
│ ├── run.js # Shared "spawn the toolchain binary" logic for all bins
│ └── scripts/ # PowerShell helpers used by env-setup.js
│ ├── read-env.ps1 # reads HKCU/HKLM environment (raw values + registry kinds)
│ └── write-env.ps1 # writes HKCU\Environment and broadcasts WM_SETTINGCHANGE
├── install.js # postinstall script
└── package.jsonHow it works
npm installtriggers thepostinstallscript (install.js).The script detects the OS, CPU architecture and (on Linux) glibc vs musl.
It reads the official channel manifest (
channel-rust-stable.toml) to report which toolchain version is about to be installed and to verify that the target is published for it.rustup-initis downloaded from the official rustup distribution (https://static.rust-lang.org/rustup/dist/<target>/rustup-init) intodist/.rustup-initruns non-interactively:rustup-init -y --no-modify-path --default-host <host> --default-toolchain stable --profile defaultwith
CARGO_HOME=<package>/dist/cargoandRUSTUP_HOME=<package>/dist/rustup.Every
bin/*.jsentry point is a thin wrapper: it locates the toolchain binary insideCARGO_HOME/bin, passesCARGO_HOME/RUSTUP_HOME/PATHthrough and delegates all CLI arguments viachild_process.spawnSync(exit codes and signals are forwarded as-is).On Windows the installer also prepends
dist\cargo\binto the userPATHand setsCARGO_HOME/RUSTUP_HOMEinHKCU\Environment(global installs only), so IDE tools such as rust-analyzer can spawn the realcargo.exe/rustc.exe.rust-cargo-cli doctorreports the current state,rust-cargo-cli unsetupreverts it.
Linking note — rustc still needs a system linker when a crate compiles C code or links a binary:
- Windows (gnu host): the self-contained
rust-mingwlinker is used, nothing else is needed. - Windows (msvc host): Visual Studio Build Tools must be installed.
- macOS: Xcode Command Line Tools (
xcode-select --install). - Linux:
gcc/build-essential.
Updating Rust
The toolchain manages itself — just use rustup:
rustup update # update all installed toolchains
rustup toolchain install beta # add another toolchain
rustup self update # update rustup itselfTo reinstall from scratch (e.g. after changing RUST_CARGO_CLI_TOOLCHAIN or the Windows host flavour), remove the toolchain directory and re-run the installer:
rm -rf dist # PowerShell: Remove-Item -Recurse -Force dist
node install.jsTroubleshooting
- Windows: rust-analyzer fails with
os error 193or “cannot find cargo” — the toolchain is not on the userPATHthat VS Code sees. Runrust-cargo-cli doctor, thenrust-cargo-cli setupand fully quit VS Code (all windows — a window reload keeps the old environment snapshot); a globalnpm installperforms this automatically. cargo is not installed— thepostinstallscript did not run (npm v11+ skips lifecycle scripts by default). Runnode node_modules/rust-cargo-cli/install.js, or reinstall globally withnpm i -g --allow-scripts=rust-cargo-cli rust-cargo-cli@latest.- Global install done with
sudo— the toolchain landed in a root-owned folder, socargocannot write its registry cache. Set a writable home first:RUST_CARGO_CLI_HOME=$HOME/.rust-cargo-cli npm i -g .... rustup-initfails behind a proxy — exportHTTPS_PROXY/HTTP_PROXYbefore installing.- Windows:
link.exe not found— you are on themsvchost without Visual Studio Build Tools. Reinstall withRUST_CARGO_CLI_WINDOWS_HOST=gnu(the default for x64). - Big download size — a full
defaultprofile takes roughly 1.5–2 GB on disk (measured: ~1.7 GB forstableon Windows x64). UseRUST_CARGO_CLI_PROFILE=minimalfor a much smaller toolchain (rustc + cargo + rust-std only).
License
MIT
