rust-lang / rust-lang/rust-analyzer

Auto Import structure breaks with `"rust-analyzer.imports.prefix": "plain"`

Open
#17,320 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I do not believe this is a regression, but it is exacerbated by #17277.


rust-analyzer version: 0.3.1975-standalone (71a816a90 2024-05-26)

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

editor or extension: VSCode

relevant settings: rust-analyzer.imports.prefix

to reproduce:

  1. Create a new project with serde (including feature derive) as a dependency; any external crate would work, but I chose serde for this report.
  2. Create the following src directory
├── lib.rs
├── foo.rs

and the following files.

// lib.rs
use serde::Serialize;

mod foo;

#[derive(Serialize)]
struct Example;

impl Example {
    fn a() {
    }
}
// foo.rs
pub fn b() {
}

It is important that the module is named foo, so it is alphabetically before serde.
3. Through UI or JSON, change the setting rust-analyzer.imports.prefix to self.
4. In the body of Example::a(), type b and use auto-import. Observe that self::foo::b is correctly imported in a group below serde::Serialize. Remove the function call and import, so it once again resembles Step 2.
5. Change rust-analyzer.imports.prefix to plain (the default).
6. In the body of Example::a(), type b and use auto-import. Observe that foo::c is incorrectly imported. It should be below serde::Serialize, as it is a current module import, but it is instead above serde::Serialize in the same group, as if it was an external crate.

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

Reproduce the behavior using the minimal project in src/lib.rs and src/foo.rs, with serde and the rust-analyzer.imports.prefix setting changed between self and plain. Trace the auto-import behavior for b inside Example::a(); done means plain places the current-module import below serde::Serialize rather than grouping it as an external import.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.