rust-analyzer: Seemingly incompatible design between rust-project.json and checkOnSave.overrideCommand
Hey all!
I have a couple of questions:
- Is it expected that in a code base where with Cargo you would have many
Cargo.tomlfiles spread across multiple folders, you only use onerust-project.json? - Is there a way to give some info on what file we want to “cargo check” (i.e. what file triggered the invocation of
checkOnSave.overrideCommand)? For example, passing it as an argument tocheckOnSave.overrideCommand.
My personal investigation has reached “yes, you should have only one rust-project.json” for 1.:
- in
rust-project.jsonyou reference deps with their index in the array for the samerust-project.jsonfile, and I don’t know how this could be compatible with multiplerust-project.jsons - trying to run
rust-analyzerwith multiplerust-project.jsons doesn’t work - e.g. crossrust-project.jsongoto def doesn’t work
and “no” for 2.:
- it seems that nothing dynamic is passed to the executable when it’s run (in the source code) - only the presupplied args and the environment from
checkOnSave.extraEnvare passed, which both appear to be static
This combination is very unfortunate, because now, there is no way for the tool that’s going to be used instead of cargo check to know what it should be building - in a huge repo, this is not viable.
I assume that when we’re using cargo this “what should I build” issue is resolved by the fact that cargo check is run in the root of the workspace, which is determined by a Cargo.toml file. However, in the rust-project.json, this is no longer a solution, because we can have only one rust-project.json file, hence in order to be correct, the only thing checkOnSave.overrideCommand could do is build everything in the workspace.
My proposed solutions are to do one of
- “fix” 2. so that we pass some info to
checkOnSave.overrideCommand(seems to be much easier) - “fix” 1. to allow autodiscovery and combining of
rust-project.jsonfiles - seems to be much more difficult to implement, but perhaps it’s a better approach in the long run, so that the assumptions made for cargo based projects are also valid forrust-project.jsonbased ones?
I would be happy to implement some version of “fix” 2., if we could come to an agreement on what that would be.
Context:
I’m trying to implement diagnostics support for rust-analyzer+rules_rust, which relies on checkOnSave.overrideCommand/cargo check.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 24 (14 by maintainers)
The agreement seems to be that the
$saved_filething might be accepted, as a stopgap solution, but there is no one working on that currently, afaik.Rustc actually creates these suggestions on it’s own. It exposes them in a machine readable way when using the json error format to allow other tools like
cargo fixand rust-analyzer to apply them verbatim. AFAIK rust-analyzer never feeds error messages generated by rustc back into any analysis or generates it’s own suggestions based on rustc error messages.This doesn’t really make sense though, what would r-a pass to bazel? If we only invoke the command once there is no dynamic data at play here, at which point you can just encode whatever your check command needs in the additional args.
Ah, so you want the file path of whatever file was saved. That would be something we could add as an interpolation var for
checkOnSave. To clarify https://github.com/rust-lang/rust-analyzer/pull/13128 technically only adds two things (it’s independent from interpolation in a sense),per_workspaceinvocations andonce_in_root. Usages of interpolated variables then may change additional things. So for this use case the idea would be for you to useonce_in_rootand an interpolation variable that expands to the file path being saved.A manifest file would be the
Cargo.tomlof the relevant workspace, I don’t really knowrust-project.jsontoo well but given it is there to not be dependent on cargo I would imagine the termmanifestto not mean anything there, so the var wouldn’t make sense forrust-project.jsonprojects.Thanks for clarifying these things, this is really helpful in figuring things out here 😃