Skip to content

Could rustfmt format Cargo.toml ? #4091

Open
@Stargateur

Description

@Stargateur

I actually use a very very small tool toml-fmt, I currently use it like:

toml-fmt < Cargo.toml > Cargo.toml.new && mv Cargo.toml.new Cargo.toml

That not very practical and I must remember to use it. Could rustfmt handle it too ? I think Cargo.toml is also a part of rust file so maybe it's ok to add it.

Activity

calebcartwright

calebcartwright commented on Mar 31, 2020

@calebcartwright
Member

That's an interesting idea @Stargateur

Formatting toml files is outside rustfmt's scope (at least currently), though I can see how having the optional/opt-in ability to format toml files would be beneficial. prettier also supports formatting json and yaml files in addition to JavaScript and TypeScript, so it's not completely unheard of.

How would you envision the behavior when running cargo fmt in a multi-crate workspace? Would the expectation be that cargo fmt would support formatting all the detected Cargo.toml files in the workspace?

Stargateur

Stargateur commented on Apr 1, 2020

@Stargateur
Author

How would you envision the behavior when running cargo fmt in a multi-crate workspace? Would the expectation be that cargo fmt would support formatting all the detected Cargo.toml files in the workspace?

I guess the same that the actual behaviour, if the actual behaviour is to format all detected rust file in a workspace than yes, if no than no.

calebcartwright

calebcartwright commented on Apr 11, 2020

@calebcartwright
Member

It looks like the Style Guide actually already includes a spec for Cargo.toml, so IMO formatting Cargo.toml files accordingly is something that rustfmt should indeed support.

There's a few things to think through (default behavior, cli args/opts, configurable via rustfmt.toml, etc.) but it should be a fun project if anyone is interested in working on it!

gilescope

gilescope commented on Sep 3, 2020

@gilescope

You could start small and gradually crank it up. Alphabetical ordering of blocks of dependencies (with newlines demarcating different blocks of dependencies) would be good.
Adding spaces around dependencies. I.e. foo = "1.2" rather than foo="1.2".

Those two would probably be 80% of the file sorted out nicely - the dependencies are going to have the most change so they are the areas that benefit the most from consistency.

srinikhil-07

srinikhil-07 commented on Nov 8, 2020

@srinikhil-07
Contributor

@calebcartwright
Can I try this one following yours and @gilescope directions?

calebcartwright

calebcartwright commented on Nov 20, 2020

@calebcartwright
Member

@Nikhil0487 - absolutely! I suspect this may end up being a sizeable effort, but can likely be developed iteratively. I still don't have a feel for what the end state would look like from an experience/usability perspective, nor have I really had the bandwidth to think on it.

As such I'd advise starting with the formatting logic (something that takes in an unformatted string and returns a formatted one) and we'll figure out the rest later on. Feel free to put this in a new mod under src/formatting

thomaseizinger

thomaseizinger commented on Nov 20, 2020

@thomaseizinger
Contributor

For people watching this issue, I've developed a dprint plugin that does some formatting of Cargo.toml files: https://github.com/thomaseizinger/dprint-plugin-cargo-toml

dprint also has a rustfmt plugin which means you can have a single tool for all your formatting needs :)

gilescope

gilescope commented on Aug 12, 2021

@gilescope

I notice that vscode is formatting Cargo.toml if I do format on save. But I'm not sure how - is that rust-analyzer or the better-toml extension or something else?

mati865

mati865 commented on Aug 12, 2021

@mati865
Member

@gilescope it's "Even Better TOML" extension who does that. Link to the GitHub project: https://github.com/tamasfe/taplo

56 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @epage@gilescope@mati865@robertbastian@thomaseizinger

        Issue actions

          Could rustfmt format Cargo.toml ? · Issue #4091 · rust-lang/rustfmt