Environment variables needed by Rust code are not visible to `rust-analyzer` in VSCode
Categories
(Developer Infrastructure :: Developer Environment Integration, enhancement, P2)
Tracking
(firefox105 fixed)
Tracking | Status | |
---|---|---|
firefox105 | --- | fixed |
People
(Reporter: rkraesig, Assigned: nika)
References
(Blocks 1 open bug)
Details
Attachments
(3 files)
Normally, our make system sets up some environment variables before running cargo
on our code. Unfortunately, running mach ide vscode
doesn't do that, so viewing Rust files in VSCode causes rust-analyzer
(bug 1645020) to fail somewhat cryptically.
I was able to alleviate this locally by setting some environment variables in rust-analyzer.server.extraEnv
in settings.json
. It would be nice if mach ide vscode
could add those for me instead.
I'm not sure what constitutes the complete set of relevant environment variables, but RUSTC_BOOTSTRAP
and MOZ_TOPOBJDIR
, at least, seem like reasonable candidates for injection by mach
. (I also ended up adding BUILDCONFIG_RS
; but that's from build.rs
output, so there's probably a less explicit way to get that value to rust-analyzer
.)
Updated•3 years ago
|
Comment 1•2 years ago
|
||
I've been working around this issue by adding a task to tasks.json and running it manually to get rid of all the bogus errors:
{
"label": "mach cargo check",
"args": [
"cargo",
"check"
],
"problemMatcher": [
"$rustc"
]
}
But this requires shutting off check-on-save, which isn't ideal...
Assignee | ||
Comment 2•2 years ago
|
||
This switches the code to instead use the generated source file, which is
discovered based on OUT_DIR
so is easier for rust-analyzer to understand.
The configuration for rust-analyzer will be updated in the next part to
make sure that rust-analyzer is able to take advantage of these changes,
and produce better diagnostics.
Updated•2 years ago
|
Assignee | ||
Comment 3•2 years ago
|
||
In order to allow rust-analyzer to be able to use the build script in
the mozbuild crate to discover the configuration information, this patch
adds new flags to the vscode config to tell rust-analyzer to invoke
cargo through ./mach cargo check
, and use the correct target directory
within the objdir rather than $(topsrcdir)/target
.
Due to the virtual filesystem used by rust-analyzer not including files
in the object directory, this is not sufficient to get suggestions for
symbols from the included files, however it will accurately fetch
diagnostics upon save and run things like proc macros.
A new feature will likely need to be added to rust-analyzer to allow us
to specify additional paths to add to the source root for packages to
fix that issue.
Due to this change using ./mach cargo check
, rather than running it
independently, we don't run into issues caused by running check
against crates in the workspace which aren't being used, making the
diagnostics more useful.
An additional feature needed to be added to ./mach cargo check
to
allow specifying --message-format=json
. I am open to suggestions for a
more elegant way to communicate this flag into the makefile.
Depends on D153269
Assignee | ||
Comment 5•2 years ago
|
||
Updated•2 years ago
|
Comment 7•2 years ago
|
||
bugherder |
https://hg.mozilla.org/mozilla-central/rev/01d58d75edf5
https://hg.mozilla.org/mozilla-central/rev/e0f6a845537f
https://hg.mozilla.org/mozilla-central/rev/35761d5441bc
Updated•2 years ago
|
Description
•