rust-lang / rust-lang/rust-analyzer

Potential character range mis-calculation leading to erroneous text replacement

Open
#14,343 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

rust-analyzer version: rust-analyzer 1.65.0-nightly (2befdefd 2022-08-06)

rustc version: rustc 1.67.1 (d5a82bbd2 2023-02-07)

relevant settings: Editor is BBEdit version 14.6.4

Reproduction example: https://github.com/fspeirs/bbedit-rust-analyzer-bug/ @ 5fc0a1b

I noticed an issue in BBEdit when using rust-analyzer when I wanted to change L1 in bbedit_test::main.rs.

use bbedit_lib::add;

becomes:

use bbedit_lib::{bbedit_do_thing, add};

I can reproduce the bug by:

  • Select the text "add" in the import line.
  • Type { to wrap it in braces
  • Left arrow to move to after the opening brace
  • Type-select "bbedi" to import "bbedit_do_thing"
  • Tab to accept the completion.

Expected result: that the braces would contain {bbedit_do_thing, add}

Result: add is overwritten with bbedit_do_thing.

Feedback from BareBones tech support:

When requesting the completion, BBEdit sends the following position parameter in the request:

    position =     {
        character = 22;
        line = 0;
    };

Character position 22 corresponds to where the insertion point is after typing "bbedi" just inside the open brace.

The response from the server includes an entry for bbedit_do_thing:

    {
        additionalTextEdits = (
        );
        deprecated = 0;
        detail = "fn()";
        filterText = "bbedit_do_thing";
        kind = 3;
        label = "bbedit_do_thing";
        preselect = 1;
        sortText = ffffffef;
        textEdit =             {
            newText = "bbedit_do_thing";
            range =                 {
                end =                     {
                    character = 25;
                    line = 0;
                };
                start =                     {
                    character = 17;
                    line = 0;
                };
            };
        };
   }

The textEdit value is an instruction to replace characters 17 through 25 on the first line with "bbedit_do_thing". This range includes the "add" that was already there.

When BBEdit follows the instruction specified by the server, the practical effect is to replace the add with bbedit_do_thing.

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 with the linked reproduction at fspeirs/bbedit-rust-analyzer-bug commit 5fc0a1b and the reported completion request and textEdit range. Trace how rust-analyzer calculates completion replacement ranges for the BBEdit sequence, then verify that accepting the completion preserves add inside the braces as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.