Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TOML based config for rust-analyzer #17058

Merged
merged 9 commits into from
Jun 7, 2024

Conversation

alibektas
Copy link
Member

@alibektas alibektas commented Apr 13, 2024

Important

We don't promise any stability with this feature yet, any configs exposed may be removed again, the grouping may change etc.

TOML Based Config for RA

This PR ( addresses #13529 and this is a follow-up PR on #16639 ) makes rust-analyzer configurable by configuration files called rust-analyzer.toml. Files must be named rust-analyzer.toml. There is not a strict rule regarding where the files should be placed, but it is recommended to put them near a file that triggers server to start (i.e., Cargo.{toml,lock}, rust-project.json).

Configuration Types

Previous configuration keys are now split into three different classes.

  1. Client keys: These keys only make sense when set by the client (e.g., by setting them in settings.json in VSCode). They are but a small portion of this list. One such example is rust_analyzer.files_watcher, based on which either the client or the server will be responsible for watching for changes made to project files.
  2. Global keys: These keys apply to the entire workspace and can only be set on the very top layers of the hierarchy. The next section gives instructions on which layers these are.
  3. Workspace keys: These keys apply to a single workspace (TBD).
  4. Local keys: Keys that can be changed for each crate if desired.

How Am I Supposed To Know If A Config Is Gl/Loc/Cl ?

#17101

Configuration Hierarchy

There are 5 levels in the configuration hierarchy. When a key is searched for, it is searched in a bottom-up depth-first fashion.

Default Configuration

Scope: Global, Local, and Client

This is a hard-coded set of configurations. When a configuration key could not be found, then its default value applies.

User configuration

Scope: Global, Local

If you want your configurations to apply to every project you have, you can do so by setting them in your $CONFIG_DIR/rust-analyzer/rust-analyzer.toml file, where $CONFIG_DIR is :

Platform Value Example
Linux $XDG_CONFIG_HOME or $HOME/.config /home/alice/.config
macOS $HOME/Library/Application Support /Users/Alice/Library/Application Support
Windows {FOLDERID_RoamingAppData} C:\Users\Alice\AppData\Roaming

Client configuration

Scope: Global, Local, and Client

Previously, the only way to configure rust-analyzer was to configure it from the settings of the Client you are using. This level corresponds to that.

With this PR, you don't need to port anything to benefit from new features. You can continue to use your old settings as they are.

Workspace Configuration

Scope: Global, Workspace, Local

A rust-analyzer.toml file that is located at the root of your workspace can be used to configure all of the mentioned scopes. A workspace scope does in practice not exist but will soon be implemented and these files will soon not have a global scope but rather only workspace and local.

Local Configuration

Scope: Local

You can also configure rust-analyzer on a crate level. Although it is not an error to define global ( or client ) level keys in such files, they won't be taken into consideration by the server. Defined local keys will affect the crate in which they are defined and crate's descendants. Internally, a Rust project is split into what we call SourceRoots. This, although with exceptions, is equal to splitting a project into crates.

You may choose to have more than one rust-analyzer.toml files within a SourceRoot, but among them, the one closer to the project root will be

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 13, 2024
@alibektas alibektas changed the title feature: TOML based config for rust-analyzer feat: TOML based config for rust-analyzer Apr 13, 2024
Copy link
Contributor

@davidbarsky davidbarsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some nits; i'll try to review more later.

crates/rust-analyzer/tests/slow-tests/main.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/tests/slow-tests/main.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/reload.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/mem_docs.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/main_loop.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/config.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/config.rs Outdated Show resolved Hide resolved
@@ -827,16 +1177,45 @@ impl Config {
caps: ClientCapabilities,
workspace_roots: Vec<AbsPathBuf>,
visual_studio_code_version: Option<Version>,
user_config_path: Option<Utf8PathBuf>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: are there non-user config paths? i would just rename all usage here to config_path, IMO.

(i see a "root config path", but I'd remove the user qualifier entirely.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the user here refers to the user's home dir I think

Copy link
Member Author

@alibektas alibektas Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah naming things is not my strength. I though of XDG_CONFIG_.. but that is also not very accurate either. As a matter of fact it was Veykril's suggestion to change it to user_config. In any case the name is still the same but I added comment.

Comment on lines 1210 to 1225
client_config: ClientConfig::default(),
user_config: None,
ratoml_files: FxHashMap::default(),
default_config: ConfigData::default(),
source_root_parent_map: Arc::new(FxHashMap::default()),
user_config_path,
root_ratoml: None,
root_ratoml_path,
should_update: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do two things:

  1. keep ConfigData named data instead of default_config.
  2. I'd group all the configuration file-related values into their own wrapper struct.

Comment on lines +1303 to +1226
// FIXME @alibektas : This is the only config that is confusing. If it's a proper configuration
// why is it not among the others? If it's client only which I doubt it is current state should be alright
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's weird because the VS Code extension, by default, cares about what folder rust-analyzer is running in. it's debatable whether it makes sense in this context, but I'm inclined to think it does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed a very odd config and only necessary (I hope) because we are lacking proper support for handling detached files currently.

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not a complete review yet, will give the previous PR another pass and merge that first (then adjust things as I see to speed up things, instead of doing more review rounds on that)

crates/rust-analyzer/src/cli/scip.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/mem_docs.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/config.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/config.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Apr 16, 2024

☔ The latest upstream changes (presumably #16639) made this pull request unmergeable. Please resolve the merge conflicts.

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 18, 2024
@alibektas alibektas added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 18, 2024
@bors
Copy link
Contributor

bors commented Apr 19, 2024

☔ The latest upstream changes (presumably #17110) made this pull request unmergeable. Please resolve the merge conflicts.

crates/paths/src/lib.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/cli/scip.rs Show resolved Hide resolved
crates/rust-analyzer/tests/slow-tests/support.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/lib.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/main_loop.rs Show resolved Hide resolved
crates/rust-analyzer/src/config.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Apr 21, 2024

☔ The latest upstream changes (presumably #17118) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Apr 29, 2024

☔ The latest upstream changes (presumably #17157) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 more things otherwise I think this is good to go

crates/rust-analyzer/src/config.rs Outdated Show resolved Hide resolved
@Veykril
Copy link
Member

Veykril commented Apr 29, 2024

@bors delegate+

@bors
Copy link
Contributor

bors commented Apr 29, 2024

✌️ @alibektas, you can now approve this pull request!

If @Veykril told you to "r=me" after making some further change, please make that change, then do @bors r=@Veykril

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 29, 2024
@alibektas
Copy link
Member Author

alibektas commented Apr 29, 2024

My splitting the config keys into global/local/client wasn't really meticulous, because I was thinking that you would intervene at some point 😄. Are we planning to this in the near future or do you want to take a look before I actually merge this PR @Veykril ?

@Veykril
Copy link
Member

Veykril commented Apr 29, 2024

I haven't given the split a look yet tbf, I can do that afterwards / once you've fixed up the rest here

@Veykril
Copy link
Member

Veykril commented Jun 5, 2024

Okay diagnostics work now, though the UX is horrible for now and this generally needs some cleanup that I'll task @alibektas with as a follow up. It also seems like the server hangs up when you create a fresh toml file so I'll investigate that later

@Veykril
Copy link
Member

Veykril commented Jun 6, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Jun 6, 2024

📌 Commit 3178e5f has been approved by Veykril

It is now in the queue for this repository.

bors added a commit that referenced this pull request Jun 6, 2024
feat: TOML based config for rust-analyzer

# TOML Based Config for RA

This PR ( fixes #13529 and this is a follow-up PR on #16639 ) makes rust-analyzer configurable by configuration files called `rust-analyzer.toml`. Files **must** be named `rust-analyzer.toml`. There is not a strict rule regarding where the files should be placed, but it is recommended to put them near a file that triggers server to start (i.e., `Cargo.{toml,lock}`, `rust-project.json`).

## Configuration Types

Previous configuration keys are now split into three different classes.

1. Client keys: These keys only make sense when set by the client (e.g., by setting them in `settings.json` in VSCode). They are but a small portion of this list. One such example is `rust_analyzer.files_watcher`, based on which either the client or the server will be responsible for watching for changes made to project files.
2. Global keys: These keys apply to the entire workspace and can only be set on the very top layers of the hierarchy. The next section gives instructions on which layers these are.
3. Local keys: Keys that can be changed for each crate if desired.

### How Am I Supposed To Know If A Config Is Gl/Loc/Cl ?

#17101

## Configuration Hierarchy

There are 5 levels in the configuration hierarchy. When a key is searched for, it is searched in a bottom-up depth-first fashion.

### Default Configuration

**Scope**: Global, Local, and Client

This is a hard-coded set of configurations. When a configuration key could not be found, then its default value applies.

### User configuration

**Scope**: Global, Local

If you want your configurations to apply to **every** project you have, you can do so by setting them in your `$CONFIG_DIR/rust-analyzer/rust-analyzer.toml` file, where `$CONFIG_DIR` is :

| Platform | Value                                 | Example                                  |
| ------- | ------------------------------------- | ---------------------------------------- |
| Linux   | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config                      |
| macOS   | `$HOME`/Library/Application Support   | /Users/Alice/Library/Application Support |
| Windows | `{FOLDERID_RoamingAppData}`           | C:\Users\Alice\AppData\Roaming           |

### Client configuration

**Scope**: Global, Local, and Client

Previously, the only way to configure rust-analyzer was to configure it from the settings of the Client you are using. This level corresponds to that.

> With this PR, you don't need to port anything to benefit from new features. You can continue to use your old settings as they are.

### Workspace Root Configuration

**Scope**: Global, Local

Rust-analyzer already used the path of the workspace you opened in your Client. We used this information to create a configuration file that won't affect your other projects and define global level configurations at the same time.

### Local Configuration

**Scope**: Local

You can also configure rust-analyzer on a crate level. Although it is not an error to define global ( or client ) level keys in such files, they won't be taken into consideration by the server. Defined local keys will affect the crate in which they are defined and crate's descendants. Internally, a Rust project is split into what we call `SourceRoot`s. This, although with exceptions, is equal to splitting a project into crates.

> You may choose to have more than one `rust-analyzer.toml` files within a `SourceRoot`, but among them, the one closer to the project root will be
@bors
Copy link
Contributor

bors commented Jun 6, 2024

⌛ Testing commit 3178e5f with merge 6e427d8...

@bors
Copy link
Contributor

bors commented Jun 6, 2024

💔 Test failed - checks-actions

@Veykril Veykril force-pushed the 13529/ratoml branch 2 times, most recently from 4c2b832 to 3178e5f Compare June 7, 2024 10:23
@Veykril Veykril mentioned this pull request Jun 7, 2024
5 tasks
@Veykril
Copy link
Member

Veykril commented Jun 7, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Jun 7, 2024

📌 Commit 5a7bf1d has been approved by Veykril

It is now in the queue for this repository.

@Veykril
Copy link
Member

Veykril commented Jun 7, 2024

Quoting my comment on #13529 for visibility:

#17058 will implement the core features here, there are still some issues with it (the user config is not working correctly yet), as well some restrictions with crate specific configs due to architecture reasons (they shouldn't really be noticeably in most cases). A lot of configs are also still missing from it and the diagnostics UX (when an invalid config is supplied) is really bad. All of that will be resolved in the near or far future (time will tell).

Important

We don't promise any stability with this feature yet, any configs exposed may be removed again, the ordering may change etc.

Once the PR lands (should today), this issue will be used to track any updates on the feature.

@bors
Copy link
Contributor

bors commented Jun 7, 2024

⌛ Testing commit 5a7bf1d with merge 7c5d496...

@bors
Copy link
Contributor

bors commented Jun 7, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 7c5d496 to master...

@bors bors merged commit 7c5d496 into rust-lang:master Jun 7, 2024
11 checks passed
bors added a commit that referenced this pull request Jul 23, 2024
minor : fixes for ratoml module

This is a follow-up PR to #17058.

- Parse errors are reflected as such by defining a new variant called `ConfigError::ParseError`
- New error collection has been added to store config level agnostic errors.

EDIT : Some things that this PR promised to solve are removed and will be addressed by other PRs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants