1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat(cli): add DENO_V8_FLAGS env var (#17313)

Closes #5669
This commit is contained in:
Leo Kettmeir 2023-01-25 05:03:03 +01:00 committed by GitHub
parent 900929f65c
commit f3711f28f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 37 deletions

View file

@ -545,6 +545,7 @@ static ENV_VARIABLES_HELP: &str = r#"ENVIRONMENT VARIABLES:
(alternative to passing --no-prompt on invocation)
DENO_NO_UPDATE_CHECK Set to disable checking if a newer Deno version is
available
DENO_V8_FLAGS Set V8 command line options
DENO_WEBGPU_TRACE Directory to use for wgpu traces
DENO_JOBS Number of parallel workers used for the --parallel
flag with the test subcommand. Defaults to number
@ -2115,7 +2116,8 @@ fn v8_flags_arg<'a>() -> Arg<'a> {
.use_value_delimiter(true)
.require_equals(true)
.help("Set V8 command line options")
.long_help("To see a list of all available flags use --v8-flags=--help.")
.long_help("To see a list of all available flags use --v8-flags=--help.\
Any flags set with this flag are appended after the DENO_V8_FLAGS environmental variable")
}
fn seed_arg<'a>() -> Arg<'a> {

View file

@ -31,43 +31,19 @@ use crate::args::Flags;
use crate::proc_state::ProcState;
use crate::resolver::CliResolver;
use crate::util::display;
use crate::util::v8::get_v8_flags_from_env;
use crate::util::v8::init_v8_flags;
use args::CliOptions;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::error::JsError;
use deno_core::v8_set_flags;
use deno_runtime::colors;
use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::tokio_util::run_local;
use std::env;
use std::iter::once;
use std::path::PathBuf;
fn init_v8_flags(v8_flags: &[String]) {
let v8_flags_includes_help = v8_flags
.iter()
.any(|flag| flag == "-help" || flag == "--help");
// Keep in sync with `standalone.rs`.
let v8_flags = once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
.chain(v8_flags.iter().cloned())
.collect::<Vec<_>>();
let unrecognized_v8_flags = v8_set_flags(v8_flags)
.into_iter()
.skip(1)
.collect::<Vec<_>>();
if !unrecognized_v8_flags.is_empty() {
for f in unrecognized_v8_flags {
eprintln!("error: V8 did not recognize flag '{}'", f);
}
eprintln!("\nFor a list of V8 flags, use '--v8-flags=--help'");
std::process::exit(1);
}
if v8_flags_includes_help {
std::process::exit(0);
}
}
async fn run_subcommand(flags: Flags) -> Result<i32, AnyError> {
match flags.subcommand.clone() {
DenoSubcommand::Bench(bench_flags) => {
@ -285,9 +261,8 @@ pub fn main() {
}
Err(err) => unwrap_or_exit(Err(AnyError::from(err))),
};
if !flags.v8_flags.is_empty() {
init_v8_flags(&flags.v8_flags);
}
init_v8_flags(&flags.v8_flags, get_v8_flags_from_env());
util::logger::init(flags.log_level);

View file

@ -6,6 +6,7 @@ use crate::colors;
use crate::file_fetcher::get_source_from_data_url;
use crate::ops;
use crate::proc_state::ProcState;
use crate::util::v8::construct_v8_flags;
use crate::version;
use crate::CliResolver;
use deno_core::anyhow::Context;
@ -38,7 +39,6 @@ use import_map::parse_from_json;
use log::Level;
use std::env::current_exe;
use std::io::SeekFrom;
use std::iter::once;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::Arc;
@ -248,12 +248,7 @@ pub async fn run(
todo!("Workers are currently not supported in standalone binaries");
});
// Keep in sync with `main.rs`.
v8_set_flags(
once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
.chain(metadata.v8_flags.iter().cloned())
.collect::<Vec<_>>(),
);
v8_set_flags(construct_v8_flags(&metadata.v8_flags, vec![]));
let root_cert_store = ps.root_cert_store.clone();

View file

@ -1154,6 +1154,12 @@ itest!(v8_flags_run {
output: "run/v8_flags.js.out",
});
itest!(v8_flags_env_run {
envs: vec![("DENO_V8_FLAGS".to_string(), "--expose-gc".to_string())],
args: "run run/v8_flags.js",
output: "run/v8_flags.js.out",
});
itest!(v8_flags_unrecognized {
args: "repl --v8-flags=--foo,bar,--trace-gc,-baz",
output: "run/v8_flags_unrecognized.out",

View file

@ -13,4 +13,5 @@ pub mod path;
pub mod progress_bar;
pub mod text_encoding;
pub mod unix;
pub mod v8;
pub mod windows;

47
cli/util/v8.rs Normal file
View file

@ -0,0 +1,47 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
#[inline(always)]
pub fn get_v8_flags_from_env() -> Vec<String> {
std::env::var("DENO_V8_FLAGS")
.ok()
.map(|flags| flags.split(',').map(String::from).collect::<Vec<String>>())
.unwrap_or_default()
}
#[inline(always)]
pub fn construct_v8_flags(
v8_flags: &[String],
env_v8_flags: Vec<String>,
) -> Vec<String> {
std::iter::once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
.chain(env_v8_flags.into_iter())
.chain(v8_flags.iter().cloned())
.collect::<Vec<_>>()
}
pub fn init_v8_flags(v8_flags: &[String], env_v8_flags: Vec<String>) {
if v8_flags.is_empty() && env_v8_flags.is_empty() {
return;
}
let v8_flags_includes_help = env_v8_flags
.iter()
.chain(v8_flags)
.any(|flag| flag == "-help" || flag == "--help");
// Keep in sync with `standalone.rs`.
let v8_flags = construct_v8_flags(v8_flags, env_v8_flags);
let unrecognized_v8_flags = deno_core::v8_set_flags(v8_flags)
.into_iter()
.skip(1)
.collect::<Vec<_>>();
if !unrecognized_v8_flags.is_empty() {
for f in unrecognized_v8_flags {
eprintln!("error: V8 did not recognize flag '{}'", f);
}
eprintln!("\nFor a list of V8 flags, use '--v8-flags=--help'");
std::process::exit(1);
}
if v8_flags_includes_help {
std::process::exit(0);
}
}