vscode-rust icon indicating copy to clipboard operation
vscode-rust copied to clipboard

Pass environment variables to RLS

Open kjeremy opened this issue 7 years ago • 10 comments

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":"<infer_schema macros>","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":"<infer_schema macros>","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":"<infer_schema macros>","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 panicked\n --> Development/pathfinder-server/pathfinder-server/src/schema.rs:1:1\n |\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)\n\n"} {"message":"proc-macro derive panicked","code":null,"level":"error","spans":[{"file_name":"<infer_schema macros>","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":"<infer_schema macros>","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":"<infer_schema macros>","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 panicked\n --> Development/pathfinder-server/pathfinder-server/src/schema.rs:1:1\n |\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)\n\n"}

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.

kjeremy avatar Dec 01 '17 15:12 kjeremy

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.

mpj avatar Dec 20 '17 18:12 mpj

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

jeremybmerrill avatar Jan 16 '18 19:01 jeremybmerrill

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

pronebird avatar Jul 16 '18 13:07 pronebird

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.

Xanewok avatar Apr 07 '19 12:04 Xanewok

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

cuzfrog avatar Jul 06 '20 15:07 cuzfrog

Can we not having something like this to configure?

"rust-client.environments": [
  "OPENSSL_DIR": "/path/to/openssl",
  "SSL_CERT_FILE": "/path/to/openssl/certs"
]

18601673727 avatar Nov 15 '21 10:11 18601673727

@18601673727 maybe you can give https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer a try, it has a preference for extra environment variables.

lnicola avatar Nov 15 '21 10:11 lnicola

@18601673727 maybe you can give https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer a try, it has a preference for extra environment variables.

Thanks and I tried, but compares it to tls, I reckon it too weak to detect bugs I wrote in Rust, so for noobs like me, tls is the cure. Or would you mind enlighten me how to configure rust-analyzer as strict as rls?

18601673727 avatar Nov 15 '21 10:11 18601673727

@18601673727 it should be just as strict as RLS, with the caveats that:

  • you need to save to see most diagnostics (Code has an auto-save option, but I wouldn't recommend using it)
  • some complex errors might be harder to read in the IDE (the Problems panel or running cargo check manually should help)
  • you might see some strange warnings or some features might not work correctly with code that uses procedural macros (but RLS might have issues with those too, I haven't tried)
  • some more advanced language features like const generics are not fully supported
  • Rustlings probably doesn't work

If you have any other problems, consider filing an issue over there.

lnicola avatar Nov 15 '21 10:11 lnicola

@18601673727 it should be just as strict as RLS, with the caveats that:

  • you need to save to see most diagnostics (Code has an auto-save option, but I wouldn't recommend using it)
  • some complex errors might be harder to read in the IDE (the Problems panel or running cargo check manually should help)
  • you might see some strange warnings or some features might not work correctly with code that uses procedural macros (but RLS might have issues with those too, I haven't tried)
  • some more advanced language features like const generics are not fully supported
  • Rustlings probably doesn't work

If you have any other problems, consider filing an issue over there.

Thank you again for so many useful inputs! Miraculously I solved my issue with rls by adding

[env]
OPENSSL_DIR = "C:\\OpenSSL-Win64"
SSL_CERT_FILE = "C:\\OpenSSL-Win64\\certs\\cacert.pem"

in .cargo/config.toml with a restart of Visual Studio Code.

Should able to re-close https://github.com/sfackler/rust-openssl/issues/1542

18601673727 avatar Nov 15 '21 11:11 18601673727