Closed Bug 1759555 Opened 3 years ago Closed 2 years ago

Environment variables needed by Rust code are not visible to `rust-analyzer` in VSCode

Categories

(Developer Infrastructure :: Developer Environment Integration, enhancement, P2)

enhancement

Tracking

(firefox105 fixed)

RESOLVED FIXED
105 Branch
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.)

Severity: -- → S2
Priority: -- → P2

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

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.

Assignee: nobody → nika
Status: NEW → ASSIGNED

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

Pushed by nlayzell@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/01d58d75edf5 Part 1: Avoid using env! for includes in the mozbuild crate, r=glandium https://hg.mozilla.org/integration/autoland/rev/e0f6a845537f Part 2: Partially configure rust-analyzer for ./mach ide vscode, r=glandium,andi
Attachment #9289038 - Attachment description: WIP: Bug 1759555 - Part 3: Wrap lines in generate_buildconfig.py earlier → Bug 1759555 - Part 3: Wrap lines in generate_buildconfig.py earlier
Pushed by imoraru@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/35761d5441bc Part 3: Wrap lines in generate_buildconfig.py earlier
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 105 Branch
Product: Firefox Build System → Developer Infrastructure
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: