rust-analyzer: Invalid offset -32603 when editing an empty .rs file

rust-analyzer version: 2024-05-09

rustc version: (eg. output of rustc -V): 1.78.0

editor or extension: neovim with Mason+Lspconfig+RustAnalyzer

relevant settings: none

repository link (if public, optional): none

code snippet to reproduce:


Rust analyzer is printing this error if I start editing an empty .rs file before it has completed its startup. There are no settings involved, just an empty cargo project with a main.rs. Once that error pops up, it will keep appearing on every keystroke until I close the buffer, reopen it, wait for it to fully initialize and then start writing. This does not happen if the file is not empty.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 57
  • Comments: 57 (11 by maintainers)

Commits related to this issue

Most upvoted comments

It’s still not fixed.

I am still have this error even though I have the most recent version of rust-analyzer installed via rustup and NVIM is picking it up.

:!which rust-analyzer -v /root/.cargo/bin/rust-analyzer

:!rust-analyzer -V rust-analyzer 1.81.0 (eeb90cd 2024-09-04)

I do NOT have rust-analyzer installed via Mason:

:Mason

Language Filter: press <C-f> to apply filter

  Installed
    ◍ bash-language-server bashls
    ◍ codelldb
    ◍ lua-language-server lua_ls
    ◍ shellcheck
    ◍ shfmt
    ◍ stylua
    ◍ taplo

However, when saving a file I still get these errors (disregard the circles I drew, they’re not related to this issue)

Image

@lnicola @Veykril Is there any update on this? It’s quite hard to use rust-analyzer with Neovim as it currently is.

Potential Fix

Mason downloads rust-analyzer version 0.35 on my system, while rustup downloads version 1.80. This is most likely an issue with Mason specifically and not Neovim, as using the updated binary fixes the issue on my end. YMMV as always, but given that’s a massively out of date binary, I imagine it’s a compatibility issue with the old rust-analyzer binary and newer LSP protocols.

Check Mason’s rust-analyzer version

~/.local/share/nvim/mason/bin/rust-analyzer --version

If this is an older version, proceed with the following:

Download the newest rust-analyzer version via rustup

rustup component add rust-analyzer

This will download the newest version, which can be found in ~/.cargo/bin/rust-analyzer. Now remove the one Mason downloaded at ~/.local/share/nvim/mason/bin/rust-analyzer via an rm call as follows, then create a symlink to the new one from rustup:

Create symlink to rustup version

rm ~/.local/share/nvim/mason/bin/rust-analyzer
ln -s ~/.cargo/bin/rust-analyzer ~/.local/share/nvim/mason/bin/rust-analyzer

This will likely need to be repeated if you update rust-analyzer through Mason. Consider creating a shell script with the second snippet to run in case of update.

In the case that this resolves the issue for everyone, this is not an issue for this repo, but williamboman/mason.nvim.

This issue still persists in v0.10.4:

NVIM v0.10.4
Build type: RelWithDebInfo
LuaJIT 2.1.1731601260

https://github.com/rust-lang/rust-analyzer/pull/17742 fixed the remaining issue. For other requests, rust-analyzer has been correctly reporting errors due to invalid offset as “content modified” error, which is ignored by the client UI. However, inlay hint handler in the versions prior to #17742 reported it as an internal error (which is shown to the user) because inlay hint request was marked as retry-able. #17742 fixes this by marking it as not retry-able.

@yuvaldolev how do you install it, using rustup, or from source / GitHub releases?

I installed rust-analyzer using Mason (Rust itself is installed using Rustup but not rust-analyzer).

BTW this error seems to happen in other times than just editing an empty file. It happens to me once in a while when coding in Neovim, which is a real bummer as I have to reopen Neovim and lose the state of my project (doing LspRestart doesn’t solve this… I must reopen Neovim to fix it).

Can confirm I get this error on both vscode and neovim, I have tried installing rust-analyzer both from mason and rustup but it does not solve the issue.

I experienced the same issue with neovim. They have a fix for it in nightly.

The issue has been fixed for me in the nightly neovim release.

NVIM v0.11.0-dev-1810+gdb2c3d1143
Build type: RelWithDebInfo
LuaJIT 2.1.1736781742

I’m still having this issue on v0.10.3:

NVIM v0.10.3
Build type: Release
LuaJIT 2.1.1713773202

