feat: Codegen feature flags (#28920)

This commit adds "deno_features" crate that contains definitions of all
unstable features in Deno.

Based on these definitions, both Rust and JS code is generated ensuring
that the two are always in sync.

In addition some of flag handling was rewritten to use the generated
definitions, instead of hand rolling these flag definitions.

---------

Co-authored-by: snek <snek@deno.com>
This commit is contained in:
Bartek Iwańczuk 2025-04-25 10:33:45 +02:00 committed by GitHub
parent 4924731ac3
commit 189ccffdb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 701 additions and 284 deletions

View file

@ -38,6 +38,7 @@ use deno_npm::NpmSystemInfo;
use deno_path_util::normalize_path;
use deno_path_util::url_to_file_path;
use deno_runtime::deno_permissions::SysDescriptor;
use deno_runtime::UnstableFeatureKind;
use deno_telemetry::OtelConfig;
use deno_telemetry::OtelConsoleConfig;
use deno_telemetry::OtelPropagators;
@ -4428,114 +4429,41 @@ impl CommandExt for Command {
.action(ArgAction::SetTrue)
.hide(matches!(cfg, UnstableArgsConfig::None))
.display_order(next_display_order())
).arg(
Arg::new("unstable-bare-node-builtins")
.long("unstable-bare-node-builtins")
.help("Enable unstable bare node builtins feature")
.env("DENO_UNSTABLE_BARE_NODE_BUILTINS")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
.long_help(match cfg {
UnstableArgsConfig::None => None,
UnstableArgsConfig::ResolutionOnly
| UnstableArgsConfig::ResolutionAndRuntime => Some("true"),
})
.help_heading(UNSTABLE_HEADING)
.display_order(next_display_order()),
).arg(
Arg::new("unstable-detect-cjs")
.long("unstable-detect-cjs")
.help("Treats ambiguous .js, .jsx, .ts, .tsx files as CommonJS modules in more cases")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
.long_help(match cfg {
UnstableArgsConfig::None => None,
UnstableArgsConfig::ResolutionOnly
| UnstableArgsConfig::ResolutionAndRuntime => Some("true"),
})
.help_heading(UNSTABLE_HEADING)
.display_order(next_display_order())
).arg(
Arg::new("unstable-byonm")
.long("unstable-byonm")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
.help_heading(UNSTABLE_HEADING)
.display_order(next_display_order()),
).arg(
Arg::new("unstable-lazy-dynamic-imports")
.long("unstable-lazy-dynamic-imports")
.help("Lazily loads statically analyzable dynamic imports when not running with type checking. Warning: This may change the order of semver specifier resolution.")
.env("DENO_UNSTABLE_LAZY_DYNAMIC_IMPORTS")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
.long_help(match cfg {
UnstableArgsConfig::None => None,
UnstableArgsConfig::ResolutionOnly | UnstableArgsConfig::ResolutionAndRuntime => Some("true")
})
.help_heading(UNSTABLE_HEADING)
.display_order(next_display_order())
).arg(
Arg::new("unstable-sloppy-imports")
.long("unstable-sloppy-imports")
.help("Enable unstable resolving of specifiers by extension probing, .js to .ts, and directory probing")
.env("DENO_UNSTABLE_SLOPPY_IMPORTS")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
.long_help(match cfg {
UnstableArgsConfig::None => None,
UnstableArgsConfig::ResolutionOnly | UnstableArgsConfig::ResolutionAndRuntime => Some("true")
})
.help_heading(UNSTABLE_HEADING)
.display_order(next_display_order())
).arg(
Arg::new("unstable-npm-lazy-caching")
.long("unstable-npm-lazy-caching")
.help("Enable unstable lazy caching of npm dependencies, downloading them only as needed (disabled: all npm packages in package.json are installed on startup; enabled: only npm packages that are actually referenced in an import are installed")
.env("DENO_UNSTABLE_NPM_LAZY_CACHING")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
.help_heading(UNSTABLE_HEADING)
.display_order(next_display_order()),
).arg(
Arg::new("unstable-lockfile-v5")
.long("unstable-lockfile-v5")
.help("Enable unstable lockfile v5")
.env("DENO_UNSTABLE_LOCKFILE_V5")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.help_heading(UNSTABLE_HEADING)
.hide(true)
.display_order(next_display_order()),
);
for granular_flag in crate::UNSTABLE_GRANULAR_FLAGS.iter() {
cmd = cmd.arg(
Arg::new(format!("unstable-{}", granular_flag.name))
.long(format!("unstable-{}", granular_flag.name))
.help(granular_flag.help_text)
.action(ArgAction::SetTrue)
.hide(true)
.help_heading(UNSTABLE_HEADING)
// we don't render long help, so using it here as a sort of metadata
.long_help(if granular_flag.show_in_help {
match cfg {
UnstableArgsConfig::None | UnstableArgsConfig::ResolutionOnly => {
None
}
UnstableArgsConfig::ResolutionAndRuntime => Some("true"),
}
} else {
None
})
.display_order(next_display_order()),
);
for feature in crate::UNSTABLE_FEATURES.iter() {
let mut arg = Arg::new(feature.flag_name)
.long(feature.flag_name)
.help(feature.help_text)
.action(ArgAction::SetTrue)
.value_parser(FalseyValueParser::new())
.hide(true)
.help_heading(UNSTABLE_HEADING)
.display_order(next_display_order());
// TODO(bartlomieju):
// Value of `.long_help()` is actuall a metadata. It should be rewritten to use
// Clap's `ArgExt` instead
let mut long_help_val = None;
if feature.show_in_help {
if matches!(cfg, UnstableArgsConfig::ResolutionOnly)
&& matches!(feature.kind, UnstableFeatureKind::Cli)
{
long_help_val = Some("true");
}
if matches!(cfg, UnstableArgsConfig::ResolutionAndRuntime) {
long_help_val = Some("true");
}
}
arg = arg.long_help(long_help_val);
if let Some(env_var_name) = feature.env_var {
arg = arg.env(env_var_name);
}
cmd = cmd.arg(arg);
}
cmd
@ -6142,6 +6070,7 @@ fn unstable_args_parse(
flags.unstable_config.legacy_flag_enabled = true;
}
// TODO(bartlomieju): this should be factored out since these are configured via UNSTABLE_FEATURES
flags.unstable_config.bare_node_builtins =
matches.get_flag("unstable-bare-node-builtins");
flags.unstable_config.detect_cjs = matches.get_flag("unstable-detect-cjs");
@ -6153,12 +6082,12 @@ fn unstable_args_parse(
matches.get_flag("unstable-npm-lazy-caching");
if matches!(cfg, UnstableArgsConfig::ResolutionAndRuntime) {
for granular_flag in crate::UNSTABLE_GRANULAR_FLAGS {
if matches.get_flag(&format!("unstable-{}", granular_flag.name)) {
for feature in crate::UNSTABLE_FEATURES {
if matches.get_flag(feature.flag_name) {
flags
.unstable_config
.features
.push(granular_flag.name.to_string());
.push(feature.name.to_string());
}
}
}
@ -11752,7 +11681,7 @@ mod tests {
unstable_config: UnstableConfig {
bare_node_builtins: true,
sloppy_imports: false,
features: svec!["ffi", "worker-options"],
features: svec!["bare-node-builtins", "ffi", "worker-options"],
..Default::default()
},
..Flags::default()

View file

@ -1116,21 +1116,10 @@ impl CliOptions {
.collect::<Vec<_>>();
if !unstable_features.is_empty() {
let all_valid_unstable_flags: Vec<&str> = crate::UNSTABLE_GRANULAR_FLAGS
let all_valid_unstable_flags: Vec<&str> = crate::UNSTABLE_FEATURES
.iter()
.map(|granular_flag| granular_flag.name)
.chain([
"byonm",
"bare-node-builtins",
"detect-cjs",
"fmt-component",
"fmt-sql",
"lazy-dynamic-imports",
"npm-lazy-caching",
"npm-patch",
"sloppy-imports",
"lockfile-v5",
])
.map(|feature| feature.name)
.chain(["fmt-component", "fmt-sql", "npm-lazy-caching", "npm-patch"])
.collect();
// check and warn if the unstable flag of config file isn't supported, by

View file

@ -1142,9 +1142,9 @@ impl CliFactory {
let mut checker = FeatureChecker::default();
checker.set_exit_cb(Box::new(crate::unstable_exit_cb));
let unstable_features = cli_options.unstable_features();
for granular_flag in crate::UNSTABLE_GRANULAR_FLAGS {
if unstable_features.contains(&granular_flag.name.to_string()) {
checker.enable_feature(granular_flag.name);
for feature in crate::UNSTABLE_FEATURES {
if unstable_features.contains(&feature.name.to_string()) {
checker.enable_feature(feature.name);
}
}

View file

@ -45,7 +45,7 @@ use deno_runtime::worker::WorkerServiceOptions;
use deno_runtime::BootstrapOptions;
use deno_runtime::WorkerExecutionMode;
use deno_runtime::WorkerLogLevel;
use deno_runtime::UNSTABLE_GRANULAR_FLAGS;
use deno_runtime::UNSTABLE_FEATURES;
use node_resolver::errors::ResolvePkgJsonBinExportError;
use node_resolver::UrlOrPath;
use url::Url;
@ -216,11 +216,10 @@ impl<TSys: DenoLibSys> LibWorkerFactorySharedState<TSys> {
&self,
feature_checker: &FeatureChecker,
) -> Vec<i32> {
let mut unstable_features =
Vec::with_capacity(UNSTABLE_GRANULAR_FLAGS.len());
for granular_flag in UNSTABLE_GRANULAR_FLAGS {
if feature_checker.check(granular_flag.name) {
unstable_features.push(granular_flag.id);
let mut unstable_features = Vec::with_capacity(UNSTABLE_FEATURES.len());
for feature in UNSTABLE_FEATURES {
if feature_checker.check(feature.name) {
unstable_features.push(feature.id);
}
}
unstable_features

View file

@ -49,7 +49,7 @@ use deno_resolver::npm::ResolvePkgFolderFromDenoReqError;
use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::tokio_util::create_and_run_current_thread_with_maybe_metrics;
use deno_runtime::WorkerExecutionMode;
pub use deno_runtime::UNSTABLE_GRANULAR_FLAGS;
pub use deno_runtime::UNSTABLE_FEATURES;
use deno_telemetry::OtelConfig;
use deno_terminal::colors;
use factory::CliFactory;