Skip to content

Group imports together with imports_granularity results in non-idempotent formatting #6195

Open
@Kobzol

Description

@Kobzol

When group_imports = "StdExternalCrate" is combined with imports_granularity = "Module", rustfmt can format a file in a non-idempotent way, i.e. running rustfmt twice in a row results in a different formatting.

Reduced from a case in the stdlib (rust-lang/rust#126394).

Minimal repro:

# rustfmt.toml
version = "Two"
group_imports = "StdExternalCrate"
imports_granularity = "Module"
$ rustfmt --version
rustfmt 1.7.0-nightly (72fdf91 2024-06-05)
# file.rs
use a::c;
// foo
use a::b;
use a::d;

First run (rustfmt file.rs):

// foo
use a::b;
use a::{c, d};

Second run (rustfmt file.rs):

// foo
use a::{b, c, d};

Removing the group_imports or imports_granularity options removes the issue. The problem seems to be caused by the comment, without it it works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-imports`use` syntaxbugPanic, non-idempotency, invalid code, etc.only-with-optionrequires a non-default option value to reproduce

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions