Skip to content

Rust

The Cargo plugin lets Axle see the Rust crates in a repository, so they build, test and lint in the same run as everything else, in dependency order, from the cache when nothing changed. It’s written in Rust and runs with cargo, which every repository it serves already has.

Terminal window
axle add cargo

This writes it into the root axle.toml:

[plugins.cargo]
source = "npm:@axle-rs/plugin-cargo@0.1.0"
hash = "sha256-…"

It needs cargo on the PATH. The first time it starts, Cargo compiles it, which takes a few seconds; it uses only Rust’s standard library, so nothing is downloaded.

A repository with no JavaScript needs no package.json: the axle.toml at its root, the one that lists the plugin, is what Axle finds it by.

The plugin asks cargo metadata about every Cargo workspace in the repository, each Cargo.toml with a [workspace] table, and about every crate outside a workspace. Each member of a workspace is a project, named after its package, and axle list shows them with the kind plugin. Crates inside a workspace’s folder that aren’t its members, such as those it excludes, are left out, and so are files Git ignores, folders named target, node_modules and .git, a crate at the root of the repository and a folder that is already a package.

A crate depends on another crate the plugin found when it names it under [dependencies] or [build-dependencies] with a path, or through workspace = true, so its tasks wait for that crate’s. [dev-dependencies] don’t count as dependencies, since Cargo lets two crates use each other in their tests, but the tasks that compile tests still read those crates’ files.

The plugin reads the versions each crate uses from its workspace’s Cargo.lock, through every dependency of its dependencies, and Axle hashes them like a JavaScript lockfile. A new version of a crate from crates.io or Git misses the cache of the crates that use it and no others. Cargo.lock itself isn’t among the files every task reads, and --affected counts a change to it against every crate.

Each task reads its crate’s files, the files of the crates it depends on, the workspace’s Cargo.toml, and any .cargo/config.toml and rust-toolchain.toml from the crate’s folder up to the root. RUSTFLAGS, RUSTDOCFLAGS, RUSTUP_TOOLCHAIN and the CARGO_BUILD_* and CARGO_PROFILE_* variables change a task’s hash, and tasks also get CARGO_HOME, RUSTUP_HOME, CARGO_TARGET_DIR and Cargo’s network and registry settings.

Axle caches each task’s result and output, not target. Cargo’s target folder holds what it compiled for every crate of the workspace together, and Cargo already knows what in it is out of date, so a task Axle runs again is quick. A task from the cache doesn’t run Cargo at all.

The plugin’s answers are kept until a Cargo.toml, Cargo.lock or .gitignore changes.

Each crate gets three tasks:

Task Command
build cargo build -p <crate>
test cargo test -p <crate>
lint cargo clippy -p <crate>

tasks in the plugin’s settings adds tasks or changes them for every crate. A task is the Cargo command and its arguments, which the plugin runs with -p <crate> after the command’s name, a table with that command in run and any task settings, or false to leave it out:

[plugins.cargo]
source = "npm:@axle-rs/plugin-cargo@0.1.0"
hash = "sha256-…"
tasks = { lint = "clippy --all-targets -- -D warnings", doc = { run = "doc --no-deps", cache = false }, build = false }

A project’s own axle.toml and the root one change them as they would any task, and win over the plugin.

The tasks don’t run in batches. One cargo test -p a -p b would be quicker than two, but Cargo turns on the features both crates ask for when it builds them together, so a crate could pass in a batch and fail on its own, and Axle would cache the wrong result. Cargo lets one command at a time use a target folder, so Axle’s tasks of one workspace take turns, each using every core.