Support rustfmt on crates with generated sources (#2255)

closes https://github.com/bazelbuild/rules_rust/issues/2254
This commit is contained in:
UebelAndre 2023-11-16 04:40:34 -06:00 committed by GitHub
parent 6d35ac1c40
commit ce1b842c9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,6 @@
# rustfmt options: https://rust-lang.github.io/rustfmt/
# Skipping children allows rustfmt to run on crates with
# generated source files. Without this formatting will
# fail with: `failed to resolve mod ${MOD}`.
skip_children = true

View File

@ -46,6 +46,14 @@ file is used whenever `rustfmt` is run:
build --@rules_rust//:rustfmt.toml=//:rustfmt.toml
```
### Tips
Any target which uses Bazel generated sources will cause the `@rules_rust//tools/rustfmt` tool to fail with
``failed to resolve mod `MOD` ``. To avoid failures, [`skip_children = true`](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=skip_chil#skip_children)
is recommended to be set in the workspace's `rustfmt.toml` file which allows rustfmt to run on these targets
without failing.
[rustfmt]: https://github.com/rust-lang/rustfmt#readme
[rsg]: https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/guide.md
[rfcp]: https://github.com/rust-lang-nursery/fmt-rfcs

View File

@ -38,6 +38,14 @@ file is used whenever `rustfmt` is run:
```text
build --@rules_rust//:rustfmt.toml=//:rustfmt.toml
```
#[[
### Tips
]]#
Any target which uses Bazel generated sources will cause the `@rules_rust//tools/rustfmt` tool to fail with
``failed to resolve mod `MOD` ``. To avoid failures, [`skip_children = true`](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=skip_chil#skip_children)
is recommended to be set in the workspace's `rustfmt.toml` file which allows rustfmt to run on these targets
without failing.
[rustfmt]: https://github.com/rust-lang/rustfmt#readme
[rsg]: https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/guide.md

View File

@ -1,9 +1,21 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load(":rustfmt_integration_test_suite.bzl", "rustfmt_integration_test_suite")
exports_files([
"test_rustfmt.toml",
])
write_file(
name = "srcs/generated/generated",
out = "srcs/generated/generated.rs",
content = """\
pub fn greeting() {
println!("Guten tag!");
}
""".splitlines(),
newline = "unix",
)
rustfmt_integration_test_suite(
name = "rustfmt_integration_test_suite",
)

View File

@ -69,6 +69,7 @@ EOF
check_build_result $TEST_FAILED ${variant}_unformatted_2018_test
check_build_result $TEST_OK ${variant}_formatted_2015_test
check_build_result $TEST_OK ${variant}_formatted_2018_test
check_build_result $TEST_OK ${variant}_generated_test
done
# Format a specific target
@ -81,6 +82,7 @@ EOF
check_build_result $TEST_OK ${variant}_unformatted_2018_test
check_build_result $TEST_OK ${variant}_formatted_2015_test
check_build_result $TEST_OK ${variant}_formatted_2018_test
check_build_result $TEST_OK ${variant}_generated_test
done
# Format all targets

View File

@ -29,6 +29,9 @@ def rustfmt_integration_test_suite(name, **kwargs):
tests = []
for variant, rust_rule in _VARIANTS.items():
#
# Test edition 2018
#
rust_rule(
name = "{}_formatted_2018".format(variant),
srcs = ["srcs/2018/formatted.rs"],
@ -53,6 +56,9 @@ def rustfmt_integration_test_suite(name, **kwargs):
targets = [":{}_unformatted_2018".format(variant)],
)
#
# Test edition 2015
#
rust_rule(
name = "{}_formatted_2015".format(variant),
srcs = ["srcs/2015/formatted.rs"],
@ -77,11 +83,30 @@ def rustfmt_integration_test_suite(name, **kwargs):
targets = [":{}_unformatted_2015".format(variant)],
)
#
# Test targets with generated sources
#
rust_rule(
name = "{}_generated".format(variant),
srcs = [
"srcs/generated/lib.rs",
"srcs/generated/generated.rs",
],
crate_root = "srcs/generated/lib.rs",
edition = "2021",
)
rustfmt_test(
name = "{}_generated_test".format(variant),
targets = [":{}_generated".format(variant)],
)
tests.extend([
"{}_formatted_2015_test".format(variant),
"{}_formatted_2018_test".format(variant),
"{}_unformatted_2015_test".format(variant),
"{}_unformatted_2018_test".format(variant),
"{}_generated_test".format(variant),
])
native.test_suite(
@ -93,4 +118,5 @@ def rustfmt_integration_test_suite(name, **kwargs):
native.sh_binary(
name = "{}.test_runner".format(name),
srcs = ["rustfmt_failure_test.sh"],
testonly = True,
)

View File

@ -0,0 +1,5 @@
mod generated;
pub fn main() {
generated::greeting();
}

View File

@ -1 +1,8 @@
# rustfmt options: https://rust-lang.github.io/rustfmt/
# Skipping children allows rustfmt to run on crates with
# generated source files. Without this formatting will
# fail with: `failed to resolve mod ${MOD}`.
skip_children = true
reorder_imports = false

View File

@ -1 +1,6 @@
# rustfmt options: https://rust-lang.github.io/rustfmt/
# Skipping children allows rustfmt to run on crates with
# generated source files. Without this formatting will
# fail with: `failed to resolve mod ${MOD}`.
skip_children = true