I found a temporary solution to it:

I am using noice.nvim, so I hide this message through configuration:

routes = {
{
  filter = {
    event = "notify",
    error = true,
    find = "Invalid offset LineCol",
  },
  opts = { skip = true },
  },
}

notify can use override vim.lsp.handlers to hidden it.

I also do experience this issue, using Rust edition 2024 and nightly rust-analyzer seem to occur more frequently for me.

It seems that both neovim and rust-analyzer have some issues. Here is what I found, but I wouldn’t have enough time to investigate further.

environment

  • ubuntu 22.04

  • cargo new bug. This creates src/main.rs with the follwoing content in the new workspace

    fn main() {
        println!("Hello, world!");
    }
    
  • apply the following patch to rust-analyzer (3cf17dcfc)

    patch
    diff --git a/crates/rust-analyzer/src/line_index.rs b/crates/rust-analyzer/src/line_index.rs
    index 951762074..102e30fde 100644
    --- a/crates/rust-analyzer/src/line_index.rs
    +++ b/crates/rust-analyzer/src/line_index.rs
    @@ -9,12 +9,13 @@ use ide_db::line_index::WideEncoding;
     use memchr::memmem;
     use triomphe::Arc;
     
    -#[derive(Clone, Copy)]
    +#[derive(Clone, Copy, Debug)]
     pub enum PositionEncoding {
         Utf8,
         Wide(WideEncoding),
     }
     
    +#[derive(Debug)]
     pub(crate) struct LineIndex {
         pub(crate) index: Arc<ide::LineIndex>,
         pub(crate) endings: LineEndings,
    diff --git a/crates/rust-analyzer/src/lsp/from_proto.rs b/crates/rust-analyzer/src/lsp/from_proto.rs
    index aea424298..4f5c320a6 100644
    --- a/crates/rust-analyzer/src/lsp/from_proto.rs
    +++ b/crates/rust-analyzer/src/lsp/from_proto.rs
    @@ -35,9 +35,10 @@ pub(crate) fn offset(
             }
         };
         let text_size = line_index.index.offset(line_col).ok_or_else(|| {
    -        format_err!("Invalid offset {line_col:?} (line index length: {:?})", line_index.index.len())
    -    })?;
    -    Ok(text_size)
    +        panic!("Invalid offset {line_col:?} (line index length: {:?})\n{line_index:#?}", line_index.index.len())
    +        // format_err!("Invalid offset {line_col:?} (line index length: {:?})\n{line_index:#?}", line_index.index.len())
    +    });
    +    text_size
     }
     
     pub(crate) fn text_range(
    
  • nvim 0.11 dev version, with rustaceanvim configured to use the patched rust-analyzer

reproduction step: open main.rs, wait until indexing and etc finishes, then ggdG.

Then you will get something like this in nvim’s lsp log file

messages
Panic context:
request: textDocument/inlayHint InlayHintParams {
  ...
    range: Range {
        start: Position {
            line: 0,
            character: 0,
        },
        end: Position {
            line: 3,
            character: 0,
        },
    },
}
...
LineIndex {
    index: LineIndex {
        newlines: [],
        line_wide_chars: {},
        len: 0,
    },
    endings: Unix,
    encoding: Wide(
        Utf16,
    ),
}

   2: rust_analyzer::lsp::from_proto::offset::{{closure}}
             at ./crates/rust-analyzer/src/lsp/from_proto.rs:38:9
   3: core::option::Option<T>::ok_or_else
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/option.rs:1236:25
   4: rust_analyzer::lsp::from_proto::offset
             at ./crates/rust-analyzer/src/lsp/from_proto.rs:37:21
   5: rust_analyzer::lsp::from_proto::text_range
             at ./crates/rust-analyzer/src/lsp/from_proto.rs:49:15
   6: rust_analyzer::lsp::from_proto::file_range_uri
             at ./crates/rust-analyzer/src/lsp/from_proto.rs:85:17
   7: rust_analyzer::lsp::from_proto::file_range
             at ./crates/rust-analyzer/src/lsp/from_proto.rs:75:5
   8: rust_analyzer::handlers::request::handle_inlay_hints
             at ./crates/rust-analyzer/src/handlers/request.rs:1561:40

This issue can be fixed with the following patch to nvim.

diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 59ae3beae..0615c5da1 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -2145,7 +2145,7 @@ local function make_line_range_params(bufnr, start_line, end_line, offset_encodi
   ---@type lsp.Position
   local end_pos
 
-  if end_line == last_line and not vim.api.nvim_get_option_value('endofline', { buf = bufnr }) then
+  if end_line == last_line then
     end_pos = {
       line = end_line,
       character = M.character_offset(bufnr, end_line, #get_line(bufnr, end_line), offset_encoding),

(Or alternatively, rust-analyzer could be modified to gracefully handle the last line eol.)

update: the following is invalid. see https://github.com/rust-lang/rust-analyzer/issues/17289#issuecomment-2255637976


outdated

Now repeat the same repro steps. Then you will get a similar error with slightly different contents:

messages
"rpc.receive"	{ id = 10, jsonrpc = "2.0", method = "workspace/inlayHint/refresh" }
"server_request: callback result"	{ result = vim.NIL, status = true }
"rpc.send"	{ id = 9, jsonrpc = "2.0", result = vim.NIL }
"server_request"	"workspace/inlayHint/refresh"	nil
"server_request: found handler for"	"workspace/inlayHint/refresh"
"default_handler"	"workspace/inlayHint/refresh"	{ ctx = '{\n  client_id = 1,\n  method = "workspace/inlayHint/refresh"\n}' }
"LSP[rust-analyzer]"	"client.request"	1	"textDocument/inlayHint"	{ range = { ["end"] = { character = 1, line = 2 }, start = { character = 0, line = 0 } }, textDocument = { uri = "file:///home/user/apps/neovim/bug/src/main.rs" } }	<function 1>	1
"rpc.send"	{ id = 4, jsonrpc = "2.0", method = "textDocument/inlayHint", params = { range = { ["end"] = { character = 1, line = 2 }, start = { character = 0, line = 0 } }, textDocument = { uri = "file:///home/user/apps/neovim/bug/src/main.rs" } } }
"rpc.receive"	{ id = 21, jsonrpc = "2.0", method = "workspace/inlayHint/refresh" }
"server_request: callback result"	{ result = vim.NIL, status = true }
"rpc.send"	{ id = 19, jsonrpc = "2.0", result = vim.NIL }
"server_request: callback result"	{ result = vim.NIL, status = true }
"rpc.send"	{ id = 20, jsonrpc = "2.0", result = vim.NIL }
"server_request"	"workspace/inlayHint/refresh"	nil
"server_request: found handler for"	"workspace/inlayHint/refresh"
"default_handler"	"workspace/inlayHint/refresh"	{ ctx = '{\n  client_id = 1,\n  method = "workspace/inlayHint/refresh"\n}' }
"LSP[rust-analyzer]"	"client.request"	1	"textDocument/inlayHint"	{ range = { ["end"] = { character = 1, line = 2 }, start = { character = 0, line = 0 } }, textDocument = { uri = "file:///home/user/apps/neovim/bug/src/main.rs" } }	<function 1>	1
"rpc.send"	{ id = 5, jsonrpc = "2.0", method = "textDocument/inlayHint", params = { range = { ["end"] = { character = 1, line = 2 }, start = { character = 0, line = 0 } }, textDocument = { uri = "file:///home/user/apps/neovim/bug/src/main.rs" } } }
"server_request: callback result"	{ result = vim.NIL, status = true }
"rpc.send"	{ id = 21, jsonrpc = "2.0", result = vim.NIL }
"server_request: callback result"	{ result = vim.NIL, status = true }
"rpc.send"	{ id = 22, jsonrpc = "2.0", result = vim.NIL }
"server_request: callback result"	{ result = vim.NIL, status = true }
"rpc.send"	{ id = 23, jsonrpc = "2.0", result = vim.NIL }
"rpc.send"	{ jsonrpc = "2.0", method = "textDocument/didChange", params = { contentChanges = { { range = { ["end"] = { character = 0, line = 3 }, start = { character = 0, line = 0 } }, rangeLength = 45, text = "" } }, textDocument = { uri = "file:///home/user/apps/neovim/bug/src/main.rs", version = 4 } } }
"rpc.receive"	{ id = 25, jsonrpc = "2.0", method = "workspace/inlayHint/refresh" }
"server_request: callback result"	{ result = vim.NIL, status = true }
"rpc.send"	{ id = 24, jsonrpc = "2.0", result = vim.NIL }
"server_request"	"workspace/inlayHint/refresh"	nil
"server_request: found handler for"	"workspace/inlayHint/refresh"
"default_handler"	"workspace/inlayHint/refresh"	{ ctx = '{\n  client_id = 1,\n  method = "workspace/inlayHint/refresh"\n}' }
"LSP[rust-analyzer]"	"client.request"	1	"textDocument/inlayHint"	{ range = { ["end"] = { character = 0, line = 0 }, start = { character = 0, line = 0 } }, textDocument = { uri = "file:///home/user/apps/neovim/bug/src/main.rs" } }	<function 1>	1
"rpc.send"	{ id = 10, jsonrpc = "2.0", method = "textDocument/inlayHint", params = { range = { ["end"] = { character = 0, line = 0 }, start = { character = 0, line = 0 } }, textDocument = { uri = "file:///home/user/apps/neovim/bug/src/main.rs" } } }
"server_request: callback result"	{ result = vim.NIL, status = true }
"rpc.send"	{ id = 25, jsonrpc = "2.0", result = vim.NIL }
"rpc.receive"	{ error = { code = -32801, message = "content modified" }, id = 7, jsonrpc = "2.0" }
"rpc.receive"	{ error = { code = -32801, message = "content modified" }, id = 9, jsonrpc = "2.0" }
"rpc.receive"	{ error = { code = -32801, message = "content modified" }, id = 6, jsonrpc = "2.0" }
"codelens"	{ code = -32801, message = "content modified", <metatable> = { __tostring = <function 1> } }

...

Panic context:
> 
version: 0.0.0 (3cf17dc 2024-07-28)
request: textDocument/inlayHint InlayHintParams {
    range: Range {
        start: Position {
            line: 0,
            character: 0,
        },
        end: Position {
            line: 2,
            character: 1,
        },
    },
}
Invalid offset LineCol { line: 2, col: 1 } (line index length: 0)
LineIndex {
    index: LineIndex {
        newlines: [],
        line_wide_chars: {},
        len: 0,
    },
    endings: Unix,
    encoding: Wide(
        Utf16,
    ),
}

What happens here is that

  • There is an ongoing inlayHint request with
    range = { ["end"] = { character = 1, line = 2 }, start = { character = 0, line = 0 } }
    
    that was sent at the beginning
  • the user executes ggdG, and the change is sent via didChange
  • rust-analyzer processes the old inlayHint, which references the position that does not exist anymore

This is odd because rust-analyzer is using some form of snapshot for global state, which would prevent such issues. So it seems either

  • there is some mistake in the implementation of the snapshot, e.g., shallow copy instead of deep copy or
  • the order of requests is not preserved.

I have the same error with latest stable neovim, mason, and rust-analyzer. $ cargo new helloworld, $ edit the src/main.rs and start deleting all lines, and it will start getting into a error flooding these errors and making it hard to navigate in nvim.

rust-analyzer: -32603: Invalid offset LineCol { line: 5, col: 0 } (line index length: 16)
Press ENTER or type command to continue

Is there any work around / fix to this issue?

I had the same issue today using Mason+Lspconfig+RustAnalyzer as installed by Mason. I tried rust-analyzer -V in the terminal and the output was error: Unknown binary 'rust-analyzer' in official toolchain 'stable-x86_64-apple-darwin'.

I’m not sure what the root cause of this error is, but following this Stack Overflow link, I tried

rustup component add rust-analyzer

I no longer had the terminal Unknown binary error or the buffer Invalid offset error in neovim. Could be some Mason issue.

not fixed on linux with neovim@0.12-dev and rust-analyzer@2025-06-14

I have the same issue, would be nice to have a fix for this pushed

I have no knowledge about neovim/mason, so I’d recommend someone who has the problem in here to open an issue regarding that for them (as I don’t believe anyone here has done so yet).

Right there shouldn’t be any backtraces, this is not a panic but a general error being bubbled up back to the client.

but I think this is something on neovims side, because in LSP every change has document id, and races like that should not happen.

It has to be, otherwise we’d observe this in other clients as well. Might be good to see if an older neovim doesn’t have this problem