rust-lang / rust-lang/rust-analyzer

Auto-import destroys previous formatting of nested-imports (merges all into one line)

Open
#17,391 8 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-formatting C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: 0.3.1995-standalone

rustc version: rustc 1.78.0 (9b00956e5 2024-04-29)

editor or extension: VSCode (extension version v0.3.1995)

code snippet to reproduce:

use std::{
    collections::HashMap,
    net::IpAddr,
    fs::{File, copy},
};

fn main() {
    let x = HashSet::new();
}

When triggering an auto-import for HashSet in above snippet, the import is changed to:

use std::{
    collections::{HashMap, HashSet}, fs::{copy, File}, net::IpAddr
};

This makes the import a lot less readable and leads to more diff-noise in git. Instead, I would have expected this:

use std::{
    collections::{HashMap, HashSet},
    net::IpAddr,
    fs::{File, copy},
};

This bug is already present for quite a while (years, if my memory serves me right).

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

Start by reproducing the reported Rust snippet in rust-analyzer with the VSCode extension and trace the auto-import path that formats nested imports. The fix is done when adding HashSet preserves separate import lines and existing formatting, and the behavior is covered by the relevant rust-analyzer test if one is found.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.