Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rustfmt on crates with generated sources #2255

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions docs/rust_fmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions docs/rust_fmt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
2 changes: 2 additions & 0 deletions test/rustfmt/rustfmt_failure_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/rustfmt/rustfmt_integration_test_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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(
Expand All @@ -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,
)
5 changes: 5 additions & 0 deletions test/rustfmt/srcs/generated/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod generated;

pub fn main() {
generated::greeting();
}
7 changes: 7 additions & 0 deletions test/rustfmt/test_rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tools/rustfmt/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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
Loading