Rust-analyzer and windows native x64

Hi,

Im struggling to get the rust-analyzer visual studio code plugin to work.

I have several crates in my workspace that depend on msvc c++ tools to build (wrapped c++ code). This means I must run rust-analyzer and cargo in a command promt / environment appropriately prepared e.g. "x64 Naitve Tools Command Prompt for VS 2022" as provided by the VS install.

For manual "cargo build" etc. that is fine since I have set visual studio to use that Native tools command prompt as the integrated terminal.

However, I seem unable to configure the rust-analyzer plugin to use that terminal, I have tried various things, the most promissing (but still failing) is to set:

"rust-analyzer.cargo.targetDir": true,
"rust-analyzer.server.path": "ra-ra.bat",

Where ra-ra.bat is a file on PATH with the following content only (needed since server.path seems not to accept arguments but a path to a binary only):

ra-ra.bat

python.exe C:\rust\bin\run-ra.py

And run-ra.py is a short ptyhon script to locate the latest version of the plugin and start the
the bundled rust-analyzer in a correct environment:

run-ra.py

from glob import glob
import subprocess
alt = list(glob("C:\vsc_extensions\rust-lang.rust-analyzer*"))
alt.sort()
ra_path = alt[-1] + "\server\rust-analyzer.exe"
args = ['cmd.exe', '/c',
'C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat', "&&", e]
result = subprocess.run(args)

I.e. starting rust-analyzer inside a cmd shell that loads the environemnt first.

The ra-ra.bat and run-ra.py files seem to work fine when I start them manually from a commandprompt, the native tools settings are loaded and rust-analyzer is started (and i can send messages to it from the commandline).

However, the rust-analyzer plugin in visual studio is unable to start it (non descirpt error message in the UI with "spawn EINVAL" and nothing under output in vs code for the language server)

Does anyone know if there is another / better way to ensure that the rust-analyzer plugin starts the rust-analyzer.exe binary in a shell where I can preload a .bat fle (effectively what the native tools from VS does)?

Regards

have you tried to start vscode by running code.exe from the vs devenv command prompt?

You could disect the vcvars script and look at which env vars you actually need, and then set those in either you workspace or global vscode settings for RA using "rust-analyzer.server.extraEnv": { /* ENV vars go here */ }.

Or you could write a wrapper script around vscode that loads the vcvars file before starting code.exe if you want the changes to apply to everything else in the editor as well

Thanks both, yes, running / starting it from the terminal works - but the downside with this is:

  1. that starting it from context menues etc will start it incorrectly (not so bad)
  2. If att any time that terminal window is closed - all codes will exit immediately (more of a problem)

Anyway - Thanks for the suggestion, its a workable work-around - just a bit surprised - I would have thought this to be a "common" challange on windows that you need the vs environemnt to compile dependencies.

In my case, I just pushed the relevant bits from the vcvars scripts to the back of my user PATH on my development machine. Some googling led me to a reddit post for starting a process detached from powershell, which would be perfect for a wrapper script