Skip to content

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

@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-commentsArea: commentsA-importsArea: imports, e.g. `use` syntaxC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEI-non-idempotencyIssue: non-idempotency (different formatting outcomes even though unchanged by user)UO-group_importsUnstable option: group_importsUO-imports_granularityUnstable option: imports_granularityonly-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