Rustfmt: Does `cargo fmt` update registry index and lockfile?

Created on 16 Dec 2019  路  10Comments  路  Source: rust-lang/rustfmt

Related to https://github.com/rust-lang/cargo/issues/7712

I feel that cargo fmt could be significantly slower than this command:

git ls-files -z '*.rs' | xargs -0 rustfmt --edition=2018

Is that because cargo fmt does more initiate steps, including update registry index?

All 10 comments

hey @lzutao. Regarding the edition question in the linked thread, rustfmt 1.x uses the 2015 edition by default, however rustfmt 2.x will be using the 2018 edition by default. That's why the --edition opt is required when running rustfmt 1.x directly without a config file.

Regarding cargo fmt, yes it queries the workspace to determine the various params/opts to be passed to rustfmt (which amongst other things, includes the target edition for all the packages that will be formatted), and this involves running other commands like cargo metadata.

AFAIK cargo fmt is not explicitly/deliberately updating the lockfile nor the registry index, but I would not be surprised if cargo metadata does so (at least in certain situations)

@lzutao AFAICT, if you run cargo fmt --all in the fresh environment, then as @calebcartwright pointed out, cargo metadata could be updating the registry indexes.

If you are experiencing the delay in the CI environment, the delay may be hidden by running cargo build or cargo test before running cargo fmt.

cargo fmt relies on cargo metadata which is recursive to crate dependencies is inevitable.
We could write this to known issues or close this one if you prefer.

@lzutao - what's the full cargo fmt command you are running? Are you including the --all flag by any chance?

Yes, I often use cargo fmt --all to do formatting.

Is that because you're often working in large, multi-package/crate workspaces that have local path based dependencies you also want to format? Personally I wouldn't recommend using the --all flag unless it's really needed.

Here's the description of the --all flag for reference:

Format all packages, and also their local path-based dependencies

Running cargo fmt in the root directory of a workspace will still support formatting all the crates in the workspace, just not the local path-based dependencies

Oh, that's really helpful. Thank you.
But should we note this to README file?

In https://github.com/rust-lang/rustfmt#checking-style-on-a-ci-server, it is recommended
to use cargo fmt --all -- --check?

In https://github.com/rust-lang/rustfmt#checking-style-on-a-ci-server, it is recommended
to use cargo fmt --all -- --check?

So it is, thanks for pointing that out!

IMO that should be changed to cargo fmt -- --check. There are certainly some use cases where the --all flag is useful, but it's definitely not something needed in every use case and that should be reflected accordingly in that readme section.

IMO that should be changed to cargo fmt -- --check.

Agreed :+1: I will update the README. Meanwhile, I will be closing this as resolved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Diggsey picture Diggsey  路  4Comments

tkilbourn picture tkilbourn  路  5Comments

alatiera picture alatiera  路  4Comments

scampi picture scampi  路  4Comments

gnzlbg picture gnzlbg  路  3Comments