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()