-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Format placeholder code when generating a crate #7827
Format placeholder code when generating a crate #7827
Conversation
Today I learned that `rustfmt` is now a part of `rustup`. So I need to actually look it up in PATH instead of using `cargo-fmt`. I also learned that `mk` is the file that does the main work and is shared by `cargo-new` and `cargo-init`, as expected. Printing inside `mk` produces output as expected.
I think I found the correct place in the algorithm where rustfmt should be called. I'm not sure if using `std::process::Command` and converting errors into warnings is the correct way here. Also the warning should only be printed once. Unless `config.shell().warn()` deduplicates warnings, right now there will be one warning per source file. So I should probably check that `rustfmt` exists only once and disable formatting if it doesn't.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Eh2406 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
It looks good to me, can we add a test? |
Sure! One for the good case and one for the case with missing Should I put them in I actually wasn't planning on submitting this PR just yet, which is why it's a draft and there's no description. I'll make sure it only prints the warning once and reword the warning first. |
Either location would be great. Let me know when you are ready for revue. |
There's now exactly one warning per new crate instead of one per source file. Also warnings are being logged now, not printed to console.
Test 1: init a library crate with a `rustfmt.toml` config file in it. Expect the generated source files to be formatted according to the config file. Test 2: same as test 1, but with missing `rustfmt`. Expect `cargo init` to ignore the absence of `rustfmt` and generate source files with default formatting.
@Eh2406 ready 😃
I eventually decided to put them in |
Also feel free to bikeshed: I'm sure this is not entirely idiomatic Rust |
I would leave it how it was before and avoid calling Also, the tests appear to be failing. |
ae4adb2
to
ea4f53c
Compare
The tests were failing with error: "could not determine the current user, please set $USER"
Oops about the tests, I'm still on Windows. Should be fixed now. Edit: nope, not fixed.
The source files are being created in a loop, so it's possible that rustfmt will be called multiple times, isn't it? |
Hm, I forgot that I wouldn't worry about the performance if |
Yeah, I initially considered formatting the whole directory. I think I'm only applying
I was mostly worried about spamming the same error multiple times. But I guess this is less of a problem with logs, will fix. |
Worst case is that the logs will have multiple errors caused by missing rustfmt, so checking separately that rustfmt exists is unnecessary.
I think the test ( Does it make sense to just disable the test if there's no This could also be a problem for other contributors. But I could add a note that this error can be safely ignored if the user doesn't have rustup/rustfmt. |
Sorry for the delay. Can you add |
Oh, I totally should have thought of doing that D: Done! |
Generalized `clippy_is_available` and renamed as `command_is_available`. No checks in `ignores_failure_to_format_source`, it's not supposed to use `rustfmt` even if it's available
Thanks! @bors r+ |
📌 Commit bc4c65c has been approved by |
…ating-a-crate, r=ehuss Format placeholder code when generating a crate When generating source files in `cargo new` and `cargo init`, try to run `rustfmt` CLI on them. If it fails, log the error and proceed. Tests: * Works for `cargo init --lib` * No changes in behavior if `rustfmt` is missing Closes #7656
☀️ Test successful - checks-azure |
Yay, thanks :D 🎉 |
When generating source files in
cargo new
andcargo init
, try to runrustfmt
CLI on them.If it fails, log the error and proceed.
Tests:
cargo init --lib
rustfmt
is missingAlso install
rustfmt
component in CI.Also replace
clippy_is_available
with a more generalcommand_is_available
.Closes #7656