Stream: general

Topic: rust-analyzer is awesome!


RalfJ (May 16 2020 at 12:54, on Zulip):

I finally tried vscode + rust-analyzer on the rustc codebase and I am completely blown away by how well this works. After a bit of loading I am getting ctrl-click-to-definition, show-type/doc-on-hover, and with a bit of configuration now also format-on-save. Kudos to everyone involved! <3

Is there documentation somewhere for the recommended settings when using this with rustc? So far I added llvm-project to the ignore list (so that it doesn't show up in Ctrl-P) and I added a +nightly argument to rustfmt. But maybe there is a way to make it use the bootstrap compiler (currently there's a "failure" in librustc_index, I think caused by using the wrong compiler).

LeSeulArtichaut (May 16 2020 at 13:16, on Zulip):

I saw a discussion about using rust-analyser for rustc few days ago... I think you want to override the cargo check command with ./x.py check, and override the formatter command with ./build/target/stage0/bin/rustfmt or something alike.

LeSeulArtichaut (May 16 2020 at 13:18, on Zulip):

See this conversation

RalfJ (May 16 2020 at 14:12, on Zulip):

hm, setting the formatter like that lead to weird errors... "error[internal]: left behind trailing whitespace"

RalfJ (May 16 2020 at 14:13, on Zulip):

oh that might be older messages (there's no time)

RalfJ (May 16 2020 at 14:13, on Zulip):

the latest one is

[Error - 4:12:59 PM] Request textDocument/formatting failed.
  Message: No such file or directory (os error 2)
  Code: -32603
RalfJ (May 16 2020 at 14:13, on Zulip):

oh d'oh I have to replace "target" by something

RalfJ (May 16 2020 at 14:14, on Zulip):

hm still no luck though

Charles Lew (May 16 2020 at 14:32, on Zulip):

maybe try ./x.py fmt as formatter command?

RalfJ (May 16 2020 at 14:34, on Zulip):

that format the entire tree, not just a single crate/file

RalfJ (May 16 2020 at 14:34, on Zulip):

so it takes a while

RalfJ (May 16 2020 at 14:35, on Zulip):

I doubt that matches what format-on-save usually does

RalfJ (May 16 2020 at 14:36, on Zulip):

so far I got this:

{
    "editor.formatOnSave": true,
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./x.py",
        "check",
        "--json-output"
    ],
    "rust-analyzer.rustfmt.extraArgs": [
        "+nightly"
    ],
    "files.exclude": {
        "**/llvm-project/**": true
    }
}
Jonas Schievink (May 16 2020 at 14:38, on Zulip):

Which rustdoc is invoked by x.py fmt? build/x86_64-unknown-linux-gnu/stage0/bin/rustdoc?

simulacrum (May 16 2020 at 14:41, on Zulip):

yes

Jonas Schievink (May 16 2020 at 15:33, on Zulip):

Err, rustfmt, not rustdoc

simulacrum (May 16 2020 at 15:34, on Zulip):

also yes

simulacrum (May 16 2020 at 15:34, on Zulip):

I missed the rustdoc bit :)

Jonas Schievink (May 16 2020 at 15:35, on Zulip):

Hmm, r-a does not seem to find that

[Error - 5:34:04 PM] Request textDocument/formatting failed.
Message: No such file or directory (os error 2)
Code: -32603

Jonas Schievink (May 16 2020 at 15:36, on Zulip):

That is with:

    "rust-analyzer.rustfmt.overrideCommand": [
        "./build/x86_64-unknown-linux-gnu/stage0/bin/rustfmt"
    ],

It clearly is there and seems to work though

Jonas Schievink (May 16 2020 at 15:39, on Zulip):

Ah, looks like it adjusts the working dir dynamically, so it can't find the command with a relative path

https://github.com/rust-analyzer/rust-analyzer/blob/d51c1f62178c383363a2d95e865131d9a7b969d0/crates/rust-analyzer/src/main_loop/handlers.rs#L651-L655

Specifying an absolute path works

ecstatic-morse (May 16 2020 at 16:04, on Zulip):

Jonas Schievink said:

Ah, looks like it adjusts the working dir dynamically, so it can't find the command with a relative path

https://github.com/rust-analyzer/rust-analyzer/blob/d51c1f62178c383363a2d95e865131d9a7b969d0/crates/rust-analyzer/src/main_loop/handlers.rs#L651-L655

Specifying an absolute path works

Literally was just scratching my had about this.

ecstatic-morse (May 16 2020 at 16:05, on Zulip):

@Jonas Schievink Do you want to fix this/submit an issue? Otherwise I'll do it.

Jonas Schievink (May 16 2020 at 16:07, on Zulip):

You mean on rust-analyzer? It seems to be doing this on purpose

simulacrum (May 16 2020 at 16:07, on Zulip):

I think it might be necessary to configure the working directory for some of rustfmt's features to work

simulacrum (May 16 2020 at 16:08, on Zulip):

in particular iirc rustfmt does support "crate wide" formatting

simulacrum (May 16 2020 at 16:08, on Zulip):

or perhaps it'll fail to parse the file without this if the file contains include_str! with a relative path, for example

ecstatic-morse (May 16 2020 at 16:09, on Zulip):

I was thinking you would first normalize the path to the workspace directory when you get a RustfmtConfig::CustomCommand

ecstatic-morse (May 16 2020 at 16:09, on Zulip):

Not that you would stop setting current_dir

Jonas Schievink (May 16 2020 at 16:10, on Zulip):

Yeah that could work here. What happens when you normalize a path like rustfmt, without any other elements? Does it stay as-is?

simulacrum (May 16 2020 at 16:11, on Zulip):

hm normalizing would definitely leave it is as is, not sure what canonicalization does

simulacrum (May 16 2020 at 16:11, on Zulip):

it might try to resolve with respect to current directory of the process?

ecstatic-morse (May 16 2020 at 16:14, on Zulip):

You would need to look for "path-like" things, much like the shell does (hence why you have to write ./x.py and not x.py)

ecstatic-morse (May 16 2020 at 16:30, on Zulip):

I'll submit a PR over there.

RalfJ (May 16 2020 at 17:18, on Zulip):

having to use absolute paths would be a shame as then I couldnt share the settings between my 3 rustc worktrees any more

RalfJ (May 16 2020 at 17:18, on Zulip):

so thanks for bringing this up upstream :)

simulacrum (May 16 2020 at 20:14, on Zulip):

Hm well we update the rustfmt version very rarely - I think twice so far? - so you probably could

RalfJ (May 17 2020 at 08:27, on Zulip):

in that case rustfmt +nightly will probably also do it I guess?^^
I had to disable format-on-save now though because it also formats the test suite, breaking some tests...

RalfJ (May 17 2020 at 09:00, on Zulip):

Also it keeps telling me that there are too many files to be watched. are there some other folders besides llvm-project and build that I could exclude?

bjorn3 (May 17 2020 at 09:32, on Zulip):

You could exclude src/doc and the things in src/tools that you don't care about. Another big contributor is the src/test dir.

Last update: Jun 05 2020 at 22:20UTC