Skip to content
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

Don't nuke workspace formatting #70

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Twey
Copy link

@Twey Twey commented Feb 7, 2024

Currently any formatting inside workspace.members is destroyed. Notably this means that a Cargo.toml with a trailing comma in this member can never be correctly formatted with trailing multiline commas enabled:

$ cat tomlfmt.toml
multiline_trailing_comma = true

$ cat Cargo.toml 
[workspace]
members = [
    "arthur",
    "bar",
    "baz",
    "charles",
    "foo",
    "johnny",
    "quux",
]

$ cargo sort .
Checking ....
Finished: Cargo.toml for "." has been rewritten

$ cargo sort -c .
Checking ....
error: Dependencies for . are not sorted
error: Cargo.toml for . is not formatted

$ cargo sort .
Checking ....
Finished: Cargo.toml for "." has been rewritten

$ cargo sort -c .
Checking ....
error: Dependencies for . are not sorted
error: Cargo.toml for . is not formatted

This problem is exacerbated by the fact that the test sort::toml_edit_check, which would otherwise catch this issue, is negated: it checks that the workspace example is changed, which it is: the example

[workspace]

members = [
    "3",
    "4",
    "2",
    "1",
]

is modified to

[workspace]

members = [
    "3",
    "4",
    "2",
    "1"]

which is different. This is an unusual output format, but that's okay, because when running cargo sort for real, it will then immediately be reformatted back into

[workspace]

members = [
    "3",
    "4",
    "2",
    "1",
]

Additionally, this issue can only appear in projects in which a tomlfmt.toml is present. This is because, contrary to documentation, multiline_trailing_comma is actually false by default.

This PR:

  • fixes the test (which then breaks)
  • changes sort::sort_array to not throw away the trailing comma
  • updates the default value of multiline_trailing_comma to true to match the documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant