Skip to content

Commit f6b889b

Browse files
authored
refactor: snapshotting of runtime/ and cli/ (#21430)
This commit removes some of the technical debt related to snapshotting JS code: - "cli/ops/mod.rs" and "cli/build.rs" no longer define "cli" extension which was not required anymore - Cargo features for "deno_runtime" crate have been unified in "cli/Cargo.toml" - "cli/build.rs" uses "deno_runtime::snapshot::create_runtime_snapshot" API instead of copy-pasting the code - "cli/js/99_main.js" was completely removed as it's not necessary anymore Towards #21137
1 parent 0f990d9 commit f6b889b

File tree

9 files changed

+30
-152
lines changed

9 files changed

+30
-152
lines changed

cli/Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ __runtime_js_sources = ["deno_runtime/__runtime_js_sources"]
3939
__vendored_zlib_ng = ["flate2/zlib-ng-compat", "libz-sys/zlib-ng"]
4040

4141
[build-dependencies]
42-
deno_runtime = { workspace = true, features = ["exclude_runtime_main_js", "include_js_files_for_snapshotting"] }
42+
# TODO(bartlomieju): should we not include `dont_create_runtime_snapshot`
43+
# feature here and actually create the snapshot in `deno_runtime` build script?
44+
# How do we pass options?
45+
deno_runtime = { workspace = true, features = ["dont_create_runtime_snapshot", "include_js_files_for_snapshotting"] }
4346
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
4447
lazy-regex.workspace = true
4548
serde.workspace = true
@@ -63,7 +66,7 @@ deno_graph = "=0.61.5"
6366
deno_lint = { version = "=0.52.2", features = ["docs"] }
6467
deno_lockfile.workspace = true
6568
deno_npm = "0.15.2"
66-
deno_runtime = { workspace = true, features = ["dont_create_runtime_snapshot", "exclude_runtime_main_js", "include_js_files_for_snapshotting"] }
69+
deno_runtime = { workspace = true, features = ["dont_create_runtime_snapshot", "include_js_files_for_snapshotting"] }
6770
deno_semver = "0.5.1"
6871
deno_task_shell = "=0.14.0"
6972
eszip = "=0.55.5"

cli/build.rs

+12-103
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::env;
44
use std::path::PathBuf;
55

66
use deno_core::snapshot_util::*;
7-
use deno_core::ExtensionFileSource;
8-
use deno_core::ExtensionFileSourceCode;
97
use deno_runtime::*;
108

119
mod ts {
@@ -318,36 +316,9 @@ mod ts {
318316
}
319317
}
320318

321-
// Duplicated in `ops/mod.rs`. Keep in sync!
322-
deno_core::extension!(
323-
cli,
324-
deps = [runtime],
325-
esm_entry_point = "ext:cli/99_main.js",
326-
esm = [
327-
dir "js",
328-
"99_main.js"
329-
],
330-
customizer = |ext: &mut deno_core::Extension| {
331-
ext.esm_files.to_mut().push(ExtensionFileSource {
332-
specifier: "ext:cli/runtime/js/99_main.js",
333-
code: ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
334-
deno_runtime::js::PATH_FOR_99_MAIN_JS,
335-
),
336-
});
337-
}
338-
);
339-
340319
#[cfg(not(feature = "__runtime_js_sources"))]
341-
#[must_use = "The files listed by create_cli_snapshot should be printed as 'cargo:rerun-if-changed' lines"]
342-
fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput {
343-
use deno_core::Extension;
344-
use deno_runtime::deno_cache::SqliteBackedCache;
345-
use deno_runtime::deno_cron::local::LocalCronHandler;
346-
use deno_runtime::deno_http::DefaultHttpPropertyExtractor;
347-
use deno_runtime::deno_kv::sqlite::SqliteDbHandler;
320+
fn create_cli_snapshot(snapshot_path: PathBuf) {
348321
use deno_runtime::ops::bootstrap::SnapshotOptions;
349-
use deno_runtime::permissions::PermissionsContainer;
350-
use std::sync::Arc;
351322

352323
// NOTE(bartlomieju): keep in sync with `cli/version.rs`.
353324
// Ideally we could deduplicate that code.
@@ -359,76 +330,17 @@ fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput {
359330
}
360331
}
361332

362-
// NOTE(bartlomieju): ordering is important here, keep it in sync with
363-
// `runtime/worker.rs`, `runtime/web_worker.rs` and `runtime/build.rs`!
364-
let fs = Arc::new(deno_fs::RealFs);
365-
let extensions: Vec<Extension> = vec![
366-
deno_webidl::deno_webidl::init_ops(),
367-
deno_console::deno_console::init_ops(),
368-
deno_url::deno_url::init_ops(),
369-
deno_web::deno_web::init_ops::<PermissionsContainer>(
370-
Default::default(),
371-
Default::default(),
372-
),
373-
deno_fetch::deno_fetch::init_ops::<PermissionsContainer>(Default::default()),
374-
deno_cache::deno_cache::init_ops::<SqliteBackedCache>(None),
375-
deno_websocket::deno_websocket::init_ops::<PermissionsContainer>(
376-
"".to_owned(),
377-
None,
378-
None,
379-
),
380-
deno_webstorage::deno_webstorage::init_ops(None),
381-
deno_crypto::deno_crypto::init_ops(None),
382-
deno_broadcast_channel::deno_broadcast_channel::init_ops(
383-
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
384-
),
385-
deno_ffi::deno_ffi::init_ops::<PermissionsContainer>(),
386-
deno_net::deno_net::init_ops::<PermissionsContainer>(None, None),
387-
deno_tls::deno_tls::init_ops(),
388-
deno_kv::deno_kv::init_ops(SqliteDbHandler::<PermissionsContainer>::new(
389-
None, None,
390-
)),
391-
deno_cron::deno_cron::init_ops(LocalCronHandler::new()),
392-
deno_napi::deno_napi::init_ops::<PermissionsContainer>(),
393-
deno_http::deno_http::init_ops::<DefaultHttpPropertyExtractor>(),
394-
deno_io::deno_io::init_ops(Default::default()),
395-
deno_fs::deno_fs::init_ops::<PermissionsContainer>(fs.clone()),
396-
deno_node::deno_node::init_ops::<PermissionsContainer>(None, fs),
397-
deno_runtime::runtime::init_ops(),
398-
deno_runtime::ops::runtime::deno_runtime::init_ops(
399-
"deno:runtime".parse().unwrap(),
400-
),
401-
deno_runtime::ops::worker_host::deno_worker_host::init_ops(
402-
Arc::new(|_| unreachable!("not used in snapshot.")),
403-
None,
404-
),
405-
deno_runtime::ops::fs_events::deno_fs_events::init_ops(),
406-
deno_runtime::ops::os::deno_os::init_ops(Default::default()),
407-
deno_runtime::ops::permissions::deno_permissions::init_ops(),
408-
deno_runtime::ops::process::deno_process::init_ops(),
409-
deno_runtime::ops::signal::deno_signal::init_ops(),
410-
deno_runtime::ops::tty::deno_tty::init_ops(),
411-
deno_runtime::ops::http::deno_http_runtime::init_ops(),
412-
deno_runtime::ops::bootstrap::deno_bootstrap::init_ops(Some(
413-
SnapshotOptions {
414-
deno_version: deno_version(),
415-
ts_version: ts::version(),
416-
v8_version: deno_core::v8_version(),
417-
target: std::env::var("TARGET").unwrap(),
418-
},
419-
)),
420-
cli::init_ops_and_esm(), // NOTE: This needs to be init_ops_and_esm!
421-
];
422-
423-
create_snapshot(CreateSnapshotOptions {
424-
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
333+
let snapshot_options = SnapshotOptions {
334+
deno_version: deno_version(),
335+
ts_version: ts::version(),
336+
v8_version: deno_core::v8_version(),
337+
target: std::env::var("TARGET").unwrap(),
338+
};
339+
340+
deno_runtime::snapshot::create_runtime_snapshot(
425341
snapshot_path,
426-
startup_snapshot: deno_runtime::js::deno_isolate_init(),
427-
extensions,
428-
compression_cb: None,
429-
with_runtime_cb: None,
430-
skip_op_registration: false,
431-
})
342+
snapshot_options,
343+
);
432344
}
433345

