Skip to content
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

Closed
hbcondo opened this issue Apr 29, 2024 · 2 comments
Closed

Support for .env file? #127

hbcondo opened this issue Apr 29, 2024 · 2 comments

Comments

@hbcondo
Copy link

hbcondo commented Apr 29, 2024

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:

.env file not found: Io(Custom { kind: NotFound, error: "path not found" })
@dream-dasher
Copy link

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.
dotenvy is presumably looking in the locale of where the compiled code lives, rather than where you're running it.

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.
And then you can just feed the desired path into dotenvy. (pulling dynamically if you want the run path)

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 cargo +nightly -Zscript <file.rs> <args/flags>
But maybe helps

@hbcondo
Copy link
Author

hbcondo commented Oct 15, 2024

@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 👍

@hbcondo hbcondo closed this as completed Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants