Skip to content

Commit 32be158

Browse files
committed
Auto merge of rust-lang#13941 - matklad:rpj, r=Veykril
internal: explain the idea behind rust-project.json
2 parents a119352 + bd35008 commit 32be158

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

crates/project-model/src/project_json.rs

+44
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,50 @@
44
//! idea here is that people who do not use Cargo, can instead teach their build
55
//! system to generate `rust-project.json` which can be ingested by
66
//! rust-analyzer.
7+
//!
8+
//! This short file is a somewhat big conceptual piece of the architecture of
9+
//! rust-analyzer, so it's worth elaborating on the underlying ideas and
10+
//! motivation.
11+
//!
12+
//! For rust-analyzer to function, it needs some information about the project.
13+
//! Specifically, it maintains an in-memory data structure which lists all the
14+
//! crates (compilation units) and dependencies between them. This is necessary
15+
//! a global singleton, as we do want, eg, find usages to always search across
16+
//! the whole project, rather than just in the "current" crate.
17+
//!
18+
//! Normally, we get this "crate graph" by calling `cargo metadata
19+
//! --message-format=json` for each cargo workspace and merging results. This
20+
//! works for your typical cargo project, but breaks down for large folks who
21+
//! have a monorepo with an infinite amount of Rust code which is built with bazel or
22+
//! some such.
23+
//!
24+
//! To support this use case, we need to make _something_ configurable. To avoid
25+
//! a [midlayer mistake](https://lwn.net/Articles/336262/), we allow configuring
26+
//! the lowest possible layer. `ProjectJson` is essentially a hook to just set
27+
//! that global singleton in-memory data structure. It is optimized for power,
28+
//! not for convenience (you'd be using cargo anyway if you wanted nice things,
29+
//! right? :)
30+
//!
31+
//! `rust-project.json` also isn't necessary a file. Architecturally, we support
32+
//! any convenient way to specify this data, which today is:
33+
//!
34+
//! * file on disk
35+
//! * a field in the config (ie, you can send a JSON request with the contents
36+
//! of rust-project.json to rust-analyzer, no need to write anything to disk)
37+
//!
38+
//! Another possible thing we don't do today, but which would be totally valid,
39+
//! is to add an extension point to VS Code extension to register custom
40+
//! project.
41+
//!
42+
//! In general, it is assumed that if you are going to use `rust-project.json`,
43+
//! you'd write a fair bit of custom code gluing your build system to ra through
44+
//! this JSON format. This logic can take form of a VS Code extension, or a
45+
//! proxy process which injects data into "configure" LSP request, or maybe just
46+
//! a simple build system rule to generate the file.
47+
//!
48+
//! In particular, the logic for lazily loading parts of the monorepo as the
49+
//! user explores them belongs to that extension (it's totally valid to change
50+
//! rust-project.json over time via configuration request!)
751
852
use std::path::PathBuf;
953

0 commit comments

Comments
 (0)