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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

alibektas
Copy link
Member

@alibektas alibektas commented Apr 13, 2024

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 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 +1220
// 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
Collaborator

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
Collaborator

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
change.change_client_config(json);
let mut error_sink = ConfigError::default();
config = config.apply_change(change, &mut error_sink);
// FIXME @alibektas : What happens to errors without logging?
Copy link
Member

Choose a reason for hiding this comment

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

They get eaten, we should probably set up our tracing logging to error by default in these binaries

Copy link
Member Author

Choose a reason for hiding this comment

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

I am holding onto this one as I couldn't quite understand what I need to do.

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
Collaborator

bors commented Apr 21, 2024

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

@bors
Copy link
Collaborator

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
Collaborator

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

@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 29, 2024
@cbiffle
Copy link
Contributor

cbiffle commented May 2, 2024

Hi! I'm reading this with an eye toward enabling rust-analyzer in our complex cross-compilation builds in Hubris. I'm not very deep into it (and in particular need to look at your split of config keys into client/global/local) but one thing jumped out:

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

It would be really nice for this to be at least a warning. Otherwise I foresee frustration where we set a key and it silently doesn't take effect.

@cbiffle
Copy link
Contributor

cbiffle commented May 2, 2024

Caveat: I am not familiar with this codebase and have no idea how big of a request I am making. I'm also not a VScode user, and some of the comments in the code seem VScode-specific, so it's possible I'm not understanding things correctly.

It would be nice for rust-embedded use cases if the following things were available as crate-local rather than workspace-level settings; from my read of the current code they are global:

  • Control of features, in particular whether or not to pass --all-features -- for Cargo reasons, a lot of projects in the rust-embedded space use features to control processor variants, and cannot build with --all-features set. (This is not Hubris-specific.) While I assume your workspace-level feature specification allows for the cratename/featurename style of specification (since workspace-level features aren't a thing), it would be nice to specify these in crates themselves to avoid maintaining a big list at the root.
  • Target triple. It's possible the target triple can be set here already -- rust-analyzer using the word "target" to mean both "a thing that can be built" and "the target triple" means it's difficult for me, not being super familiar with the codebase, to tell which case is referring to which. But since Cargo still has no stable way of indicating a preference to build a crate for a specific triple, generated rust-analyzer client configurations tend to fill in for this rn, and this PR could eliminate that if it were configurable at a crate level.

I assume that sufficiently flexible/configurable clients are able to override all of the settings? (This seems important if config files are checked in but you want to do something different in a specific editor.)

@Veykril Veykril self-assigned this May 2, 2024
@Veykril
Copy link
Member

Veykril commented May 3, 2024

It would be really nice for this to be at least a warning. Otherwise I foresee frustration where we set a key and it silently doesn't take effect.

We ought to have that indeed thats a good point to raise.

Regarding the preference for crate-local configs, the ones you raised are certainly targets for those but they will require some more follow up work to this. This PR is mainly bringing the infra up that allows us to have these config files for now.

@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 May 13, 2024
@bors
Copy link
Collaborator

bors commented May 13, 2024

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

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.

rust-analyzer.toml
6 participants