forked from bazelbuild/rules_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
rust_lint_group
and cargo_lints
rule to define lint gro…
…ups (bazelbuild#2993) Chatted about this previously in [Slack](https://bazelbuild.slack.com/archives/CSV56UT0F/p1718030356910019). The goal with this PR is to make it easier to define and share lint configurations for your build. ### Description The PR adds two new rules, `rust_lint_group` and `cargo_lints`. It also adds a "lints" attr to `rust_library` and `rust_binary` rules that expects a new `LintsInfo` provider. * `rust_lint_group` allows you to define Rust, Clippy, and Rustdoc lints in a `BUILD` file. * `cargo_lints` automatically generates a set of lints by reading a crate's `Cargo.toml`, and optionally the workspace's Cargo.toml for workspace inheritance. Then the rustc, clippy, and rustdoc actions are all updated to check for the `LintsInfo` provider and appends the correct arguments so the lints take effect. ### Design Honestly this is my first large change to a set of Bazel rules, so I definitely could have done something wrong here! The change is split into two commits, the first introduces `rust_lint_group` which IMO is relatively straight forward. Some attributes are defined on a rule which are then formatted into command line flags and passed around with a provider. The second commit adds `cargo_lints` and is much larger, a few things worth considering: * I was back and forth a bit on using a `repository_rule` or a regular `rule`. While the `repository_rule` maps well to a Cargo Workspace and you'd only need to define it once, not everyone uses workspaces and so I figured a regular `rule` was more general. * Parsing a `Cargo.toml` is done via a new Rust binary called `cargo_toml_info`. I tried to keep the external dependencies on this binary to a minimum, it only has one at the moment which is [`cargo_toml`](https://docs.rs/cargo_toml/latest/cargo_toml/) and I based this change largely off of bazelbuild#2772. I tried to make the tool general though so other Cargo metadata rules could use it in the future. ### Tests There aren't any! I wasn't sure where the best place to start was, any guidance here is appreciated!
- Loading branch information
Showing
53 changed files
with
3,374 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
""" | ||
The dependencies for running the cargo_toml_info binary. | ||
""" | ||
|
||
load("//cargo/private/cargo_toml_info/3rdparty/crates:defs.bzl", "crate_repositories") | ||
|
||
def cargo_dependencies(): | ||
"""Define dependencies of the `cargo` Bazel tools""" | ||
return crate_repositories() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Rule used to retrieve the lints specified in the [lints section] of a `Cargo.toml`. | ||
[lints section](https://doc.rust-lang.org/cargo/reference/manifest.html#the-lints-section) | ||
""" | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:providers.bzl", "LintsInfo") | ||
|
||
def _extract_cargo_lints(ctx): | ||
# Cargo.toml's to read from. | ||
inputs = [ctx.file.manifest] | ||
args = ctx.actions.args() | ||
|
||
args.add("--manifest_toml={0}".format(ctx.file.manifest.path)) | ||
if ctx.attr.workspace: | ||
inputs.append(ctx.file.workspace) | ||
args.add("--workspace_toml={0}".format(ctx.file.workspace.path)) | ||
args.add("lints") | ||
|
||
# Files to output our formatted arguments into. | ||
rustc_lints_out = ctx.actions.declare_file(ctx.label.name + ".rustc" + ".lints") | ||
clippy_lints_out = ctx.actions.declare_file(ctx.label.name + ".clippy" + ".lints") | ||
rustdoc_lints_out = ctx.actions.declare_file(ctx.label.name + ".rustdoc" + ".lints") | ||
|
||
args.add(rustc_lints_out) | ||
args.add(clippy_lints_out) | ||
args.add(rustdoc_lints_out) | ||
|
||
outputs = [rustc_lints_out, clippy_lints_out, rustdoc_lints_out] | ||
|
||
ctx.actions.run( | ||
outputs = outputs, | ||
executable = ctx.file._cargo_toml_info, | ||
inputs = inputs, | ||
arguments = [args], | ||
mnemonic = "CargoLints", | ||
progress_message = "Reading Cargo metadata to get Lints for {}".format(ctx.attr.name), | ||
) | ||
|
||
return [ | ||
DefaultInfo(files = depset(outputs), runfiles = ctx.runfiles(outputs)), | ||
LintsInfo( | ||
rustc_lint_flags = [], | ||
rustc_lint_files = [rustc_lints_out], | ||
clippy_lint_flags = [], | ||
clippy_lint_files = [clippy_lints_out], | ||
rustdoc_lint_flags = [], | ||
rustdoc_lint_files = [rustdoc_lints_out], | ||
), | ||
] | ||
|
||
extract_cargo_lints = rule( | ||
implementation = _extract_cargo_lints, | ||
attrs = { | ||
"manifest": attr.label( | ||
allow_single_file = True, | ||
mandatory = True, | ||
doc = "Cargo.toml to read lints from.", | ||
), | ||
"workspace": attr.label( | ||
allow_single_file = True, | ||
doc = "Workspace Cargo.toml that the manifest Cargo.toml inherits from.", | ||
), | ||
"_cargo_toml_info": attr.label( | ||
allow_single_file = True, | ||
executable = True, | ||
default = Label("//cargo/private/cargo_toml_info:cargo_toml_info"), | ||
cfg = "exec", | ||
), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("//crate_universe:defs.bzl", "crate", "crates_vendor") | ||
|
||
crates_vendor( | ||
name = "crates_vendor", | ||
cargo_lockfile = "Cargo.Bazel.lock", | ||
mode = "remote", | ||
packages = { | ||
"cargo_toml": crate.spec(version = "0.20.5"), | ||
}, | ||
repository_name = "rrcti", | ||
tags = ["manual"], | ||
) |
155 changes: 155 additions & 0 deletions
155
cargo/private/cargo_toml_info/3rdparty/Cargo.Bazel.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
############################################################################### | ||
# @generated | ||
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To | ||
# regenerate this file, run the following: | ||
# | ||
# bazel run @@//cargo/private/cargo_toml_info/3rdparty:crates_vendor | ||
############################################################################### | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files( | ||
[ | ||
"cargo-bazel.json", | ||
"crates.bzl", | ||
"defs.bzl", | ||
] + glob( | ||
include = ["*.bazel"], | ||
allow_empty = True, | ||
), | ||
) | ||
|
||
filegroup( | ||
name = "srcs", | ||
srcs = glob( | ||
include = [ | ||
"*.bazel", | ||
"*.bzl", | ||
], | ||
allow_empty = True, | ||
), | ||
) | ||
|
||
# Workspace Member Dependencies | ||
alias( | ||
name = "cargo_toml", | ||
actual = "@rrcti__cargo_toml-0.20.5//:cargo_toml", | ||
tags = ["manual"], | ||
) |
Oops, something went wrong.