rust-lang / rust-lang/rustfmt

`group_imports` does not differentiate between a local import and an external crate

Open
#4,709 10 comments 23 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-imports C-feature-request P-low UO-group_imports
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Describe the bug

rustfmt does not differentiate between local imports not prefixed by self:: and external crates. The behavior is as expected when prefixed by self::.

To Reproduce

Using group_imports = "StdExternalCrate" in rustfmt.toml, the following code is considered formatted.

mod foo {
    struct Foo;
}

use foo::Foo;
use rand::random;

Expected behavior

rustfmt recognizes that foo is a local module, not an external crate.

mod foo {
    struct Foo;
}

use rand::random;

use foo::Foo;

Meta

  • rustfmt version: rustfmt 1.4.30-stable (8c6769d 2021-01-18)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: cargo fmt

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Reproduce the behavior with group_imports = "StdExternalCrate" and the provided module and imports, then trace rustfmt's import-grouping logic. Done means local imports without self:: are separated from external-crate imports, with coverage for the shown example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.