diff --git a/.rustfmt.toml b/.rustfmt.toml index e69de29bb..c5eee4814 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -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 diff --git a/docs/rust_fmt.md b/docs/rust_fmt.md index 11c3fcd27..e33b7b8ef 100644 --- a/docs/rust_fmt.md +++ b/docs/rust_fmt.md @@ -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 diff --git a/docs/rust_fmt.vm b/docs/rust_fmt.vm index 48bac64dd..727d8fea3 100644 --- a/docs/rust_fmt.vm +++ b/docs/rust_fmt.vm @@ -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 diff --git a/test/rustfmt/BUILD.bazel b/test/rustfmt/BUILD.bazel index 2d5a8103b..1b327cb8d 100644 --- a/test/rustfmt/BUILD.bazel +++ b/test/rustfmt/BUILD.bazel @@ -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", ) diff --git a/test/rustfmt/rustfmt_failure_test.sh b/test/rustfmt/rustfmt_failure_test.sh index 8d97612d7..3daaf238f 100755 --- a/test/rustfmt/rustfmt_failure_test.sh +++ b/test/rustfmt/rustfmt_failure_test.sh @@ -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 diff --git a/test/rustfmt/rustfmt_integration_test_suite.bzl b/test/rustfmt/rustfmt_integration_test_suite.bzl index d7f56eb8a..df3bec800 100644 --- a/test/rustfmt/rustfmt_integration_test_suite.bzl +++ b/test/rustfmt/rustfmt_integration_test_suite.bzl @@ -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, ) diff --git a/test/rustfmt/srcs/generated/lib.rs b/test/rustfmt/srcs/generated/lib.rs new file mode 100644 index 000000000..e451afbdd --- /dev/null +++ b/test/rustfmt/srcs/generated/lib.rs @@ -0,0 +1,5 @@ +mod generated; + +pub fn main() { + generated::greeting(); +} diff --git a/test/rustfmt/test_rustfmt.toml b/test/rustfmt/test_rustfmt.toml index fa091418c..34c276eec 100644 --- a/test/rustfmt/test_rustfmt.toml +++ b/test/rustfmt/test_rustfmt.toml @@ -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 diff --git a/tools/rustfmt/rustfmt.toml b/tools/rustfmt/rustfmt.toml index 44bdbf248..c5eee4814 100644 --- a/tools/rustfmt/rustfmt.toml +++ b/tools/rustfmt/rustfmt.toml @@ -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