Skip to content

Conversation

@TpvdB
Copy link

@TpvdB TpvdB commented Sep 22, 2025

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.json in 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

  • I have never worked with C# before. I hope my code quality is sufficient, but realistically I cannot guarantee that I have not included any bad practices.
  • I have tested the features, they should handle all common (/possible?) errors fine, but I have not implemented test cases or unit tests for them.
  • Some code locations may be awkward, for example, I have placed logic for parsing arguments in the Options class. The general location seems logical, but it might be that the Options class is intended only as an interface and should not contain logic.
  • I may have implemented ShowErrorMessagesAsync in a way that does not fit the pattern used elsewhere.
  • Changing the options implemented requires reopening the project to take effect. I have mentioned in the RA Config options description that this is a requirement, but I have not implemented a popup or similar notification to alert the user.

Additional References

  • The main parsing logic is located in Options.cs.
  • Error handling and message display logic is placed in VsCommon.cs.

Images

  • New Settings image
  • Invalid JSON popup
image

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-analyzer directory 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!

Implements settings based parsing of config for rust analyzer (https://rust-analyzer.github.io/book/configuration.html)
Adds option to parse Rust Analyzer env vars
Adds optional logging for rust analyzer stderr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant