Skip to content

Commit

Permalink
fix: load env-file at start of flag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Feb 15, 2025
1 parent 56f67b5 commit 0240f04
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
26 changes: 24 additions & 2 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use deno_path_util::url_to_file_path;
use deno_runtime::deno_permissions::SysDescriptor;
use deno_telemetry::OtelConfig;
use deno_telemetry::OtelConsoleConfig;
use deno_terminal::colors;
use log::debug;
use log::Level;
use serde::Deserialize;
Expand Down Expand Up @@ -1239,6 +1240,9 @@ pub fn flags_from_vec(args: Vec<OsString>) -> clap::error::Result<Flags> {

let mut flags = Flags::default();

env_file_arg_parse(&mut flags, &mut matches);
load_env_variables_from_env_file(flags.env_file.as_ref());

if matches.get_flag("quiet") {
flags.log_level = Some(Level::Error);
} else if let Some(log_level) = matches.get_one::<String>("log-level") {
Expand Down Expand Up @@ -1398,6 +1402,26 @@ pub fn flags_from_vec(args: Vec<OsString>) -> clap::error::Result<Flags> {
Ok(flags)
}

fn load_env_variables_from_env_file(filename: Option<&Vec<String>>) {
let Some(env_file_names) = filename else {
return;
};

for env_file_name in env_file_names.iter().rev() {
match dotenvy::from_filename(env_file_name) {
Ok(_) => (),
Err(error) => {
match error {
dotenvy::Error::LineParse(line, index)=> log::info!("{} Parsing failed within the specified environment file: {} at index: {} of the value: {}", colors::yellow("Warning"), env_file_name, index, line),
dotenvy::Error::Io(_)=> log::info!("{} The `--env-file` flag was used, but the environment file specified '{}' was not found.", colors::yellow("Warning"), env_file_name),
dotenvy::Error::EnvVar(_)=> log::info!("{} One or more of the environment variables isn't present or not unicode within the specified environment file: {}", colors::yellow("Warning"), env_file_name),
_ => log::info!("{} Unknown failure occurred with the specified environment file: {}", colors::yellow("Warning"), env_file_name),
}
}
}
}
}

fn enable_unstable(command: Command) -> Command {
command
.mut_arg("unstable", |arg| {
Expand Down Expand Up @@ -5125,7 +5149,6 @@ fn repl_parse(
v8_flags_arg_parse(flags, matches);
seed_arg_parse(flags, matches);
enable_testing_features_arg_parse(flags, matches);
env_file_arg_parse(flags, matches);
strace_ops_parse(flags, matches);

let eval_files = matches
Expand Down Expand Up @@ -5708,7 +5731,6 @@ fn runtime_args_parse(
v8_flags_arg_parse(flags, matches);
seed_arg_parse(flags, matches);
enable_testing_features_arg_parse(flags, matches);
env_file_arg_parse(flags, matches);
strace_ops_parse(flags, matches);
Ok(())
}
Expand Down
23 changes: 0 additions & 23 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ use deno_semver::npm::NpmPackageReqReference;
use deno_semver::StackString;
use deno_telemetry::OtelConfig;
use deno_terminal::colors;
use dotenvy::from_filename;
pub use flags::*;
pub use lockfile::AtomicWriteFileWithRetriesError;
pub use lockfile::CliLockfile;
Expand Down Expand Up @@ -481,8 +480,6 @@ impl CliOptions {
}
}

load_env_variables_from_env_file(flags.env_file.as_ref());

Ok(Self {
flags,
initial_cwd,
Expand Down Expand Up @@ -1321,26 +1318,6 @@ pub fn config_to_deno_graph_workspace_member(
})
}

fn load_env_variables_from_env_file(filename: Option<&Vec<String>>) {
let Some(env_file_names) = filename else {
return;
};

for env_file_name in env_file_names.iter().rev() {
match from_filename(env_file_name) {
Ok(_) => (),
Err(error) => {
match error {
dotenvy::Error::LineParse(line, index)=> log::info!("{} Parsing failed within the specified environment file: {} at index: {} of the value: {}", colors::yellow("Warning"), env_file_name, index, line),
dotenvy::Error::Io(_)=> log::info!("{} The `--env-file` flag was used, but the environment file specified '{}' was not found.", colors::yellow("Warning"), env_file_name),
dotenvy::Error::EnvVar(_)=> log::info!("{} One or more of the environment variables isn't present or not unicode within the specified environment file: {}", colors::yellow("Warning"), env_file_name),
_ => log::info!("{} Unknown failure occurred with the specified environment file: {}", colors::yellow("Warning"), env_file_name),
}
}
}
}
}

/// Gets the --allow-import host from the provided url
fn allow_import_host_from_url(url: &Url) -> Option<String> {
let host = url.host()?;
Expand Down

0 comments on commit 0240f04

Please sign in to comment.