Preventing Rust Analyzer Crash from Build Script Panics

Hello Rust community,

I’m encountering an issue while trying to set an environment variable during compile time using a command like FOO=BAR cargo build using a build script. My build script looks like this:

fn main() {
        std::env::var("FOO").expect("Error: Failed to get `FOO` environment variable")

However, whenever Rust Analyzer attempts to build, the build script panics due to the missing environment variable. This causes Rust Analyzer to stop working, displaying the following error:

2024-02-13T16:53:47.296528Z ERROR rust_analyzer::main_loop: FetchBuildDataError:
error: failed to run custom build command for `demo v0.1.0 (/home/xyz/demo)`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.

Caused by:
  process didn't exit successfully: `/home/xyz/demo/target/debug/build/demo-a948a56f58b797ec/build-script-build` (exit status: 101)
  --- stdout

  --- stderr
  thread 'main' panicked at
  Error: Failed to get `FOO` environment variable: NotPresent

How to fix this issue or any suggestions would be greatly appreciated.

Looks like you want to set the rust-analyzer.cargo.extraEnv config option, however your editor wants you to do that.

(I just searched the manual for "environment".)

1 Like

Thanks for the reply! I wanted to set the environment variable through the command line like FOO=BAR cargo build. And also I wanted project-based configuration.

Edit: Apologies for the confusion. I mistakenly assumed that Rust Analyzer would handle setting the environment variable for the build process. Additionally, I was under the impression that there might be alternative methods to achieve this, as setting the environment for every different project seems cumbersome.

Do you even need the build script? Seems redundant to set FOO to the value of FOO, which you can access with env! in your code? Or am I misunderstanding something?

1 Like

Well, I wanted to compile it into the binary by providing the environment variable at build time.

Thats' exactly what env! does, no?

If I don't use a build script to print the environment variables to the output, I get a diagnostic error like environment variable FOO not defined at compile time. You can use std::env::var("FOO") to read the variable at runtime. And also I want it to panic when environment variable is not provided at build time.

I'm note sure I understand you/. Are you making an explicit distinction between compile-time and build-time? Because this makes no sense to me:

  1. You don't want an error environment variable FOO not defined at compile time
  2. You want an error when the environment variable FOO is not defined at build-time.

These seem contradictory to me.

I meant LSP complains environment variable FOO not defined at compile time. You can use std::env::var("FOO") to read the variable at runtime. but it compiles fine when FOO is provided.

That makes sense. Thanks for clarifying.

1 Like