Stream: t-compiler/rust-analyzer

Topic: rust-analyzer in nix

Albert Cervin (May 04 2020 at 17:57, on Zulip):

Hi, I made a little "hack" nix derivation that essentially downloads a github release of rust-analyzer. In the nix env I already have rust-src and I am then using coc.nvim with a super simple config but I do not get any completions on std? Any ideas what I can try?

Albert Cervin (May 04 2020 at 18:04, on Zulip):

Currently using it without an extension like

 "rust-analyzer": {
            "command": "rust-analyzer",
            "filetypes": ["rust"],
            "rootPatterns": ["Cargo.toml"]
Timo Freiberg (May 04 2020 at 18:09, on Zulip):

i don't know why that setup doesn't work, but i can recommend coc-rust-analyzer

Albert Cervin (May 04 2020 at 18:11, on Zulip):

Yeah, I have seen that one and will most likely try it but I would like to get a simpler case working first :)

Albert Cervin (May 04 2020 at 18:11, on Zulip):

Another error I get is this one

[Error  - 7:39:31 PM] Request textDocument/documentHighlight failed.
  Message: content modified
  Code: -32801
Albert Cervin (May 04 2020 at 18:13, on Zulip):

Is there some method to debug where it tries to find source?

bjorn3 (May 04 2020 at 18:16, on Zulip):

That error isn't really an error. It is just that you edited the document while it was processing a request.

Albert Cervin (May 04 2020 at 18:18, on Zulip):

Ah, makes sense. But what about not getting completions on std? This is what I get from tracing:

