Skip to content

Provide an easy way for contributors to run cargo fmt like it's executed on CI #3835

@phansch

Description

@phansch

As pointed out by @flip1995 in #3824 (comment), we currently don't have an easy way to run rustfmt on the tests locally like we do on CI.

What we are doing on CI currently:

rustup component add rustfmt || cargo install --git https://github.com/rust-lang/rustfmt/ --force

cargo +nightly fmt --all -- --check

# make sure tests are formatted
# some lints are sensitive to formatting, exclude some files
tests_need_reformatting="false"
# switch to nightly
rustup override set nightly
# avoid loop spam and allow cmds with exit status != 0
set +ex
for file in `find tests -not -path "tests/ui/doc.rs" -not -path "tests/ui/unused_unit.rs" | grep "\.rs$"` ; do
rustfmt ${file} --check
if [ $? -ne 0 ]; then
echo "${file} needs reformatting!"
tests_need_reformatting="true"
fi
done
set -ex # reset
if [ "${tests_need_reformatting}" == "true" ] ; then
echo "Tests need reformatting!"
exit 2
fi
# switch back to master
rustup override set master

It would be nice if we could extract that into a separate script so that people can use to format their tests locally, instead of waiting for CI.

Some things to consider when doing this:

  • The script also needs to add the rustfmt component if it isn't present locally
  • We use rustfmt instead of cargo fmt for the test formatting because of cargo fmt doesn't format files in sub-directories in tests rustfmt#1820
  • To make it idempotent, the new script should automatically detect the 'current' toolchain and switch back to it after the formatting script has finished (instead of switching to master)
  • Unlike a fresh VM on CI, We can't just use rustup override set nightly, because that doesn't update the locally installed nightly rust. We should probably pin to a specific nightly instead and update the version from time to time.
  • Using a shell script will not work cross-platform but I think doing it in Rust might be too much work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions