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

feat(compile): support --env #24166

Merged
merged 25 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
33cc111
fix: Added support for --env for the compile subcommand
HasanAlrimawi Jun 10, 2024
c72851e
update: repetitive code wrapped into a function to be reused without …
HasanAlrimawi Jun 11, 2024
4b48e42
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 12, 2024
57c2a76
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 23, 2024
3dbf249
lint-fix: replaced eprintln with log::info & initialized logging syst…
HasanAlrimawi Jun 24, 2024
5ee011b
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 24, 2024
a6be904
removed unsused import
HasanAlrimawi Jun 24, 2024
92d2967
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 25, 2024
d1c5efb
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 25, 2024
64cc593
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 26, 2024
b3be35f
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 26, 2024
1f8c1a6
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 27, 2024
6ec532a
Merge branch 'main' into compile-env-support
HasanAlrimawi Jun 30, 2024
fa99ddd
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 1, 2024
8df79fa
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 2, 2024
97c1f01
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 3, 2024
9b3ad26
fix: append env file variables into executable
HasanAlrimawi Jul 3, 2024
49b42ad
fix: added log and updated test .out file
HasanAlrimawi Jul 3, 2024
d6065ac
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 3, 2024
04e4b3e
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 4, 2024
6d2ec5c
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 7, 2024
65111fd
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 8, 2024
c1f9440
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 9, 2024
e33d836
Merge branch 'main' into compile-env-support
HasanAlrimawi Jul 9, 2024
670067c
Updates
dsherret Jul 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Added support for --env for the compile subcommand
  • Loading branch information
HasanAlrimawi committed Jun 10, 2024
commit 33cc111cf213c128e0d0ba7a7abcd83add93894d
4 changes: 4 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,10 @@ impl CliOptions {
}
}

pub fn get_env_file_name(&self) -> Option<String> {
self.flags.env_file.clone()
}

pub fn enable_future_features(&self) -> bool {
*DENO_FUTURE
}
Expand Down
18 changes: 18 additions & 0 deletions cli/mainrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::tokio_util::create_and_run_current_thread_with_maybe_metrics;
pub use deno_runtime::UNSTABLE_GRANULAR_FLAGS;
use deno_terminal::colors;
use dotenvy::from_filename;

use std::borrow::Cow;
use std::env;
Expand Down Expand Up @@ -70,6 +71,22 @@ fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
}
}

fn load_env_variables_from_env_file(filename: Option<&String>) {
if let Some(env_file_name) = filename {
match from_filename(env_file_name) {
Ok(_) => (),
Err(error) => {
match error {
dotenvy::Error::LineParse(line, index)=> eprintln!("{} Parsing failed within the specified environment file: {} at index: {} of the value: {}",colors::yellow("Warning"), env_file_name, index, line),
dotenvy::Error::Io(_)=> eprintln!("{} The `--env` flag was used, but the environment file specified '{}' was not found.",colors::yellow("Warning"),env_file_name),
dotenvy::Error::EnvVar(_)=> eprintln!("{} One or more of the environment variables isn't present or not unicode within the specified environment file: {}",colors::yellow("Warning"),env_file_name),
_ => eprintln!("{} Unknown failure occurred with the specified environment file: {}", colors::yellow("Warning"), env_file_name),
}
}
}
}
}
HasanAlrimawi marked this conversation as resolved.
Show resolved Hide resolved

fn main() {
let args: Vec<_> = env::args_os().collect();
let current_exe_path = current_exe().unwrap();
Expand All @@ -79,6 +96,7 @@ fn main() {
match standalone {
Ok(Some(future)) => {
let (metadata, eszip) = future.await?;
load_env_variables_from_env_file(metadata.env_file.as_ref());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just looking at this PR in detail. I'm not so sure about just providing a reference to the file because the file won't necessarily exist at runtime. Probably we need to embed the env variables into the executable and warn that we're doing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.
I'll sort it out according to your vision.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @dsherret ,

I made changes to embed the env variables from the env file into the generated executable and to warn the user about this action.
When running the executable, it will load the embedded variables as env variables.

let exit_code = standalone::run(eszip, metadata).await?;
std::process::exit(exit_code);
}
Expand Down
2 changes: 2 additions & 0 deletions cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub struct Metadata {
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
pub maybe_import_map: Option<(Url, String)>,
pub entrypoint: ModuleSpecifier,
pub env_file: Option<String>,
pub node_modules: Option<NodeModules>,
pub disable_deprecated_api_warning: bool,
pub unstable_config: UnstableConfig,
Expand Down Expand Up @@ -631,6 +632,7 @@ impl<'a> DenoCompileBinaryWriter<'a> {
ca_stores: cli_options.ca_stores().clone(),
ca_data,
entrypoint: entrypoint.clone(),
env_file: cli_options.get_env_file_name(),
maybe_import_map,
node_modules,
disable_deprecated_api_warning: cli_options
Expand Down