Crate Universe

Crate Universe is a set of Bazel rule for generating Rust targets using Cargo.

Experimental

crate_universe is experimental, and may have breaking API changes at any time. These instructions may also change without notice.

Setup

After loading rules_rust in your workspace, set the following to begin using crate_universe:

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

crate_universe_dependencies()

Note that if the current version of rules_rust is not a release artifact, you may need to set additional flags such as bootstrap = True on the crate_universe_dependencies call above or crates_repository::generator_urls in uses of crates_repository.

Rules

Utility Macros

Workflows

The crates_repository rule (the primary repository rule of rules_rust's cargo support) supports a number of different ways users can express and organize their dependencies. The most common are listed below though there are more to be found in the ./examples/crate_universe directory.

Cargo Workspaces

One of the simpler ways to wire up dependencies would be to first structure your project into a Cargo workspace. The crates_repository rule can ingest a root Cargo.toml file and generate dependencies from there.

load("@rules_rust//crate_universe:defs.bzl", "crates_repository")

crates_repository(
    name = "crate_index",
    lockfile = "//:Cargo.Bazel.lock",
    manifests = ["//:Cargo.toml"],
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

The generated crates_repository contains helper macros which make collecting dependencies for Bazel targets simpler. Notably, the all_crate_deps and aliases macros (see Dependencies API) commonly allow the Cargo.toml files to be the single source of truth for dependencies. Since these macros come from the generated repository, the dependencies and alias definitions they return will automatically update BUILD targets.

load("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

rust_library(
    name = "lib",
    aliases = aliases(),
    deps = all_crate_deps(
        normal = True,
    ),
    proc_macro_deps = all_crate_deps(
        proc_macro = True,
    ),
)

rust_test(
    name = "unit_test",
    crate = ":lib",
    aliases = aliases(
        normal_dev = True,
        proc_macro_dev = True,
    ),
    deps = all_crate_deps(
        normal_dev = True,
    ),
    proc_macro_deps = all_crate_deps(
        proc_macro_dev = True,
    ),
)

Direct Packages

In cases where Rust targets have heavy interractions with other Bazel targests (Cc, Proto, etc.), maintaining Cargo.toml files may have deminishing returns as things like rust-analyzer begin to be confused about missing targets or environment variables defined only in Bazel. In workspaces like this, it may be desirable to have a “Cargo free” setup. crates_repository supports this through the packages attribute.

load("@cargo_bazel//:defs.bzl", "crate", "crates_repository", "render_config")

crates_repository(
    name = "crate_index",
    lockfile = "//:Cargo.Bazel.lock",
    packages = {
        "async-trait": crate.spec(
            version = "0.1.51",
        ),
        "mockall": crate.spec(
            version = "0.10.2",
        ),
        "tokio": crate.spec(
            version = "1.12.0",
        ),
    },
    # Setting the default package name to `""` forces the use of the macros defined in this repository
    # to always use the root package when looking for dependencies or aliases. This should be considered
    # optional as the repository also exposes alises for easy access to all dependencies.
    render_config = render_config(
        default_package_name = ""
    ),
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

Consuming dependencies may be more ergonomic in this case through the aliases defined in the new repository.

load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

rust_library(
    name = "lib",
    deps = [
        "@crate_index//:tokio",
    ],
    proc_macro_deps = [
        "@crate_index//:async-trait",
    ],
)

rust_test(
    name = "unit_test",
    crate = ":lib",
    deps = [
        "@crate_index//:mockall",
    ],
)

Dependencies API

After rendering dependencies, convenience macros may also be generated to provide convenient accessors to larger sections of the dependency graph.



crates_repository

A rule for defining and downloading Rust dependencies (crates). This rule handles all the same workflows crate_universe rules do.

Environment Variables:

variableusage
CARGO_BAZEL_GENERATOR_SHA256The sha256 checksum of the file located at CARGO_BAZEL_GENERATOR_URL
CARGO_BAZEL_GENERATOR_URLThe URL of a cargo-bazel binary. This variable takes precedence over attributes and can use file:// for local paths
CARGO_BAZEL_ISOLATEDAn authorative flag as to whether or not the CARGO_HOME environment variable should be isolated from the host configuration
CARGO_BAZEL_REPINAn indicator that the dependencies represented by the rule should be regenerated. REPIN may also be used. See Repinning / Updating Dependencies for more details.

Example:

Given the following workspace structure:

[workspace]/
    WORKSPACE
    BUILD
    Cargo.toml
    Cargo.Bazel.lock
    src/
        main.rs

The following is something that'd be found in the WORKSPACE file:

load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate")

crates_repository(
    name = "crate_index",
    annotations = annotations = {
        "rand": [crate.annotation(
            default_features = False,
            features = ["small_rng"],
        )],
    },
    cargo_lockfile = "//:Cargo.Bazel.lock",
    lockfile = "//:cargo-bazel-lock.json",
    manifests = ["//:Cargo.toml"],
    # Should match the version represented by the currently registered `rust_toolchain`.
    rust_version = "1.60.0",
)

The above will create an external repository which contains aliases and macros for accessing Rust targets found in the dependency graph defined by the given manifests.

NOTE: The lockfile must be manually created. The rule unfortunately does not yet create it on its own. When initially setting up this rule, an empty file should be created and then populated by repinning dependencies.

Repinning / Updating Dependencies

Dependency syncing and updating is done in the repository rule which means it's done during the analysis phase of builds. As mentioned in the environments variable table above, the CARGO_BAZEL_REPIN (or REPIN) environment variables can be used to force the rule to update dependencies and potentially render a new lockfile. Given an instance of this repository rule named crate_index, the easiest way to repin dependencies is to run:

CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index

This will result in all dependencies being updated for a project. The CARGO_BAZEL_REPIN environment variable can also be used to customize how dependencies are updated. The following table shows translations from environment variable values to the equivilant cargo update command that is called behind the scenes to update dependencies.

ValueCargo command
Any of [true, 1, yes, on]cargo update
workspacecargo update --workspace
package_namecargo upgrade --package package_name
package_name@1.2.3cargo upgrade --package package_name --precise 1.2.3

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
annotationsExtra settings to apply to crates. See crate.annotation.Dictionary: String -> List of stringsoptional{}
cargo_configA Cargo configuration fileLabeloptionalNone
cargo_lockfileThe path used to store the crates_repository specific Cargo.lock file. If set, this file must exist within the workspace (but can be empty) before this rule will work.Labelrequired
generate_build_scriptsWhether or not to generate cargo build scripts by default.BooleanoptionalTrue
generatorThe absolute label of a generator. Eg. @cargo_bazel_bootstrap//:cargo-bazel. This is typically used when bootstrappingStringoptional""
generator_sha256sDictionary of host_triple -> sha256 for a cargo-bazel binary.Dictionary: String -> Stringoptional{}
generator_urlsURL template from which to download the cargo-bazel binary. {host_triple} and will be filled in according to the host platform.Dictionary: String -> Stringoptional{}
isolatedIf true, CARGO_HOME will be overwritten to a directory within the generated repository in order to prevent other uses of Cargo from impacting having any effect on the generated targets produced by this rule. For users who either have multiple crate_repository definitions in a WORKSPACE or rapidly re-pin dependencies, setting this to false may improve build times. This variable is also controled by CARGO_BAZEL_ISOLATED environment variable.BooleanoptionalTrue
lockfileThe path to a file to use for reproducible renderings.LabeloptionalNone
manifestsA list of Cargo manifests (Cargo.toml files).List of labelsoptional[]
packagesA set of crates (packages) specifications to depend on. See crate.spec.Dictionary: String -> Stringoptional{}
quietIf stdout and stderr should not be printed to the terminal.BooleanoptionalTrue
render_configThe configuration flags to use for rendering. Use //crate_universe:defs.bzl%render_config to generate the value for this field. If unset, the defaults defined there will be used.Stringoptional""
repo_mappingA dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry “@foo”: “@bar” declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).Dictionary: String -> Stringrequired
rust_toolchain_cargo_templateThe template to use for finding the host cargo binary. {version} (eg. ‘1.53.0’), {triple} (eg. ‘x86_64-unknown-linux-gnu’), {arch} (eg. ‘aarch64’), {vendor} (eg. ‘unknown’), {system} (eg. ‘darwin’), {cfg} (eg. ‘exec’), and {tool} (eg. ‘rustc.exe’) will be replaced in the string if present.Stringoptional“@rust_{system}_{arch}__{triple}_tools//:bin/{tool}”
rust_toolchain_rustc_templateThe template to use for finding the host rustc binary. {version} (eg. ‘1.53.0’), {triple} (eg. ‘x86_64-unknown-linux-gnu’), {arch} (eg. ‘aarch64’), {vendor} (eg. ‘unknown’), {system} (eg. ‘darwin’), {cfg} (eg. ‘exec’), and {tool} (eg. ‘cargo.exe’) will be replaced in the string if present.Stringoptional“@rust_{system}_{arch}__{triple}_tools//:bin/{tool}”
rust_versionThe version of Rust the currently registered toolchain is using. Eg. 1.56.0, or nightly-2021-09-08Stringoptional“1.62.0”
splicing_configThe configuration flags to use for splicing Cargo maniests. Use //crate_universe:defs.bzl%rsplicing_config to generate the value for this field. If unset, the defaults defined there will be used.Stringoptional""
supported_platform_triplesA set of all platform triples to consider when generating dependencies.List of stringsoptional[“i686-apple-darwin”, “i686-pc-windows-msvc”, “i686-unknown-linux-gnu”, “x86_64-apple-darwin”, “x86_64-pc-windows-msvc”, “x86_64-unknown-linux-gnu”, “aarch64-apple-darwin”, “aarch64-apple-ios”, “aarch64-apple-ios-sim”, “aarch64-linux-android”, “aarch64-unknown-linux-gnu”, “arm-unknown-linux-gnueabi”, “armv7-unknown-linux-gnueabi”, “armv7-linux-androideabi”, “i686-linux-android”, “i686-unknown-freebsd”, “powerpc-unknown-linux-gnu”, “s390x-unknown-linux-gnu”, “wasm32-unknown-unknown”, “wasm32-wasi”, “x86_64-apple-ios”, “x86_64-linux-android”, “x86_64-unknown-freebsd”, “riscv32imc-unknown-none-elf”]

crates_vendor

A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace. This rule is useful for users whose workspaces are expected to be consumed in other workspaces as the rendered BUILD files reduce the number of workspace dependencies, allowing for easier loads. This rule handles all the same workflows crate_universe rules do.

Example:

Given the following workspace structure:

[workspace]/
    WORKSPACE
    BUILD
    Cargo.toml
    3rdparty/
        BUILD
    src/
        main.rs

The following is something that'd be found in 3rdparty/BUILD:

load("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate")

crates_vendor(
    name = "crates_vendor",
    annotations = {
        "rand": [crate.annotation(
            default_features = False,
            features = ["small_rng"],
        )],
    },
    cargo_lockfile = "//:Cargo.Bazel.lock",
    manifests = ["//:Cargo.toml"],
    mode = "remote",
    vendor_path = "crates",
    tags = ["manual"],
)

The above creates a target that can be run to write BUILD files into the 3rdparty directory next to where the target is defined. To run it, simply call:

bazel run //3rdparty:crates_vendor

Repinning / Updating Dependencies

Repinning dependencies is controlled by both the CARGO_BAZEL_REPIN environment variable or the --repin flag to the crates_vendor binary. To update dependencies, simply add the flag ro your bazel run invocation.

bazel run //3rdparty:crates_vendor -- --repin

Under the hood, --repin will trigger a cargo update call against the generated workspace. The following table describes how to controll particular values passed to the cargo update command.

ValueCargo command
Any of [true, 1, yes, on]cargo update
workspacecargo update --workspace
package_namecargo upgrade --package package_name
package_name@1.2.3cargo upgrade --package package_name --precise 1.2.3

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
annotationsExtra settings to apply to crates. See crate.annotation.Dictionary: String -> List of stringsoptional{}
buildifierThe path to a buildifier binary used to format generated BUILD files.Labeloptional//crate_universe/private/vendor:buildifier
cargo_bazelThe cargo-bazel binary to use for vendoring. If this attribute is not set, then a CARGO_BAZEL_GENERATOR_PATH action env will be used.Labeloptional@cargo_bazel_bootstrap//:binary
cargo_configA Cargo configuration file.LabeloptionalNone
cargo_lockfileThe path to an existing Cargo.lock fileLabeloptionalNone
generate_build_scriptsWhether or not to generate cargo build scripts by default.BooleanoptionalTrue
manifestsA list of Cargo manifests (Cargo.toml files).List of labelsoptional[]
modeFlags determining how crates should be vendored. local is where crate source and BUILD files are written to the repository. remote is where only BUILD files are written and repository rules used to fetch source code.Stringoptional“remote”
packagesA set of crates (packages) specifications to depend on. See crate.spec.Dictionary: String -> Stringoptional{}
repository_nameThe name of the repository to generate for remote vendor modes. If unset, the label name will be usedStringoptional""
splicing_configThe configuration flags to use for splicing Cargo maniests. Use //crate_universe:defs.bzl%rsplicing_config to generate the value for this field. If unset, the defaults defined there will be used.Stringoptional""
supported_platform_triplesA set of all platform triples to consider when generating dependencies.List of stringsoptional[“i686-apple-darwin”, “i686-pc-windows-msvc”, “i686-unknown-linux-gnu”, “x86_64-apple-darwin”, “x86_64-pc-windows-msvc”, “x86_64-unknown-linux-gnu”, “aarch64-apple-darwin”, “aarch64-apple-ios”, “aarch64-apple-ios-sim”, “aarch64-linux-android”, “aarch64-unknown-linux-gnu”, “arm-unknown-linux-gnueabi”, “armv7-unknown-linux-gnueabi”, “armv7-linux-androideabi”, “i686-linux-android”, “i686-unknown-freebsd”, “powerpc-unknown-linux-gnu”, “s390x-unknown-linux-gnu”, “wasm32-unknown-unknown”, “wasm32-wasi”, “x86_64-apple-ios”, “x86_64-linux-android”, “x86_64-unknown-freebsd”, “riscv32imc-unknown-none-elf”]
vendor_pathThe path to a directory to write files into. Absolute paths will be treated as relative to the workspace rootStringoptional“crates”

aliases

Produces a map of Crate alias names to their original label

If no dependency kinds are specified, normal and proc_macro are used by default. Setting any one flag will otherwise determine the contents of the returned dict.

PARAMETERS

NameDescriptionDefault Value
normalIf True, normal dependencies are included in the output list.False
normal_devIf True, normla dev dependencies will be included in the output list..False
proc_macroIf True, proc_macro dependencies are included in the output list.False
proc_macro_devIf True, dev proc_macro dependencies are included in the output list.False
buildIf True, build dependencies are included in the output list.False
build_proc_macroIf True, build proc_macro dependencies are included in the output list.False
package_nameThe package name of the set of dependencies to look up. Defaults to native.package_name() when unset.None

RETURNS

dict: The aliases of all associated packages

all_crate_deps

Finds the fully qualified label of all requested direct crate dependencies for the package where this macro is called.

If no parameters are set, all normal dependencies are returned. Setting any one flag will otherwise impact the contents of the returned list.

PARAMETERS

NameDescriptionDefault Value
normalIf True, normal dependencies are included in the output list.False
normal_devIf True, normla dev dependencies will be included in the output list..False
proc_macroIf True, proc_macro dependencies are included in the output list.False
proc_macro_devIf True, dev proc_macro dependencies are included in the output list.False
buildIf True, build dependencies are included in the output list.False
build_proc_macroIf True, build proc_macro dependencies are included in the output list.False
package_nameThe package name of the set of dependencies to look up. Defaults to native.package_name() when unset.None

RETURNS

list: A list of labels to generated rust targets (str)

crate.spec

A constructor for a crate dependency.

See specifying dependencies in the Cargo book for more details.

PARAMETERS

NameDescriptionDefault Value
packageThe explicit name of the package (used when attempting to alias a crate).None
versionThe exact version of the crate. Cannot be used with git.None
default_featuresMaps to the default-features flag.True
featuresA list of features to use for the crate[]
gitThe Git url to use for the crate. Cannot be used with version.None
revThe git revision of the remote crate. Tied with the git param.None

RETURNS

string: A json encoded string of all inputs

crate.annotation

A collection of extra attributes and settings for a particular crate

PARAMETERS

NameDescriptionDefault Value
versionThe version or semver-conditions to match with a crate.“*”
additive_build_fileA file containing extra contents to write to the bottom of generated BUILD files.None
additive_build_file_contentExtra contents to write to the bottom of generated BUILD files.None
build_script_dataA list of labels to add to a crate's cargo_build_script::data attribute.None
build_script_toolsA list of labels to add to a crate's cargo_build_script::tools attribute.None
build_script_data_globA list of glob patterns to add to a crate's cargo_build_script::data attribute.None
build_script_depsA list of labels to add to a crate's cargo_build_script::deps attribute.None
build_script_envAdditional environment variables to set on a crate's cargo_build_script::env attribute.None
build_script_proc_macro_depsA list of labels to add to a crate's cargo_build_script::proc_macro_deps attribute.None
build_script_rustc_envAdditional environment variables to set on a crate's cargo_build_script::env attribute.None
build_script_toolchainsA list of labels to set on a crates's cargo_build_script::toolchains attribute.None
compile_dataA list of labels to add to a crate's rust_library::compile_data attribute.None
compile_data_globA list of glob patterns to add to a crate's rust_library::compile_data attribute.None
crate_featuresA list of strings to add to a crate's rust_library::crate_features attribute.None
dataA list of labels to add to a crate's rust_library::data attribute.None
data_globA list of glob patterns to add to a crate's rust_library::data attribute.None
depsA list of labels to add to a crate's rust_library::deps attribute.None
gen_build_scriptAn authorative flag to determine whether or not to produce cargo_build_script targets for the current crate.None
patch_argsThe patch_args attribute of a Bazel repository rule. See http_archive.patch_argsNone
patch_toolThe patch_tool attribute of a Bazel repository rule. See http_archive.patch_toolNone
patchesThe patches attribute of a Bazel repository rule. See http_archive.patchesNone
proc_macro_depsA list of labels to add to a crate's rust_library::proc_macro_deps attribute.None
rustc_envAdditional variables to set on a crate's rust_library::rustc_env attribute.None
rustc_env_filesA list of labels to set on a crate's rust_library::rustc_env_files attribute.None
rustc_flagsA list of strings to set on a crate's rust_library::rustc_flags attribute.None
shallow_sinceAn optional timestamp used for crates originating from a git repository instead of a crate registry. This flag optimizes fetching the source code.None

RETURNS

string: A json encoded string containing the specified version and separately all other inputs.

crate.workspace_member

Define information for extra workspace members

PARAMETERS

NameDescriptionDefault Value
versionThe semver of the crate to download. Must be an exact version.none
sha256The sha256 checksum of the .crate file.None

RETURNS

string: A json encoded string of all inputs

crate_deps

Finds the fully qualified label of the requested crates for the package where this macro is called.

PARAMETERS

NameDescriptionDefault Value
depsThe desired list of crate targets.none
package_nameThe package name of the set of dependencies to look up. Defaults to native.package_name().None

RETURNS

list: A list of labels to generated rust targets (str)

crate_repositories

A macro for defining repositories for all generated crates

crate_universe_dependencies

Define dependencies of the cargo-bazel Rust target

PARAMETERS

NameDescriptionDefault Value
rust_versionThe version of rust to use when generating dependencies.“1.62.0”
bootstrapIf true, a cargo_bootstrap_repository target will be generated.False

render_config

Various settings used to configure rendered outputs

The template parameters each support a select number of format keys. A description of each key can be found below where the supported keys for each template can be found in the parameter docs

keydefinition
nameThe name of the crate. Eg tokio
repositoryThe rendered repository name for the crate. Directly relates to crate_repository_template.
tripleA platform triple. Eg x86_64-unknown-linux-gnu
versionThe crate version. Eg 1.2.3
targetThe library or binary target of the crate
fileThe basename of a file

PARAMETERS

NameDescriptionDefault Value
build_file_templateThe base template to use for BUILD file names. The available format keys are [{name}, {version}].“//:BUILD.{name}-{version}.bazel”
crate_label_templateThe base template to use for crate labels. The available format keys are [{repository}, {name}, {version}, {target}].“@{repository}__{name}-{version}//:{target}”
crate_repository_templateThe base template to use for Crate label repository names. The available format keys are [{repository}, {name}, {version}].“{repository}__{name}-{version}”
crates_module_templateThe pattern to use for the defs.bzl and BUILD.bazel file names used for the crates module. The available format keys are [{file}].“//:{file}”
default_package_nameThe default package name to use in the rendered macros. This affects the auto package detection of things like all_crate_deps.None
platforms_templateThe base template to use for platform names. See platforms documentation. The available format keys are [{triple}].“@rules_rust//rust/platform:{triple}”
vendor_modeAn optional configuration for rendirng content to be rendered into repositories.None

RETURNS

string: A json encoded struct to match the Rust config::RenderConfig struct

splicing_config

Various settings used to configure Cargo manifest splicing behavior.

PARAMETERS

NameDescriptionDefault Value
resolver_versionThe resolver version to use in generated Cargo manifests. This flag is only used when splicing a manifest from direct package definitions. See crates_repository::packages.“1”

RETURNS

str: A json encoded string of the parameters provided