[Trace - 8:16:54 PM] Sending request 'textDocument/completion - (6)'.
Params: {
    "textDocument": {
        "uri": "file:///Users/abbe/code/firm/services/avery/src/"
    "position": {
        "line": 5,
        "character": 9
    "context": {
        "triggerKind": 1

[Trace - 8:16:54 PM] Received response 'textDocument/completion - (6)' in 2ms.
Result: [
        "additionalTextEdits": [],
        "deprecated": false,
        "filterText": "self",
        "insertTextFormat": 1,
        "kind": 14,
        "label": "self",
        "textEdit": {
            "newText": "self",
            "range": {
                "end": {
                    "character": 9,
                    "line": 5
                "start": {
                    "character": 9,
                    "line": 5
        "additionalTextEdits": [],
        "deprecated": false,
        "filterText": "super",
        "insertTextFormat": 1,
        "kind": 14,
        "label": "super",
        "textEdit": {
            "newText": "super::",
            "range": {
                "end": {
                    "character": 9,
                    "line": 5
                "start": {
                    "character": 9,
                    "line": 5

This is after typing std::

bjorn3 (May 04 2020 at 18:20, on Zulip):

I think rust-analyzer requires rust-src to be in the sysroot of rustc at lib/rustlib/src/rust/src.

bjorn3 (May 04 2020 at 18:21, on Zulip):

What is the result of ls $(rustc --print sysroot)/lib/rustlib/src/rust/src/?

Albert Cervin (May 04 2020 at 18:22, on Zulip):
ls $(rustc --print sysroot)/lib/rustlib/src/rust/src/
build_helper  libcore         libpanic_unwind  libprofiler_builtins  libterm  libunwind  tools
liballoc      libpanic_abort  libproc_macro    libstd                libtest  stdarch
bjorn3 (May 04 2020 at 18:23, on Zulip):

That is what it should be. :thinking:

Albert Cervin (May 04 2020 at 18:25, on Zulip):

Yeah, that is what I thought as well, and rls completions work :)

Albert Cervin (May 04 2020 at 18:26, on Zulip):

And I do get completion on other external crates

Albert Cervin (May 04 2020 at 18:27, on Zulip):

Is there any settings I could try (Is there a list of LSP settings somewhere?)?

Edwin Cheng (May 04 2020 at 18:33, on Zulip):

"RUST_SRC_PATH" overrided in editor ?

Albert Cervin (May 04 2020 at 18:34, on Zulip):

Nope, the above command gives the same output inside of Vim

Edwin Cheng (May 04 2020 at 18:38, on Zulip):

There is a config setting in RA:

"rust-analyzer.withSysroot": true

but it is true by default.

Albert Cervin (May 04 2020 at 20:43, on Zulip):

Yeah, setting that did not really change anything :)

matklad (May 05 2020 at 08:39, on Zulip):

rust-analyzeri is also in nixpgs already:

That specific version might be old, but it should be possible to overrideDerivation / overrideAttrs to get the more recent one

Albert Cervin (May 06 2020 at 11:35, on Zulip):

Yeah and that is super cool but for this project we are using a pinned downed version of 20.03 for nixpkgs where it is not yet present :)

Albert Cervin (May 06 2020 at 11:35, on Zulip):

But guess I could try it and see if it makes a difference

Albert Cervin (May 12 2020 at 20:44, on Zulip):

Hmm... it _does_ work when using the one in nixpkgs

Albert Cervin (May 12 2020 at 20:59, on Zulip):

I know what is going on now... The version in nixpkgs is built with a wrapper script hardcoding RUST_SRC_PATH whereas the derivation I created relied on rustc --print sysroot. Seems like maybe rust-analyzer cannot find rustc (it is added to path by nix-shell) to run the rustc --print sysroot command (as seen above, it works when run in shell)

Albert Cervin (May 12 2020 at 21:00, on Zulip):

As a temp workaround I guess I can set RUST_SRC_PATH (or maybe RUSTC ?) in the derivation

Albert Cervin (May 13 2020 at 16:43, on Zulip):

Ok, I did some more digging into this... Both rust-analyzers (my derivation and the one from nixpgs) fail to give std completions if I use a $RUST_SRC_PATH that looks like this (this is from rustup):

$ ls /nix/store/bz2siw7ip2hin4yjgy7biw2fm38b4vwd-rust-1.43.1-2020-05-04-8d69840ab/lib/rustlib/src/rust/src
build_helper  liballoc  libcore  libpanic_abort  libpanic_unwind  libproc_macro  libprofiler_builtins  libstd  libterm  libtest  libunwind  stdarch  tools

However, both rust-analyzers work as expected if I instead give them a $RUST_SRC_PATH that looks like this (this one is from the rust-analyzer derivation in nixpkgs):

$ ls /nix/store/a2071dn4mhsyhp6ql70rj8wbgvrdq9vc-rust-src     libcore          libproc_macro          librustc_ast_passes      librustc_data_structures  librustc_feature      librustc_interface  librustc_metadata   librustc_plugin_impl    librustc_span    librustdoc    libtest
bootstrap     libfmt_macros    libprofiler_builtins   librustc_builtin_macros  librustc_driver           librustc_fs_util      librustc_lexer      librustc_mir        librustc_privacy        librustc_target  libserialize  libunwind
build_helper  libgraphviz      librustc               librustc_codegen_llvm    librustc_error_codes      librustc_hir          librustc_lint       librustc_mir_build  librustc_resolve        librustc_traits  libstd        rustc
liballoc      libpanic_abort   librustc_apfloat       librustc_codegen_ssa     librustc_errors           librustc_incremental  librustc_llvm       librustc_parse      librustc_save_analysis  librustc_ty      libsyntax     stage0.txt
libarena      libpanic_unwind  librustc_ast_lowering  librustc_codegen_utils   librustc_expand           librustc_index        librustc_macros     librustc_passes     librustc_session        librustc_typeck  libterm

Any ideas @matklad ?

matklad (May 13 2020 at 17:04, on Zulip):

We use RUST_SRC_PATH as a substitue for --pirnt sysroot:

Albert Cervin (May 13 2020 at 18:57, on Zulip):

Yeah I have seen that but the only difference between the working case and the mon-working one is the content of what is pointed to by RUST_SRC_PATH as stated above

Albert Cervin (May 13 2020 at 19:00, on Zulip):

I.e. it works if I point it at the src folder of a git clone of Rust but it does not if I point it at a rustup installed rust-src

Albert Cervin (May 13 2020 at 20:07, on Zulip):

Plot thickens, I now have one rust-src installed with rustup and one rust-src installed with the mozilla nix overlay. It works with the rustup-installed one but not with the nix overlay installed one and diffing the source folders, they are identical!

Albert Cervin (May 13 2020 at 20:07, on Zulip):

(except for some shell shebangs)

Albert Cervin (May 13 2020 at 20:10, on Zulip):

is there something else that is used in sysroot?

Albert Cervin (May 13 2020 at 20:40, on Zulip):

AH, found it!! There are symlinks inside the nix-packages with rust-src which rust-analyzer does not resolve!

Albert Cervin (May 13 2020 at 20:42, on Zulip):

Is it expected that rust-analyzer would resolve symlinks?

Albert Cervin (May 13 2020 at 20:58, on Zulip):

rls seems to FWIW

matklad (May 13 2020 at 21:23, on Zulip):

yeah, we currently ignore symlink

Albert Cervin (May 14 2020 at 06:30, on Zulip):

I have a working workaround now! Thanks for all the help!

Last update: Jul 29 2021 at 21:15UTC