feat RA config & env var parsing #67
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Addressing Issue #48 and #45
Implements settings-based parsing of configuration for rust-analyzer (official configuration documentation).
Option to Parse RA Config
Options can be parsed from Settings under "Rust Analyzer > General > Rust Analyzer Config > Rust Analyzer LSP Initialization Options". Additionally, they can be customized per project by placing a JSON file named
lsp_initializationOptions.jsonin the project root directory. The two configurations are merged using a deep merge strategy as described here. I have chosen this strategy because in many cases it allows for nice extension of global configs for local projects. A shallow merge or just overwriting may, however, be more sensible in some cases where a deep merge means that an object filled in the global setting can't be overwritten by placing an empty object in the local JSON file ({test: "hi"}merged with{}results in{test: "hi"}). The JSON file as settings location seems a bit crude, but it is easy to work with and means that a common configuration for a project may be shared via git, which may be desirable in some cases.For the setting, I had to use a standard multiline string as VS2022 does not appear to provide a JSON Editor, and implementing one myself seemed excessive. Errors in the provided JSON are handled gracefully, and an error message box is shown to the user.
Option to Parse RA Process Environment Variables
Adds the option to parse Rust Analyzer environment variables through "Rust Analyzer > General > Rust Analyzer Environment Variables". These may be added as a list of space-separated environment variable arguments. If an argument contains an equals sign, the first equals sign is used to distinguish the name from the value; if no equals sign is present, the value is set to an empty string ("").
Stderr Logging
I needed to log rust-analyzer stderr output, so I have included a toggle to activate it if needed. It can also be found in the settings.
Caveats
Additional References
Options.cs.VsCommon.cs.Images
Solution to #45 and similar:
This issue seems to appear because RA uses different environment variables (or Rust flags(?)) than Cargo, which leads to both invalidating the cached builds of the other (according to this issue). The fix mentioned there is to specify the correct environment variables to RA (I can’t confirm that this works, but the provided changes to the extension now allow for that). Another, easier, although perhaps computationally a bit more wasteful, solution is to simply split up RA and Cargo build directories by providing a config like
{ "cargo": { "targetDir": true } }This makes rust-analyzer use the
<default_build_dir>/rust-analyzerdirectory as the target location and doesn’t require further diagnostics or testing to determine which environment variables, Rust flags, or other settings might be wrong.I will try my best to fix any problems with the code. Thank you for your extensive work on the extension, I really love being able to work with Rust in Visual Studio.
Have a great day!