A collection of easy-to-use and extensible commands to be used in your xtask CLI based on clap.
We rely on these commands in each of our Tracel repositories. By centralizing our redundant commands we save a big amount of code duplication, boilerplate and considerably lower their maintenance cost. This also provides a unified interface across all of our repositories.
These commands are not specific to Tracel repositories and they should be pretty much usable in any Rust repositories with a cargo workspace as well as other repositories where Rust is not necessarily the only language. The commands can be easily extended using handy proc macros and by following some patterns described in this README.
Getting Started
Setting Up a Cargo Workspace with an xtask binary crate
- Create a new Cargo workspace:
- Create the
xtaskbinary crate:
- Configure the workspace:
Edit the Cargo.toml file in the root of the workspace to include the following:
[]
= ["xtask"]
- Add the
tracel-xtaskdependency:
In the xtask/Cargo.toml file, add the following under [dependencies]:
[]
= "~1.0"
- Build the workspace:
Your workspace is now set up with a xtask binary crate that depends on tracel-xtask version 1.0.x.
Bootstrap main.rs
- In the
main.rsfile of the newly createdxtaskcrate, import thetracel_xtaskprelude moduel and then declare aCommandenum. Select the base commands you want to use by adding themacros::base_commandsattribute:
use *;
- Update the
mainfunction to initialize the xtask parser and dispatch the base commands:
-
Build the workspace with
cargo buildat the root of the repository to verify that everything is. -
You should now be able to display the main help screen which lists the commands you selected previously:
Setup aliases for easy invocation
Cargo Alias
Invoking the xtask binary with cargo is very verbose and not really usable as is. Happily we can create a cargo alias to make it really effortless to invoke it.
Create a new file .cargo/config.toml in your repository with the following contents:
[]
= "run --target-dir target/xtask --package xtask --bin xtask --"
This saves quite a few characters to type as you can now invoke xtask direclty like this:
Try it with cargo xtask --help.
Shell Alias
We can save even more typing by creating a shell alias for cargo xtask.
For instance we can set the alias to cx. Here is how to do it in various shells.
- For bash:
# add this to the file
# save and source the file or restart the shell session
- For zsh:
# add this to the file
# save and source the file or restart the shell session
- For fish:
nano ~/.config/fish/config.fish
# add this to the file
alias cx='cargo xtask'
# save and source the file or restart the shell session
source ~/.config/fish/config.fish
- For powershell:
notepad $PROFILE
# add this at the end of file
function cx {
cargo xtask $args
}
# save and quit then open a new powershell terminal
Try it with cx --help at the root of the repository.
Conventions
Repository structure
All our repositories follow the same directory hierarchy:
- a
cratesdirectory which contains all the crates of the workspace - an
examplesdirectory which holds all the examples crates - a
xtaskdirectory which is the binary crate for our xtask CLI usingtracel-xtask
About tests
As per Cargo convention, Integration tests are tests contained in a tests directory of a crate besides its src directory.
Inline tests in src directory are called Unit tests.
tracel-xtask allow to easily execute them separately using the test command.
Interface generalities
Target
There are 4 default targets provided by tracel-xtask:
workspacewhich targets the cargo workspace, this is the default targetcratesare all the binary crates and library cratesexamplesare all the example cratesall-packagesare bothcratesandexamplestargets
workspace and all-packages are different because workspace uses the --workspace flag of cargo whereas all-packages
relies on crates and examples targets which use the --package flag. So all-packages executes a command for each crate
or example individually.
Here are some examples:
# run all the crates tests
# check format for examples, binaries and libs
# build the workspace
# workspace is the default target so this has the same effect
Global options
The following options are global and precede the actual command on the command line:
- Environment (
-e,--environment):
-e or --environment does not do anything per se in the base commands, it is a flag whose only goal is
to inform your custom commands or dispatch functions about the targeted environment which can be development (default),
staging or production.
- Execution environment (
-E,--execution-environment):
-E or --execution-environment does not do anything per se in the base commands, it is a flag whose only goal is
to inform your custom commands or dispatch functions about the targeted execution environment which can be std or
no-std.
- Coverage (
-c,--enable-coverage):
-c or --enabled-coverage setups the Rust toolchain to generate coverage information.
Anatomy of a base command
We use the derive API of clap which is based on structs, enums and attribute proc macros. Each base command is a
submodule of the base_commands module. If the command accepts arguments there is a corresponding struct named <command>CmdArgs
which declare the options, arguments and subcommands. In the case of subcommands a corresponding enum named <command>SubCommand
is defined.
Here is an example with a foo command:
Note that it is possible to have an arbitrary level of nested subcommands but deeper nested subcommands cannot be extended, in other words, only the first level of subcommands can be extended. If possible, try to design commands with only one level of subcommands to keep the interface simple.
In the following sections we will see how to create completely new commands as well how to extend existing base commands.
Customization
Create a new command
-
First, we organize commands by creating a
commandsmodule. Create a filextask/src/commands/mycommand.rsas well as the correspondingmod.rsfile to declare the module contents. -
Then, in
mycommand.rsdefine the arguments struct with thedeclare_command_argsmacro and define thehandle_commandfunction. Thedeclare_command_argsmacro takes two parameters, the first is the type of the target enum and the second is the type of the subcommand enum if any. If the command has no target or no subcommand then putNonefor each argument respectively.Targetis the default target type provided bytracel-xtask. This type can be extended to support more targets as we will see in a later section.
use *;
- Make sure to update the
mod.rsfile to declare the command module:
pub(crate) mod my_command;
- We can now add a new variant to the
Commandenum inmain.rs:
use *;
- And dispatch its handling to our new command module:
- You can now test your new command with:
Extend the default Target enum
Let's implement a new command called extended-target to illustrate how to extend the default Target enum.
-
Create a
commands/extended_target.rsfile and update themod.rsfile as we saw in the previous section. -
We also need to add a new
strumdependency to ourCargo.tomlfile:
[]
= { = "0.26.3", = ["derive"]}
- Then we can extend the
Targetenum with themacros::extend_targetsattribute in ourextended_target.rsfile. Here we choose to add a new target calledfrontendwhich targets the frontend component we could find for instance in a monorepo:
use *;
- Then we define our command arguments by referencing our newly created
MyTargetenum in thedeclare_command_argsattribute:
- Our new target is then available for use in the
handle_commandfunction:
- Register our new command the usual way by adding it to our
Commandenum and dispatch it in themainfunction:
use *;
- Test the command with:
cargo xtask extended-target --help
cargo xtask extended-target --target frontend
Extend a base command
To extend an existing command we use the macros::extend_command_args attribute which takes three parameters:
- first argument is the type of the base command arguments struct to extend,
- second argument is the target type (or
Noneif there is no target), - third argument is the subcommand type (or
Noneif there is no subcommand).
Let's use two examples to illustrate this, the first is a command to extend the build base command with
a new --debug argument; and the second is a new command to extend the subcommands of the check base command
to add a new my-check subcommand.
Note that you can find more examples in the xtask crate of this repository.
Extend the arguments of a base command
We create a new command called extended-build-args which will have an additional argument called --debug.
-
Create the
commands/extended_build_args.rsfile and update themod.rsfile as we saw in the previous section. -
Extend the
BuildCommandArgsstruct using the attributemacros::extend_command_argsand define thehandle_commandfunction. Note that the macro automatically implements theTryIntotrait which makes it easy to dispatch back to the base command ownhandle_commandfunction. Also note that if the base command requires a target then you need to provide a target as well in your extension, i.e. the target parameter of the macro cannot beNoneif the base command has aTarget.
use *;
- Register the new command the usual way by adding it to the
Commandenum and dispatch it in themainfunction:
use *;
- Test the command with:
cargo xtask extended-build-args --help
cargo xtask extended-build-args --debug
Extend the subcommands of a base command
For this one we create a new command called extended-check-subcommands which will have an additional subcommand.
-
Create a
commands/extended_check_subcommands.rsfile and update themod.rsfile as we saw in the previous section. -
Extend the
CheckCommandArgsstruct using the attributemacros::extend_command_args:
use *;
- Implement the
ExtendedCheckSubcommandenum by extending theCheckSubcommandbase enum with the macroextend_subcommands. It takes the name of the type of the subcommand enum to extend:
- Implement the
handle_commandfunction to handle the new subcommand. Note that we must handle theAllsubcommand as well:
use IntoEnumIterator;
- Register the new command the usual way by adding it to the
Commandenum and dispatch it in themainfunction:
use *;
- Test the command with:
cargo xtask extended-check-subcommands --help
cargo xtask extended-check-subcommands my-check
Custom builds and tests
tracel-xtask provides helper functions to easily execute custom builds or tests with specific features or build targets (do not confuse
Rust build targets which is an argument of the cargo build command with the xtask target we introduced previously).
For instance we can extend the build command to build additional crates with custom features or build targets using the helper function:
Enable and generate coverage information
Here is a example GitHub job which shows how to setup coverage, enable it and upload coverage information to codecov:
env:
GRCOV_LINK: "https://github.com/mozilla/grcov/releases/download"
GRCOV_VERSION: "0.8.19"
jobs:
my-job:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: install rust
uses: dtolnay/rust-toolchain@master
with:
components: rustfmt, clippy
toolchain: stable
- name: Install grcov
shell: bash
run: |
curl -L "$GRCOV_LINK/v$GRCOV_VERSION/grcov-x86_64-unknown-linux-musl.tar.bz2" |
tar xj -C $HOME/.cargo/bin
cargo xtask coverage install
- name: Build
shell: bash
run: cargo xtask build
- name: Tests
shell: bash
run: cargo xtask --enable-coverage test all
- name: Generate lcov.info
shell: bash
# /* is to exclude std library code coverage from analysis
run: cargo xtask coverage generate --ignore "/*,xtask/*,examples/*"
- name: Codecov upload lcov.info
uses: codecov/codecov-action@v4
with:
files: lcov.info
token: ${{ secrets.CODECOV_TOKEN }}
Special command 'validate'
By convention this command is responsible to run all the checks, builds, and/or tests that validate the code before opening a pull request or merge request.
The command Validate can been added via the macro tracel_xtask_macros::commands like the other commands.
By default all the checks from the check command are run as well as both unit and integration tests from
the test command.
You can make your own handle_command function if you need to perform more validations. Ideally this function
should only call the other commands handle_command functions.
For quick reference here is a simple example to perform all checks and tests against the workspace:
Base commands list
Check and Fix
The check and fix commands are designed to help you maintain code quality during development.
They run various checks and fix issues, ensuring that your code is clean and follows best practices.
check and fix contains the same subcommands to audit, format, lint or proofread a code base.
While the check command only reports issues, the fix command attempts to fix them as they are encountered.
Each check can be executed separately or all of them can be executed sequentially using all.
Usage to lint the code base:
Running Tests
Testing is a crucial part of development, and the test command is designed to make this process easy.
This command makes the distinction between unit tests and integrations tests. Unit tests are inline tests under the
src directory of a crate. Integration tests are tests defined in files under the tests directory of a crate besides
the src directory.
Usage:
# execute workspace unit tests
# execute workspace integration tests
# execute workspace both unit tests and integration tests
Note that documentation tests are supported by the doc command.
Documentation
Command to build and test the documentation in a workspace.
Bumping Versions
This is a command reserved for repository maintainers.
The bump command is used to update the version numbers of all first-party crates in the repository.
This is particularly useful when you're preparing for a new release and need to ensure that all crates have the correct version.
You can bump the version by major, minor, or patch levels, depending on the changes made. For example, if you’ve made breaking changes, you should bump the major version. For new features that are backwards compatible, bump the minor version. For bug fixes, bump the patch version.
Usage:
Publishing Crates
This is a command reserved for repository maintainers and is tipically used in publish GitHub workflows.
This command automates the process of publishing crates to crates.io, the Rust package registry.
By specifying the name of the crate, xtask handles the publication process, ensuring that the crate is available for others to use.
Usage:
As mentioned, this command is often used in a GitHub workflow. We provide a Tracel's reusable publish-crate workflow that makes use of this command. Here is a simple example with a workflow that publishes two crates A and B with A depending on B.
name: publish all crates
on:
push:
tags:
- "v*"
jobs:
publish-B:
uses: tracel-ai/github-actions/.github/workflows/publish-crate.yml@v1
with:
crate: B
secrets:
CRATES_IO_API_TOKEN: ${{ secrets.CRATES_IO_API_TOKEN }}
# --------------------------------------------------------------------------------
publish-A:
uses: tracel-ai/github-actions/.github/workflows/publish-crate.yml@v1
with:
crate: A
needs:
- publish-B
secrets:
CRATES_IO_API_TOKEN: ${{ secrets.CRATES_IO_API_TOKEN }}
Coverage
This command provide a subcommand to install the necessary dependencies for performing code coverage and a subcommand to generate the
coverage info file that can then be uploaded to a service provider like codecov. See dedicated section Enable and generate coverage information.
Dependencies
Various additional subcommands about dependencies.
deny make sure that all dependencies meet requirements using cargo-deny.
unused detects dependencies in the workspace that are not in ussed.
Vulnerabilities
This command makes it easier to execute sanitizers as described in the Rust unstable book.
These sanitizers require a nightly toolchain.
Run the specified vulnerability check locally. These commands must be called with 'cargo +nightly'
Usage: xtask vulnerabilities <COMMAND>
Commands:
all Run all most useful vulnerability checks
address-sanitizer Run Address sanitizer (memory error detector)
control-flow-integrity Run LLVM Control Flow Integrity (CFI) (provides forward-edge control flow protection)
hw-address-sanitizer Run newer variant of Address sanitizer (memory error detector similar to AddressSanitizer, but based on partial hardware assistance)
kernel-control-flow-integrity Run Kernel LLVM Control Flow Integrity (KCFI) (provides forward-edge control flow protection for operating systems kerneljs)
leak-sanitizer Run Leak sanitizer (run-time memory leak detector)
memory-sanitizer Run memory sanitizer (detector of uninitialized reads)
mem-tag-sanitizer Run another address sanitizer (like AddressSanitizer and HardwareAddressSanitizer but with lower overhead suitable for use as hardening for production binaries)
nightly-checks Run nightly-only checks through cargo-careful `<https://crates.io/crates/cargo-careful>`
safe-stack Run SafeStack check (provides backward-edge control flow protection by separating stack into safe and unsafe regions)
shadow-call-stack Run ShadowCall check (provides backward-edge control flow protection - aarch64 only)
thread-sanitizer Run Thread sanitizer (data race detector)
help Print this message or the help of the given subcommand(s)