Stream: t-compiler/rust-analyzer

Topic: How does rust-analyzer fetch dependencies?


Jason Axelson (Feb 07 2021 at 00:09, on Zulip):

Hi, I have a question as a fellow LanguageServer implementer, how does rust-analyzer fetch and manage dependencies?

oliver (Feb 07 2021 at 00:10, on Zulip):

through rustup I would assume

oliver (Feb 07 2021 at 00:10, on Zulip):

I would have to pay closer attention next time it updates though

Lukas Wirth (Feb 07 2021 at 00:15, on Zulip):

From what I recall it uses cargo metadata, this crate is the relevant part for that I believe https://github.com/rust-analyzer/rust-analyzer/tree/master/crates/project_model/src

Jason Axelson (Feb 07 2021 at 00:32, on Zulip):

hmmm, thanks for the link, although I'm unable to see where in the code it fetches dependencies. Does rust-analyzer avoid a race condition between when it is fetching dependencies and if the user is fetching dependencies?

oliver (Feb 07 2021 at 00:54, on Zulip):

which dependencies are you interested in?

Jonas Schievink [he/him] (Feb 07 2021 at 01:12, on Zulip):

Do you mean the actual download of dependencies / the index? That is handled by Cargo, which has a file lock on the corresponding directories, which should avoid any races.

Jason Axelson (Feb 07 2021 at 02:14, on Zulip):

Ah, I see. Cargo handling the race condition makes sense. (I'm not interested in any particular dependency, I'm interested in how dependencies are fetched in general)

Jason Axelson (Feb 07 2021 at 02:15, on Zulip):

Thanks! :smiley:

Jonas Schievink [he/him] (Feb 07 2021 at 02:59, on Zulip):

It does also use rustup to install the rust-src component

matklad (Feb 07 2021 at 15:05, on Zulip):

Hi @Jason Axelson !

I believe the best approach here is to let the build system do the heavy lifting. ie, the server asks where the deps are, and the build system provides the server with paths (maybe fetching some from the internet).

One problem here is that the user and the server might invoke the build system concurrently. Solution to that is just to make sure that the build system is concurrency-safe. I submitted some bugs and fixed for Cargo for this, back when I worked on IntelliJ.

Jason Axelson (Feb 08 2021 at 09:13, on Zulip):

Thanks for the info @matklad, solving it in the build system makes a lot of sense.

Last update: Jul 29 2021 at 08:30UTC