Skip to content

fix: --env flag missing file/wrong syntax of file raises error #23915

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

Merged
merged 8 commits into from
May 27, 2024
18 changes: 11 additions & 7 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,13 +857,17 @@ impl CliOptions {
};

if let Some(env_file_name) = &flags.env_file {
if (from_filename(env_file_name)).is_err() {
log::info!(
"{} The `--env` flag was used, but the dotenv file '{}' was not found.",
colors::yellow("Warning"),
env_file_name
);
}
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` 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),
}
}
}
}

let disable_deprecated_api_warning = flags.log_level
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/eval/env_unparsable_file/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"args": "eval --env=../../../testdata/env_unparsable console.log(Deno.env.get(\"Another_FOO\"))",
"output": "main.out"
}
2 changes: 2 additions & 0 deletions tests/specs/eval/env_unparsable_file/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Warning Parsing failed within the specified environment file: ../../../testdata/env_unparsable at index: 3 of the value: c:\path
undefined
4 changes: 4 additions & 0 deletions tests/specs/run/env_unparsable_file/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"args": "run --env=../../../testdata/env_unparsable --allow-env main.js",
"output": "main.out"
}
3 changes: 3 additions & 0 deletions tests/specs/run/env_unparsable_file/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(Deno.env.get("FOO"));
console.log(Deno.env.get("ANOTHER_FOO"));
console.log(Deno.env.get("MULTILINE"));
4 changes: 4 additions & 0 deletions tests/specs/run/env_unparsable_file/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Warning Parsing failed within the specified environment file: ../../../testdata/env_unparsable at index: 3 of the value: c:\path
valid
undefined
undefined
4 changes: 4 additions & 0 deletions tests/testdata/env_unparsable
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FOO=valid
ANOTHER_FOO=c:\path
MULTILINE="First Line
Second Line"
2 changes: 1 addition & 1 deletion tests/testdata/eval/env_file_missing.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Warning The `--env` flag was used, but the dotenv file 'missing' was not found.
Warning The `--env` flag was used, but the environment file specified 'missing' was not found.
undefined
2 changes: 1 addition & 1 deletion tests/testdata/run/env_file_missing.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Warning The `--env` flag was used, but the dotenv file 'missing' was not found.
Warning The `--env` flag was used, but the environment file specified 'missing' was not found.
undefined
undefined
undefined