mirror of
https://github.com/denoland/deno.git
synced 2025-09-28 21:24:48 +00:00

Fixes #27264. Fixes https://github.com/denoland/deno/issues/28161. Currently the new lockfile version is gated behind an unstable flag (`--unstable-lockfile-v5`) until the next minor release, where it will become the default. The main motivation here is that it improves startup performance when using the global cache or `--node-modules-dir=auto`. In a create-next-app project, running an empty file: ``` ❯ hyperfine --warmup 25 -N --setup "rm -f deno.lock" "deno run --node-modules-dir=auto -A empty.js" "deno-this-pr run --node-modules-dir=auto -A empty.js" "deno-this-pr run --node-modules-dir=auto --unstable-lockfile-v5 empty.js" "deno run --node-modules-dir=manual -A empty.js" "deno-this-pr run --node-modules-dir=manual -A empty.js" Benchmark 1: deno run --node-modules-dir=auto -A empty.js Time (mean ± σ): 247.6 ms ± 1.7 ms [User: 228.7 ms, System: 19.0 ms] Range (min … max): 245.5 ms … 251.5 ms 12 runs Benchmark 2: deno-this-pr run --node-modules-dir=auto -A empty.js Time (mean ± σ): 169.8 ms ± 1.0 ms [User: 152.9 ms, System: 17.9 ms] Range (min … max): 168.9 ms … 172.5 ms 17 runs Benchmark 3: deno-this-pr run --node-modules-dir=auto --unstable-lockfile-v5 empty.js Time (mean ± σ): 16.2 ms ± 0.7 ms [User: 12.3 ms, System: 5.7 ms] Range (min … max): 15.2 ms … 19.2 ms 185 runs Benchmark 4: deno run --node-modules-dir=manual -A empty.js Time (mean ± σ): 16.2 ms ± 0.8 ms [User: 11.6 ms, System: 5.5 ms] Range (min … max): 14.9 ms … 19.7 ms 187 runs Benchmark 5: deno-this-pr run --node-modules-dir=manual -A empty.js Time (mean ± σ): 16.0 ms ± 0.9 ms [User: 12.0 ms, System: 5.5 ms] Range (min … max): 14.8 ms … 22.3 ms 190 runs Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options. Summary deno-this-pr run --node-modules-dir=manual -A empty.js ran 1.01 ± 0.08 times faster than deno run --node-modules-dir=manual -A empty.js 1.01 ± 0.07 times faster than deno-this-pr run --node-modules-dir=auto --unstable-lockfile-v5 empty.js 10.64 ± 0.60 times faster than deno-this-pr run --node-modules-dir=auto -A empty.js 15.51 ± 0.88 times faster than deno run --node-modules-dir=auto -A empty.js ``` When using the new lockfile version, this leads to a 15.5x faster startup time compared to the current deno version. Install times benefit as well, though to a lesser degree. `deno install` on a create-next-app project, with everything cached (just setting up node_modules from scratch): ``` ❯ hyperfine --warmup 5 -N --prepare "rm -rf node_modules" --setup "rm -rf deno.lock" "deno i" "deno-this-pr i" "deno-this-pr i --unstable-lockfile-v5" Benchmark 1: deno i Time (mean ± σ): 464.4 ms ± 8.8 ms [User: 227.7 ms, System: 217.3 ms] Range (min … max): 452.6 ms … 478.3 ms 10 runs Benchmark 2: deno-this-pr i Time (mean ± σ): 368.8 ms ± 22.0 ms [User: 150.8 ms, System: 198.1 ms] Range (min … max): 344.8 ms … 397.6 ms 10 runs Benchmark 3: deno-this-pr i --unstable-lockfile-v5 Time (mean ± σ): 211.9 ms ± 17.1 ms [User: 7.1 ms, System: 177.2 ms] Range (min … max): 191.3 ms … 233.4 ms 10 runs Summary deno-this-pr i --unstable-lockfile-v5 ran 1.74 ± 0.17 times faster than deno-this-pr i 2.19 ± 0.18 times faster than deno i ``` With lockfile v5, a 2.19x faster install time compared to the current deno.
292 lines
8.6 KiB
Rust
292 lines
8.6 KiB
Rust
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
|
|
use std::io::Read;
|
|
use std::sync::Arc;
|
|
|
|
use deno_cache_dir::file_fetcher::File;
|
|
use deno_config::deno_json::NodeModulesDirMode;
|
|
use deno_core::anyhow::Context;
|
|
use deno_core::error::AnyError;
|
|
use deno_core::futures::FutureExt;
|
|
use deno_core::resolve_url_or_path;
|
|
use deno_lib::standalone::binary::SerializedWorkspaceResolverImportMap;
|
|
use deno_runtime::WorkerExecutionMode;
|
|
use eszip::EszipV2;
|
|
use jsonc_parser::ParseOptions;
|
|
|
|
use crate::args::EvalFlags;
|
|
use crate::args::Flags;
|
|
use crate::args::RunFlags;
|
|
use crate::args::WatchFlagsWithPaths;
|
|
use crate::factory::CliFactory;
|
|
use crate::npm::installer::PackageCaching;
|
|
use crate::util;
|
|
use crate::util::file_watcher::WatcherRestartMode;
|
|
|
|
pub mod hmr;
|
|
|
|
pub fn check_permission_before_script(flags: &Flags) {
|
|
if !flags.has_permission() && flags.has_permission_in_argv() {
|
|
log::warn!(
|
|
"{}",
|
|
crate::colors::yellow(
|
|
r#"Permission flags have likely been incorrectly set after the script argument.
|
|
To grant permissions, set them before the script argument. For example:
|
|
deno run --allow-read=. main.js"#
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
fn set_npm_user_agent() {
|
|
static ONCE: std::sync::Once = std::sync::Once::new();
|
|
ONCE.call_once(|| {
|
|
std::env::set_var(
|
|
crate::npm::NPM_CONFIG_USER_AGENT_ENV_VAR,
|
|
crate::npm::get_npm_config_user_agent(),
|
|
);
|
|
});
|
|
}
|
|
|
|
pub async fn run_script(
|
|
mode: WorkerExecutionMode,
|
|
flags: Arc<Flags>,
|
|
watch: Option<WatchFlagsWithPaths>,
|
|
) -> Result<i32, AnyError> {
|
|
check_permission_before_script(&flags);
|
|
|
|
if let Some(watch_flags) = watch {
|
|
return run_with_watch(mode, flags, watch_flags).boxed_local().await;
|
|
}
|
|
|
|
// TODO(bartlomieju): actually I think it will also fail if there's an import
|
|
// map specified and bare specifier is used on the command line
|
|
let factory = CliFactory::from_flags(flags);
|
|
let cli_options = factory.cli_options()?;
|
|
let deno_dir = factory.deno_dir()?;
|
|
let http_client = factory.http_client_provider();
|
|
|
|
// Run a background task that checks for available upgrades or output
|
|
// if an earlier run of this background task found a new version of Deno.
|
|
#[cfg(feature = "upgrade")]
|
|
super::upgrade::check_for_upgrades(
|
|
http_client.clone(),
|
|
deno_dir.upgrade_check_file_path(),
|
|
);
|
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
|
|
|
if main_module.scheme() == "npm" {
|
|
set_npm_user_agent();
|
|
}
|
|
|
|
maybe_npm_install(&factory).await?;
|
|
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
|
let mut worker = worker_factory
|
|
.create_main_worker(mode, main_module.clone())
|
|
.await?;
|
|
|
|
let exit_code = worker.run().await?;
|
|
Ok(exit_code)
|
|
}
|
|
|
|
pub async fn run_from_stdin(flags: Arc<Flags>) -> Result<i32, AnyError> {
|
|
let factory = CliFactory::from_flags(flags);
|
|
let cli_options = factory.cli_options()?;
|
|
let main_module = cli_options.resolve_main_module()?;
|
|
|
|
maybe_npm_install(&factory).await?;
|
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
|
let mut source = Vec::new();
|
|
std::io::stdin().read_to_end(&mut source)?;
|
|
// Save a fake file into file fetcher cache
|
|
// to allow module access by TS compiler
|
|
file_fetcher.insert_memory_files(File {
|
|
url: main_module.clone(),
|
|
maybe_headers: None,
|
|
source: source.into(),
|
|
});
|
|
|
|
let mut worker = worker_factory
|
|
.create_main_worker(WorkerExecutionMode::Run, main_module.clone())
|
|
.await?;
|
|
let exit_code = worker.run().await?;
|
|
Ok(exit_code)
|
|
}
|
|
|
|
// TODO(bartlomieju): this function is not handling `exit_code` set by the runtime
|
|
// code properly.
|
|
async fn run_with_watch(
|
|
mode: WorkerExecutionMode,
|
|
flags: Arc<Flags>,
|
|
watch_flags: WatchFlagsWithPaths,
|
|
) -> Result<i32, AnyError> {
|
|
util::file_watcher::watch_recv(
|
|
flags,
|
|
util::file_watcher::PrintConfig::new_with_banner(
|
|
if watch_flags.hmr { "HMR" } else { "Watcher" },
|
|
"Process",
|
|
!watch_flags.no_clear_screen,
|
|
),
|
|
WatcherRestartMode::Automatic,
|
|
move |flags, watcher_communicator, changed_paths| {
|
|
watcher_communicator.show_path_changed(changed_paths.clone());
|
|
Ok(async move {
|
|
let factory = CliFactory::from_flags_for_watcher(
|
|
flags,
|
|
watcher_communicator.clone(),
|
|
);
|
|
let cli_options = factory.cli_options()?;
|
|
let main_module = cli_options.resolve_main_module()?;
|
|
|
|
if main_module.scheme() == "npm" {
|
|
set_npm_user_agent();
|
|
}
|
|
|
|
maybe_npm_install(&factory).await?;
|
|
|
|
let _ = watcher_communicator.watch_paths(cli_options.watch_paths());
|
|
|
|
let mut worker = factory
|
|
.create_cli_main_worker_factory()
|
|
.await?
|
|
.create_main_worker(mode, main_module.clone())
|
|
.await?;
|
|
|
|
if watch_flags.hmr {
|
|
worker.run().await?;
|
|
} else {
|
|
worker.run_for_watcher().await?;
|
|
}
|
|
|
|
Ok(())
|
|
})
|
|
},
|
|
)
|
|
.boxed_local()
|
|
.await?;
|
|
|
|
Ok(0)
|
|
}
|
|
|
|
pub async fn eval_command(
|
|
flags: Arc<Flags>,
|
|
eval_flags: EvalFlags,
|
|
) -> Result<i32, AnyError> {
|
|
let factory = CliFactory::from_flags(flags);
|
|
let cli_options = factory.cli_options()?;
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
let main_module = cli_options.resolve_main_module()?;
|
|
|
|
maybe_npm_install(&factory).await?;
|
|
|
|
// Create a dummy source file.
|
|
let source_code = if eval_flags.print {
|
|
format!("console.log({})", eval_flags.code)
|
|
} else {
|
|
eval_flags.code
|
|
};
|
|
|
|
// Save a fake file into file fetcher cache
|
|
// to allow module access by TS compiler.
|
|
file_fetcher.insert_memory_files(File {
|
|
url: main_module.clone(),
|
|
maybe_headers: None,
|
|
source: source_code.into_bytes().into(),
|
|
});
|
|
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
|
let mut worker = worker_factory
|
|
.create_main_worker(WorkerExecutionMode::Eval, main_module.clone())
|
|
.await?;
|
|
let exit_code = worker.run().await?;
|
|
Ok(exit_code)
|
|
}
|
|
|
|
pub async fn maybe_npm_install(factory: &CliFactory) -> Result<(), AnyError> {
|
|
let cli_options = factory.cli_options()?;
|
|
// ensure an "npm install" is done if the user has explicitly
|
|
// opted into using a managed node_modules directory
|
|
if cli_options.specified_node_modules_dir()? == Some(NodeModulesDirMode::Auto)
|
|
{
|
|
if let Some(npm_installer) = factory.npm_installer_if_managed().await? {
|
|
let already_done = npm_installer
|
|
.ensure_top_level_package_json_install()
|
|
.await?;
|
|
if !already_done
|
|
&& matches!(
|
|
cli_options.default_npm_caching_strategy(),
|
|
crate::graph_util::NpmCachingStrategy::Eager
|
|
)
|
|
{
|
|
npm_installer.cache_packages(PackageCaching::All).await?;
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn run_eszip(
|
|
flags: Arc<Flags>,
|
|
run_flags: RunFlags,
|
|
) -> Result<i32, AnyError> {
|
|
// TODO(bartlomieju): actually I think it will also fail if there's an import
|
|
// map specified and bare specifier is used on the command line
|
|
let factory = CliFactory::from_flags(flags.clone());
|
|
let cli_options = factory.cli_options()?;
|
|
|
|
// entrypoint#path1,path2,...
|
|
let (entrypoint, _files) = run_flags
|
|
.script
|
|
.split_once("#")
|
|
.with_context(|| "eszip: invalid script string")?;
|
|
|
|
let mode = WorkerExecutionMode::Run;
|
|
let main_module = resolve_url_or_path(entrypoint, cli_options.initial_cwd())?;
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
|
let mut worker = worker_factory
|
|
.create_main_worker(mode, main_module.clone())
|
|
.await?;
|
|
|
|
let exit_code = worker.run().await?;
|
|
Ok(exit_code)
|
|
}
|
|
|
|
#[allow(unused)]
|
|
async fn load_import_map(
|
|
eszips: &[EszipV2],
|
|
specifier: &str,
|
|
) -> Result<SerializedWorkspaceResolverImportMap, AnyError> {
|
|
let maybe_module = eszips
|
|
.iter()
|
|
.rev()
|
|
.find_map(|eszip| eszip.get_import_map(specifier));
|
|
let Some(module) = maybe_module else {
|
|
return Err(AnyError::msg(format!("import map not found '{specifier}'")));
|
|
};
|
|
let base_url = deno_core::url::Url::parse(specifier).map_err(|err| {
|
|
AnyError::msg(format!(
|
|
"import map specifier '{specifier}' is not a valid url: {err}"
|
|
))
|
|
})?;
|
|
let bytes = module
|
|
.source()
|
|
.await
|
|
.ok_or_else(|| AnyError::msg("import map not found '{specifier}'"))?;
|
|
let text = String::from_utf8_lossy(&bytes);
|
|
let json_value =
|
|
jsonc_parser::parse_to_serde_value(&text, &ParseOptions::default())
|
|
.map_err(|err| {
|
|
AnyError::msg(format!("import map failed to parse: {err}"))
|
|
})?
|
|
.ok_or_else(|| AnyError::msg("import map is not valid JSON"))?;
|
|
let import_map = import_map::parse_from_value(base_url, json_value)?;
|
|
|
|
Ok(SerializedWorkspaceResolverImportMap {
|
|
specifier: specifier.to_string(),
|
|
json: import_map.import_map.to_json(),
|
|
})
|
|
}
|