Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
/ rls Public archive

Repository for the Rust Language Server (aka RLS)

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

rust-lang/rls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Oct 13, 2016
c60726e · Oct 13, 2016

History

92 Commits
Sep 15, 2016
Oct 10, 2016
Oct 13, 2016
Oct 13, 2016
Oct 12, 2016
Oct 13, 2016
Oct 10, 2016
Oct 7, 2016
Oct 13, 2016
Oct 13, 2016
Oct 5, 2016
Sep 5, 2016
Oct 12, 2016

Repository files navigation

Rust Language Service (RLS)

This project is in the early stages of development, it is not yet ready for real use. It will probably eat your laundry.

The RLS is provides a service that runs in the background, providing IDEs, editors, and other tools with information about Rust programs. It supports functionality such as 'goto definition', symbol search, reformatting, and code completion, and enables renaming and refactorings.

The RLS gets its source data from the compiler and from Racer. Where possible it uses data from the compiler which is precise and complete. Where its not possible, (for example for code completion and where building is too slow), it uses Racer.

Since the Rust compiler does not yet support end-to-end incremental compilation, we can't offer a perfect experience. However, by optimising our use of the compiler and falling back to Racer, we can offer a pretty good experience for small to medium sized crates. As the RLS and compiler evolve, we'll offer a better experience for larger and larger crates.

The RLS is designed to be frontend-independent. We hope it will be widely adopted by different editors and IDEs. To seed development, we provide a reference implementation of an RLS frontend for Visual Studio Code.

Building

Since the RLS is closely linked to the compiler and is in active development, you'll need a recent nightly compiler to build it.

Use cargo build to build.

Running

To run the RLS, you need to specify the sysroot as an environment variable (this should become unnecessary in the future). This is the route directory of your Rust installation. You can find the sysroot with rustc --print sysroot.

If you have installed Rust directly it will probably be /usr/local; if you are using a home-made compiler, it will be something like ~/rust/x86_64-unknown-linux-gnu/stage2; with Rustup it will change depending on the version of Rust being used, it should be something like ~/multirust/toolchain/nightly-x86_64-unknown-linux-gnu.

Run with:

SYS_ROOT=/usr/local cargo run

To run with VSCode, you'll need a recent version of that installed.

You'll then need a copy of our VSCode plugin. Assuming you'll be doing this for development, you probably don't want to install that plugin, but just open it in VSCode and then run it (F5).

It should all just work! You might need to make an edit and save before some of the features kick in (which is a bug - rust-lang/vscode-rust#3).

To work with the RLS, your project must be buildable using cargo build. If you use syntax extensions or build scripts, it is likely things will go wrong.

Testing

Test using RUST_TEST_THREADS=1 cargo test, however you must set SYS_ROOT as described above.

Testing is unfortunately minimal. There is support for regression tests, but not many actual tests exists yet. There is signifcant work to do before we have a comprehensive testing story.

Standard library support

Getting the RLS to work with the standard libraries takes a little more work, we hope to address this in the future for a more ergonomic solution (#9).

The way it works is that when the libraries are built, the compiler can emit all the data that the RLS needs. This can be read by the RLS on startup and used to provide things like type on hover without having access to the source code for the libraries.

The compiler gives every definition an id, and the RLS matches up these ids. In order for the RLS to work, the id of a identifier used in the IDE and the id of its declaration in a library must match exactly. Since ids are very unstable, the data used by the RLS for libraries must match exactly with the crate that your source code links with.

You need to generate the above data for the standard libraries if you want the RLS to know about them. Furthermore, you must do so for the exact version of the libraries which your code uses. The easiest (but certainly not the quickest) way to do this is to build the compiler and libraries from source, and use these libraries with your code.

In your Rust directory, you want to run the following:

# Or whatever -j you usually use.
RUSTFLAGS_STAGE2='-Zsave-analysis-api' make -j6

Then go get a coffee, possibly from a cafe on the other side of town if you have a slower machine.

If all goes well, you should have a bunch of JSON data in a directory like ~/rust/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib/save-analysis. You need to copy all those files (should be around 16) into libs/save-analysis in the root of your project directory (i.e., next to src and target).

Finally, to run the RLS you'll need to set things up to use the newly built compiler, something like:

export RUSTC="/home/ncameron/rust/x86_64-unknown-linux-gnu/stage2/bin/rustc"
export SYS_ROOT="/home/ncameron/rust/x86_64-unknown-linux-gnu/stage2"
cargo run

Yeah, sorry, it's quite the process, like I said we should be able to do better than this...

You'll also need to use the above script to run the RLS if you're making changes to the compiler which affect the RLS.

Implementation overview

The goal of the RLS project is to provide an awesome IDE experience now. That means not waiting for incremental compilation support in the compiler. However, Rust is a somewhat complex language to analyse and providing precise and complete information about programs requires using the compiler.

The RLS has two data sources - the compiler and Racer. The compiler is always right, and always precise. But can sometimes be too slow for IDEs. Racer is nearly always fast, but can't handle some constructs (e.g., macros) or can only handle them with limited precision (e.g., complex generic types).

The RLS tries to provide data using the compiler. It sets a time budget and queries both the compiler and Racer. If the compiler completes within the time budget, we use that data. If not, we use Racer's data.

We link both Racer and the compiler into the RLS, so we don't need to shell out to either (though see notes on the build process below). We also customise our use of the compiler (via standard APIs) so that we can read modified files directly from memory without saving them to disk.

Building

The RLS tracks changes to files, and keeps the changed file in memory (i.e., the RLS does not need the IDE to save a file before providing data). These changed files are tracked by the 'Virtual File System' (which is a bit of a grandiose name for a pretty simple file cache at the moment, but I expect this area to grow significantly in the future). The VFS is in a separate crate.

We want to start building before the user needs information (it would be too slow to start a build when data is requested). However, we don't want to start a build on every keystroke (this would be too heavy on user resources). Nor is there any point starting multiple builds when we would throw away the data from some of them. We therefore try to queue up and coalesce builds. This is further documented in src/build.rs.

When we do start a build, we may also need to build dependent crates. We therefore do a full cargo build. However, we do not compile the last crate (the one the user is editing in the IDE). We only run Cargo to get a command line to build that crate. Furthermore, we cache that command line, so for most builds (where we don't need to build dependent crates, and where we can be reasonably sure they haven't changed since a previous build) we don't run Cargo at all.

The command line we got from Cargo, we chop up and feed to the in-process compiler. We then collect error messages and analysis data in JSON format (although this is inefficient and should change).

Analysis data

From the compiler, we get a serialised dump of its analysis data (from name resolution and type checking). We combine data from all crates and the standard libraries and combine this into an index for the whole project. We cross- reference and store this data in HashMaps and use it to look up data for the IDE.

Reading, processing, and storing the analysis data is handled by the rls-analysis crate.

Communicating with IDEs

TODO LS protocol, http interface + plugin

Contributing

The RLS is open source and we'd love you to contribute to the project. Testing, reporting issues, writing documentation, writing tests, writing code, and implementing clients are all extremely valuable.

Here is the list of known issues. These are good issues to start on.

We're happy to help however we can. The best way to get help is either to leave a comment on an issue in this repo, or to ping us (nrc or jntrnr) in #rust-tools on IRC.

We'd love for existing and new tools to use the RLS. If that sounds interesting please get in touch by filing an issue or on IRC.