434346
fn git_commit_hash() -> String {
@@ -539,10 +451,7 @@ fn main() {
539451
#[cfg(not(feature = "__runtime_js_sources"))]
540452
{
541453
let cli_snapshot_path = o.join("CLI_SNAPSHOT.bin");
542-
let output = create_cli_snapshot(cli_snapshot_path);
543-
for path in output.files_loaded_during_snapshot {
544-
println!("cargo:rerun-if-changed={}", path.display())
545-
}
454+
create_cli_snapshot(cli_snapshot_path);
546455
}
547456

548457
#[cfg(target_os = "windows")]

cli/js/99_main.js

-2
This file was deleted.

cli/ops/mod.rs

-30
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,5 @@
11
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
22

3-
use deno_core::Extension;
4-
53
pub mod bench;
64
pub mod jupyter;
75
pub mod testing;
8-
9-
pub fn cli_exts() -> Vec<Extension> {
10-
vec![
11-
#[cfg(not(feature = "__runtime_js_sources"))]
12-
cli::init_ops(),
13-
#[cfg(feature = "__runtime_js_sources")]
14-
cli::init_ops_and_esm(),
15-
]
16-
}
17-
18-
// ESM parts duplicated in `../build.rs`. Keep in sync!
19-
deno_core::extension!(cli,
20-
deps = [runtime],
21-
esm_entry_point = "ext:cli/99_main.js",
22-
esm = [
23-
dir "js",
24-
"40_testing.js",
25-
"99_main.js"
26-
],
27-
customizer = |ext: &mut deno_core::Extension| {
28-
ext.esm_files.to_mut().push(deno_core::ExtensionFileSource {
29-
specifier: "ext:cli/runtime/js/99_main.js",
30-
code: deno_core::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
31-
deno_runtime::js::PATH_FOR_99_MAIN_JS,
32-
),
33-
});
34-
},
35-
);

cli/worker.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ use crate::args::StorageKeyResolver;
5252
use crate::emit::Emitter;
5353
use crate::errors;
5454
use crate::npm::CliNpmResolver;
55-
use crate::ops;
5655
use crate::tools;
5756
use crate::tools::coverage::CoverageCollector;
5857
use crate::tools::run::hmr::HmrRunner;
@@ -459,7 +458,7 @@ impl CliMainWorkerFactory {
459458
&self,
460459
main_module: ModuleSpecifier,
461460
permissions: PermissionsContainer,
462-
mut custom_extensions: Vec<Extension>,
461+
custom_extensions: Vec<Extension>,
463462
stdio: deno_runtime::deno_io::Stdio,
464463
) -> Result<CliMainWorker, AnyError> {
465464
let shared = &self.shared;
@@ -564,9 +563,6 @@ impl CliMainWorkerFactory {
564563
.join(checksum::gen(&[key.as_bytes()]))
565564
});
566565

567-
let mut extensions = ops::cli_exts();
568-
extensions.append(&mut custom_extensions);
569-
570566
// TODO(bartlomieju): this is cruft, update FeatureChecker to spit out
571567
// list of enabled features.
572568
let feature_checker = shared.feature_checker.clone();
@@ -601,7 +597,7 @@ impl CliMainWorkerFactory {
601597
.maybe_binary_npm_command_name
602598
.clone(),
603599
},
604-
extensions,
600+
extensions: custom_extensions,
605601
startup_snapshot: crate::js::deno_isolate_init(),
606602
create_params: None,
607603
unsafely_ignore_certificate_errors: shared
@@ -753,8 +749,6 @@ fn create_web_worker_callback(
753749
let create_web_worker_cb =
754750
create_web_worker_callback(shared.clone(), stdio.clone());
755751

756-
let extensions = ops::cli_exts();
757-
758752
let maybe_storage_key = shared
759753
.storage_key_resolver
760754
.resolve_storage_key(&args.main_module);
@@ -800,7 +794,7 @@ fn create_web_worker_callback(
800794
.maybe_binary_npm_command_name
801795
.clone(),
802796
},
803-
extensions,
797+
extensions: vec![],
804798
startup_snapshot: crate::js::deno_isolate_init(),
805799
unsafely_ignore_certificate_errors: shared
806800
.options

runtime/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ mod startup_snapshot {
214214

215215
pub fn create_runtime_snapshot(snapshot_path: PathBuf) {
216216
// NOTE(bartlomieju): ordering is important here, keep it in sync with
217-
// `runtime/worker.rs`, `runtime/web_worker.rs` and `cli/build.rs`!
217+
// `runtime/worker.rs`, `runtime/web_worker.rs` and `runtime/snapshot.rs`!
218218
let fs = std::sync::Arc::new(deno_fs::RealFs);
219219
let mut extensions: Vec<Extension> = vec![
220220
deno_webidl::deno_webidl::init_ops_and_esm(),

runtime/snapshot.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
22

33
use crate::ops;
4+
use crate::ops::bootstrap::SnapshotOptions;
45
use crate::shared::maybe_transpile_source;
56
use crate::shared::runtime;
67
use deno_cache::SqliteBackedCache;
@@ -183,9 +184,12 @@ impl deno_kv::sqlite::SqliteDbHandlerPermissions for Permissions {
183184
}
184185
}
185186

186-
pub fn create_runtime_snapshot(snapshot_path: PathBuf) {
187+
pub fn create_runtime_snapshot(
188+
snapshot_path: PathBuf,
189+
snapshot_options: SnapshotOptions,
190+
) {
187191
// NOTE(bartlomieju): ordering is important here, keep it in sync with
188-
// `runtime/worker.rs`, `runtime/web_worker.rs` and `cli/build.rs`!
192+
// `runtime/worker.rs`, `runtime/web_worker.rs` and `runtime/snapshot.rs`!
189193
let fs = std::sync::Arc::new(deno_fs::RealFs);
190194
let mut extensions: Vec<Extension> = vec![
191195
deno_webidl::deno_webidl::init_ops_and_esm(),
@@ -234,7 +238,7 @@ pub fn create_runtime_snapshot(snapshot_path: PathBuf) {
234238
ops::signal::deno_signal::init_ops(),
235239
ops::tty::deno_tty::init_ops(),
236240
ops::http::deno_http_runtime::init_ops(),
237-
ops::bootstrap::deno_bootstrap::init_ops(None),
241+
ops::bootstrap::deno_bootstrap::init_ops(Some(snapshot_options)),
238242
];
239243

240244
for extension in &mut extensions {

runtime/web_worker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ impl WebWorker {
397397
});
398398

399399
// NOTE(bartlomieju): ordering is important here, keep it in sync with
400-
// `runtime/build.rs`, `runtime/worker.rs` and `cli/build.rs`!
400+
// `runtime/build.rs`, `runtime/worker.rs` and `runtime/snapshot.rs`!
401401

402402
let mut extensions = vec![
403403
// Web APIs

runtime/worker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl MainWorker {
297297
});
298298

299299
// NOTE(bartlomieju): ordering is important here, keep it in sync with
300-
// `runtime/build.rs`, `runtime/web_worker.rs` and `cli/build.rs`!
300+
// `runtime/build.rs`, `runtime/web_worker.rs` and `runtime/snapshot.rs`!
301301
let mut extensions = vec![
302302
// Web APIs
303303
deno_webidl::deno_webidl::init_ops_and_esm(),

0 commit comments

Comments
 (0)