Cargo format toml

Hey, dumb Rust noob question:

cargo fmt does not format the Cargo.toml and config.toml right? How do you auto-format those files in your projects?


# needed by all users not maintainer

cargo install cargo-sort candid-extractor ic-wasm

# cleanup

cargo sort -w 1>/dev/null
3 Likes

Oh nice, thanks!

It didn’t work in my workspace, though.

thread ‘main’ panicked at /Users/daviddalbusco/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cargo-sort-1.0.9/src/sort.rs:113:46:
called Result::unwrap() on an Err value: TomlError { message: “TOML parse error at line 11, column 7\n |\n11 | candid.workspace = true\n | ^\nUnexpected .\nExpected =\n” }
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

It seems it doesn’t support referencing workspace dependencies the way I do in my crates:

[dependencies]
candid.workspace = true
ic-cdk.workspace = true
etc

As a result, I gave Google another try and found prettier-plugin-toml, which seems to work. Probably less state-of-the-art, but it’ll do.

Nevertheless, cool to know what’s the cargo way!

oh weird. it just does the deps part and always works.

#justvscodethings

Oh, maybe something like candid.workspace = true is not officially supported by cargo? I borrowed this pattern from having a look at crates of the foundation.

On VSCode, you want this:

1 Like

I saw that project too but actually wondered if it was still active because on crate, there’s no README, and the last release was published 8 months ago. As you mention it, I notice now that the plugin is more active (I don’t use VS Code). Coolio, thx for the tips!