rust-lang / rust-lang/rust-analyzer

Formatting sends wrong response when line endings are supposed to be changed

Open
#12,540 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When line endings are changed during a formatting request, the whole document is sent to the client.

https://github.com/rust-lang/rust-analyzer/blob/65874dfff205335840cc9265ba06d74f234b719d/crates/rust-analyzer/src/handlers.rs#L1874-L1884

The change is correctly detected and new_text contains the changed endings.
But then to_proto::text_edit_vec subsequently changes the line endings back, because it uses the ones passed in by line_index instead of new_line_endings

So the actual TextEdit in the response contains new_text with the old line endings.

Steps to reproduce

  1. be on LF system
  2. open file with CRLF endings
  3. make document formating request
  4. https://github.com/rust-lang/rust-analyzer/blob/65874dfff205335840cc9265ba06d74f234b719d/crates/rust-analyzer/src/handlers.rs#L1876 reports CRLF -> LF change and new_text contains LF endings
  5. the client receives TextEdit replacing the document but still containing CRLF endings

This results in clients receiving the whole document on each request, because they always make requests with line endings that rust-analyzer wants to convert.

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 in crates/rust-analyzer/src/handlers.rs around lines 1874-1884 and trace the formatting response through to_proto::text_edit_vec. Verify how line_index and new_line_endings affect the returned TextEdit. Done means formatting a CRLF document on an LF system returns the intended line endings without replacing the whole document repeatedly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.