Vscode-rust: Pass environment variables to RLS

Created on 1 Dec 2017  ·  5Comments  ·  Source: rust-lang/vscode-rust

I'm having issues with projects that include diesel and use environment variables for instance:

pub fn establish_connection() -> SqliteConnection {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");

    SqliteConnection::establish(&database_url)
        .expect(&format!("Error connecting to {}", database_url))
}

Results in the following in the output window:

{"message":"proc-macro derive panicked","code":null,"level":"error","spans":[{"file_name":"","byte_start":72,"byte_end":83,"line_start":3,"line_end":3,"column_start":14,"column_end":25,"is_primary":true,"text":[{"text":"# [ derive ( InferSchema ) ] # [","highlight_start":14,"highlight_end":25}],"label":null,"suggested_replacement":null,"expansion":{"span":{"file_name":"","byte_start":72,"byte_end":83,"line_start":3,"line_end":3,"column_start":14,"column_end":25,"is_primary":false,"text":[{"text":"# [ derive ( InferSchema ) ] # [","highlight_start":14,"highlight_end":25}],"label":null,"suggested_replacement":null,"expansion":{"span":{"file_name":"Development/pathfinder-server/pathfinder-server/src/schema.rs","byte_start":0,"byte_end":37,"line_start":1,"line_end":1,"column_start":1,"column_end":38,"is_primary":false,"text":[{"text":"infer_schema!("dotenv:DATABASE_URL");","highlight_start":1,"highlight_end":38}],"label":null,"suggested_replacement":null,"expansion":null},"macro_decl_name":"infer_schema!","def_site_span":{"file_name":"","byte_start":0,"byte_end":484,"line_start":1,"line_end":11,"column_start":1,"column_end":53,"is_primary":false,"text":[{"text":"( $ database_url : expr ) => {","highlight_start":1,"highlight_end":31},{"text":"mod __diesel_infer_schema {","highlight_start":1,"highlight_end":28},{"text":"# [ derive ( InferSchema ) ] # [","highlight_start":1,"highlight_end":33},{"text":"infer_schema_options ( database_url = $ database_url ) ] struct _Dummy ; } pub","highlight_start":1,"highlight_end":79},{"text":"use self :: __diesel_infer_schema :: * ; } ; (","highlight_start":1,"highlight_end":47},{"text":"$ database_url : expr , $ schema_name : expr ) => {","highlight_start":1,"highlight_end":52},{"text":"mod __diesel_infer_schema {","highlight_start":1,"highlight_end":28},{"text":"# [ derive ( InferSchema ) ] # [","highlight_start":1,"highlight_end":33},{"text":"infer_schema_options (","highlight_start":1,"highlight_end":23},{"text":"database_url = $ database_url , schema_name = $ schema_name ) ] struct _Dummy","highlight_start":1,"highlight_end":78},{"text":"; } pub use self :: __diesel_infer_schema :: * ; } ;","highlight_start":1,"highlight_end":53}],"label":null,"suggested_replacement":null,"expansion":null}}},"macro_decl_name":"#[derive(InferSchema)]","def_site_span":null}}],"children":[{"message":"message: called Result::unwrap() on an Err value: "Failed to load environment variable DATABASE_URL: environment variable not found"","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error: proc-macro derive panickedn --> Development/pathfinder-server/pathfinder-server/src/schema.rs:1:1n |n1 | infer_schema!("dotenv:DATABASE_URL");n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^n |n = help: message: called Result::unwrap() on an Err value: "Failed to load environment variable DATABASE_URL: environment variable not found"n = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)nn"}
{"message":"proc-macro derive panicked","code":null,"level":"error","spans":[{"file_name":"","byte_start":72,"byte_end":83,"line_start":3,"line_end":3,"column_start":14,"column_end":25,"is_primary":true,"text":[{"text":"# [ derive ( InferSchema ) ] # [","highlight_start":14,"highlight_end":25}],"label":null,"suggested_replacement":null,"expansion":{"span":{"file_name":"","byte_start":72,"byte_end":83,"line_start":3,"line_end":3,"column_start":14,"column_end":25,"is_primary":false,"text":[{"text":"# [ derive ( InferSchema ) ] # [","highlight_start":14,"highlight_end":25}],"label":null,"suggested_replacement":null,"expansion":{"span":{"file_name":"Development/pathfinder-server/pathfinder-server/src/schema.rs","byte_start":0,"byte_end":37,"line_start":1,"line_end":1,"column_start":1,"column_end":38,"is_primary":false,"text":[{"text":"infer_schema!("dotenv:DATABASE_URL");","highlight_start":1,"highlight_end":38}],"label":null,"suggested_replacement":null,"expansion":null},"macro_decl_name":"infer_schema!","def_site_span":{"file_name":"","byte_start":0,"byte_end":484,"line_start":1,"line_end":11,"column_start":1,"column_end":53,"is_primary":false,"text":[{"text":"( $ database_url : expr ) => {","highlight_start":1,"highlight_end":31},{"text":"mod __diesel_infer_schema {","highlight_start":1,"highlight_end":28},{"text":"# [ derive ( InferSchema ) ] # [","highlight_start":1,"highlight_end":33},{"text":"infer_schema_options ( database_url = $ database_url ) ] struct _Dummy ; } pub","highlight_start":1,"highlight_end":79},{"text":"use self :: __diesel_infer_schema :: * ; } ; (","highlight_start":1,"highlight_end":47},{"text":"$ database_url : expr , $ schema_name : expr ) => {","highlight_start":1,"highlight_end":52},{"text":"mod __diesel_infer_schema {","highlight_start":1,"highlight_end":28},{"text":"# [ derive ( InferSchema ) ] # [","highlight_start":1,"highlight_end":33},{"text":"infer_schema_options (","highlight_start":1,"highlight_end":23},{"text":"database_url = $ database_url , schema_name = $ schema_name ) ] struct _Dummy","highlight_start":1,"highlight_end":78},{"text":"; } pub use self :: __diesel_infer_schema :: * ; } ;","highlight_start":1,"highlight_end":53}],"label":null,"suggested_replacement":null,"expansion":null}}},"macro_decl_name":"#[derive(InferSchema)]","def_site_span":null}}],"children":[{"message":"message: called Result::unwrap() on an Err value: "Failed to load environment variable DATABASE_URL: environment variable not found"","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error: proc-macro derive panickedn --> Development/pathfinder-server/pathfinder-server/src/schema.rs:1:1n |n1 | infer_schema!("dotenv:DATABASE_URL");n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^n |n = help: message: called Result::unwrap() on an Err value: "Failed to load environment variable DATABASE_URL: environment variable not found"n = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)nn"}

Is there a way to pass environment variables to the RLS? Or maybe some path needs to be set so that dotenv can figure out the environmental variable.

enhancement

Most helpful comment

Right now this can be worked around using a wrapper script set in the rust-client.rlsPath configuration. Here's an example wrapper script:

#!/bin/bash
(>&2 echo "Running custom RLS wrapper")

RLS=rls
# Needs to pick up internal rustc libraries in the sysroot 
export LD_LIBRARY_PATH=$(rustc --print sysroot)/lib
# Set some extra env vars you'd like
export RUST_BACKTRACE=full
# Important to also forward all the arguments to the RLS binary 
$RLS "$@"

However I'd like to support that on a first-class level.

All 5 comments

Hah, I ran into the EXACT same issue! Would love to hear someone more experienced with Rust give some idea about how to solve this.

I too would love to know; did either of you @mpj or @kjeremy figure it out?

Same issue everyone, is there any way to make RLS run some .bat script or something?

Right now this can be worked around using a wrapper script set in the rust-client.rlsPath configuration. Here's an example wrapper script:

#!/bin/bash
(>&2 echo "Running custom RLS wrapper")

RLS=rls
# Needs to pick up internal rustc libraries in the sysroot 
export LD_LIBRARY_PATH=$(rustc --print sysroot)/lib
# Set some extra env vars you'd like
export RUST_BACKTRACE=full
# Important to also forward all the arguments to the RLS binary 
$RLS "$@"

However I'd like to support that on a first-class level.

Add a windows version for vscode rust-client.rlsPath:

@echo off
rustc --print sysroot > a:/tmp/temp-rls-msvc.txt
SET /p LD_LIBRARY_PATH=<a:/tmp/temp-rls-msvc.txt
SET RUST_BACKTRACE=full
SET "LIB=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\lib\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64;"

rls %*
@echo on

Was this page helpful?
0 / 5 - 0 ratings