-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for .env file? #127
Comments
Hey there, I've never used rust-script, (just looking through issues to see if it talk about the cargo-script stabilization planned for 2024). But, if a random stranger's guess is helpful: rust-script(like cargo-script) is compiling and storing these files in some centralized location. A quick look at the two sites: You can pass the path whre dotenvy should read from here: dotenvy.from_path use std::path::Path;
dotenvy::from_path(Path::new("path/to/.env"))?; I don't know which .env path you want (there are plausibly 3 - compile location, script location, run location -- and either of the last two are reasonable) If you want some sense of the paths in play there's an environment variables section to the rust-script docs. That should help debug. RUST_SCRIPT_BASE_PATH: the base path used by rust-script to resolve relative dependency paths. Note that this is not necessarily the same as either the working directory, or the directory in which the script is being compiled.
RUST_SCRIPT_PATH: absolute path to the script being run, assuming one exists. Set to the empty string for expressions. um, like I said, I haven't used this crate -- just the nightly version |
@ethanmsl, thank you for taking the time to write out your thoughts on this topic based on your experience. I was using dotenvy's original API when I submitted this issue but with your link to the new API, I was able to get things working: #!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! dotenvy = { version = "0.15.7" }
//! ```
use dotenvy;
let env_path = concat!(env!("RUST_SCRIPT_BASE_PATH"), "/.env");
let env_map = match dotenvy::from_path(env_path) {
Ok(v) => println!(".env found"),
Err(r) => println!(".env NOT found")
}; Closing issue 👍 |
Is it possible to load environment variables from a
.env
file? I tried dotenvy but no matter where the.env
file is placed, I always receive the error:The text was updated successfully, but these errors were encountered: