2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
use crate::args::CliOptions;
|
2022-06-28 16:45:55 -04:00
|
|
|
use crate::args::DenoSubcommand;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::Flags;
|
2022-11-25 17:00:28 -05:00
|
|
|
use crate::args::Lockfile;
|
2022-11-25 18:29:48 -05:00
|
|
|
use crate::args::TsConfigType;
|
|
|
|
use crate::args::TsTypeLib;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::TypeCheckMode;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::cache;
|
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
|
|
|
use crate::cache::Caches;
|
2022-11-25 19:04:30 -05:00
|
|
|
use crate::cache::DenoDir;
|
2022-07-12 18:58:39 -04:00
|
|
|
use crate::cache::EmitCache;
|
2022-07-19 11:58:18 -04:00
|
|
|
use crate::cache::FastInsecureHasher;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::cache::HttpCache;
|
2022-10-01 06:15:56 -04:00
|
|
|
use crate::cache::NodeAnalysisCache;
|
2022-08-22 12:14:59 -04:00
|
|
|
use crate::cache::ParsedSourceCache;
|
2022-07-12 18:58:39 -04:00
|
|
|
use crate::cache::TypeCheckCache;
|
2022-07-19 11:58:18 -04:00
|
|
|
use crate::emit::emit_parsed_source;
|
2020-11-05 19:38:21 -05:00
|
|
|
use crate::file_fetcher::FileFetcher;
|
2023-02-22 14:15:25 -05:00
|
|
|
use crate::graph_util::build_graph_with_npm_resolution;
|
2021-12-16 05:45:41 -05:00
|
|
|
use crate::graph_util::graph_lock_or_exit;
|
2023-02-09 22:00:23 -05:00
|
|
|
use crate::graph_util::graph_valid_with_cli_options;
|
2023-02-24 14:42:45 -05:00
|
|
|
use crate::graph_util::ModuleGraphContainer;
|
2022-11-18 17:28:14 -05:00
|
|
|
use crate::http_util::HttpClient;
|
2022-08-20 11:31:33 -04:00
|
|
|
use crate::node;
|
2022-09-06 06:57:28 -04:00
|
|
|
use crate::node::NodeResolution;
|
2023-03-12 23:32:59 -04:00
|
|
|
use crate::npm::create_npm_fs_resolver;
|
2023-04-12 08:36:11 -04:00
|
|
|
use crate::npm::CliNpmRegistryApi;
|
2022-09-13 11:59:01 -04:00
|
|
|
use crate::npm::NpmCache;
|
2022-08-20 11:31:33 -04:00
|
|
|
use crate::npm::NpmPackageResolver;
|
2023-03-12 23:32:59 -04:00
|
|
|
use crate::npm::NpmResolution;
|
2023-02-24 19:35:43 -05:00
|
|
|
use crate::npm::PackageJsonDepsInstaller;
|
2023-02-15 11:30:54 -05:00
|
|
|
use crate::resolver::CliGraphResolver;
|
2022-09-02 10:54:40 -04:00
|
|
|
use crate::tools::check;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
2022-12-12 20:52:10 -05:00
|
|
|
use crate::util::progress_bar::ProgressBarStyle;
|
2020-10-22 20:50:15 -04:00
|
|
|
|
2022-08-23 10:30:14 -04:00
|
|
|
use deno_ast::MediaType;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::error::custom_error;
|
2022-10-21 11:20:18 -04:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-07-06 23:48:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2022-12-09 09:40:48 -05:00
|
|
|
use deno_core::resolve_url_or_path;
|
2021-09-29 04:47:24 -04:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-09-10 21:38:24 -04:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2021-12-16 05:45:41 -05:00
|
|
|
use deno_graph::source::Loader;
|
2022-11-02 10:47:02 -04:00
|
|
|
use deno_graph::source::Resolver;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::Module;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::ModuleGraph;
|
|
|
|
use deno_graph::Resolution;
|
2021-09-10 21:38:24 -04:00
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
2022-11-30 18:07:32 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2021-12-01 11:13:11 -05:00
|
|
|
use deno_runtime::deno_tls::rustls::RootCertStore;
|
2021-09-10 21:38:24 -04:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
|
|
|
use deno_runtime::inspector_server::InspectorServer;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2021-09-10 21:38:24 -04:00
|
|
|
use import_map::ImportMap;
|
2022-01-13 18:17:56 -05:00
|
|
|
use log::warn;
|
2023-02-15 11:30:54 -05:00
|
|
|
use std::borrow::Cow;
|
2021-06-19 10:14:43 -04:00
|
|
|
use std::collections::HashSet;
|
2021-09-24 11:10:42 -04:00
|
|
|
use std::ops::Deref;
|
2022-06-08 06:07:25 -04:00
|
|
|
use std::path::PathBuf;
|
2019-11-04 10:38:52 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// This structure represents state of single "deno" program.
|
|
|
|
///
|
|
|
|
/// It is shared by all created workers (thus V8 isolates).
|
2021-09-24 11:10:42 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ProcState(Arc<Inner>);
|
|
|
|
|
|
|
|
pub struct Inner {
|
2022-11-25 19:04:30 -05:00
|
|
|
pub dir: DenoDir,
|
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
|
|
|
pub caches: Caches,
|
2023-02-03 14:15:16 -05:00
|
|
|
pub file_fetcher: Arc<FileFetcher>,
|
2022-12-12 21:30:44 -05:00
|
|
|
pub http_client: HttpClient,
|
2022-06-29 11:51:11 -04:00
|
|
|
pub options: Arc<CliOptions>,
|
2022-07-19 11:58:18 -04:00
|
|
|
pub emit_cache: EmitCache,
|
|
|
|
pub emit_options: deno_ast::EmitOptions,
|
|
|
|
pub emit_options_hash: u64,
|
2023-02-24 14:42:45 -05:00
|
|
|
graph_container: ModuleGraphContainer,
|
2020-10-23 17:01:54 -04:00
|
|
|
pub lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2021-11-08 20:26:39 -05:00
|
|
|
pub maybe_import_map: Option<Arc<ImportMap>>,
|
2020-09-25 04:24:51 -04:00
|
|
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2022-06-28 16:45:55 -04:00
|
|
|
pub root_cert_store: RootCertStore,
|
2021-07-05 09:34:37 -04:00
|
|
|
pub blob_store: BlobStore,
|
2021-05-22 12:08:24 -04:00
|
|
|
pub broadcast_channel: InMemoryBroadcastChannel,
|
2021-07-06 13:42:52 -04:00
|
|
|
pub shared_array_buffer_store: SharedArrayBufferStore,
|
2021-09-29 04:47:24 -04:00
|
|
|
pub compiled_wasm_module_store: CompiledWasmModuleStore,
|
2022-08-22 12:14:59 -04:00
|
|
|
pub parsed_source_cache: ParsedSourceCache,
|
2023-02-15 11:30:54 -05:00
|
|
|
pub resolver: Arc<CliGraphResolver>,
|
2022-06-08 06:07:25 -04:00
|
|
|
maybe_file_watcher_reporter: Option<FileWatcherReporter>,
|
2022-10-01 06:15:56 -04:00
|
|
|
pub node_analysis_cache: NodeAnalysisCache,
|
2023-04-12 08:36:11 -04:00
|
|
|
pub npm_api: CliNpmRegistryApi,
|
2022-09-13 11:59:01 -04:00
|
|
|
pub npm_cache: NpmCache,
|
|
|
|
pub npm_resolver: NpmPackageResolver,
|
2023-03-12 23:32:59 -04:00
|
|
|
pub npm_resolution: NpmResolution,
|
2023-02-24 19:35:43 -05:00
|
|
|
pub package_json_deps_installer: PackageJsonDepsInstaller,
|
2022-08-20 11:31:33 -04:00
|
|
|
pub cjs_resolutions: Mutex<HashSet<ModuleSpecifier>>,
|
2022-09-09 15:57:39 -04:00
|
|
|
progress_bar: ProgressBar,
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
impl Deref for ProcState {
|
|
|
|
type Target = Arc<Inner>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcState {
|
2023-04-12 14:54:28 -04:00
|
|
|
pub async fn from_cli_options(
|
2022-06-29 11:51:11 -04:00
|
|
|
options: Arc<CliOptions>,
|
2022-06-28 16:45:55 -04:00
|
|
|
) -> Result<Self, AnyError> {
|
2022-06-29 11:51:11 -04:00
|
|
|
Self::build_with_sender(options, None).await
|
2022-06-08 06:07:25 -04:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:54:28 -04:00
|
|
|
pub async fn from_flags(flags: Flags) -> Result<Self, AnyError> {
|
|
|
|
Self::from_cli_options(Arc::new(CliOptions::from_flags(flags)?)).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn from_flags_for_file_watcher(
|
2022-06-28 16:45:55 -04:00
|
|
|
flags: Flags,
|
2022-06-08 06:07:25 -04:00
|
|
|
files_to_watch_sender: tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>,
|
|
|
|
) -> Result<Self, AnyError> {
|
2022-06-28 16:45:55 -04:00
|
|
|
// resolve the config each time
|
2022-06-29 11:51:11 -04:00
|
|
|
let cli_options = Arc::new(CliOptions::from_flags(flags)?);
|
2022-06-28 16:45:55 -04:00
|
|
|
let ps =
|
2022-06-29 11:51:11 -04:00
|
|
|
Self::build_with_sender(cli_options, Some(files_to_watch_sender.clone()))
|
2022-06-28 16:45:55 -04:00
|
|
|
.await?;
|
2023-02-03 14:15:16 -05:00
|
|
|
ps.init_watcher();
|
|
|
|
Ok(ps)
|
|
|
|
}
|
2022-06-08 06:07:25 -04:00
|
|
|
|
2023-02-03 14:15:16 -05:00
|
|
|
/// Reset all runtime state to its default. This should be used on file
|
|
|
|
/// watcher restarts.
|
|
|
|
pub fn reset_for_file_watcher(&mut self) {
|
|
|
|
self.0 = Arc::new(Inner {
|
|
|
|
dir: self.dir.clone(),
|
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
|
|
|
caches: self.caches.clone(),
|
2023-02-03 14:15:16 -05:00
|
|
|
options: self.options.clone(),
|
|
|
|
emit_cache: self.emit_cache.clone(),
|
|
|
|
emit_options_hash: self.emit_options_hash,
|
|
|
|
emit_options: self.emit_options.clone(),
|
|
|
|
file_fetcher: self.file_fetcher.clone(),
|
|
|
|
http_client: self.http_client.clone(),
|
2023-02-24 14:42:45 -05:00
|
|
|
graph_container: Default::default(),
|
2023-02-03 14:15:16 -05:00
|
|
|
lockfile: self.lockfile.clone(),
|
|
|
|
maybe_import_map: self.maybe_import_map.clone(),
|
|
|
|
maybe_inspector_server: self.maybe_inspector_server.clone(),
|
|
|
|
root_cert_store: self.root_cert_store.clone(),
|
2023-03-17 21:58:25 -04:00
|
|
|
blob_store: self.blob_store.clone(),
|
2023-02-03 14:15:16 -05:00
|
|
|
broadcast_channel: Default::default(),
|
|
|
|
shared_array_buffer_store: Default::default(),
|
|
|
|
compiled_wasm_module_store: Default::default(),
|
|
|
|
parsed_source_cache: self.parsed_source_cache.reset_for_file_watcher(),
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: self.resolver.clone(),
|
2023-02-03 14:15:16 -05:00
|
|
|
maybe_file_watcher_reporter: self.maybe_file_watcher_reporter.clone(),
|
|
|
|
node_analysis_cache: self.node_analysis_cache.clone(),
|
2023-03-12 23:32:59 -04:00
|
|
|
npm_api: self.npm_api.clone(),
|
2023-02-03 14:15:16 -05:00
|
|
|
npm_cache: self.npm_cache.clone(),
|
|
|
|
npm_resolver: self.npm_resolver.clone(),
|
2023-03-12 23:32:59 -04:00
|
|
|
npm_resolution: self.npm_resolution.clone(),
|
2023-02-24 19:35:43 -05:00
|
|
|
package_json_deps_installer: self.package_json_deps_installer.clone(),
|
2023-02-03 14:15:16 -05:00
|
|
|
cjs_resolutions: Default::default(),
|
|
|
|
progress_bar: self.progress_bar.clone(),
|
|
|
|
});
|
|
|
|
self.init_watcher();
|
|
|
|
}
|
2022-06-08 06:07:25 -04:00
|
|
|
|
2023-02-03 14:15:16 -05:00
|
|
|
// Add invariant files like the import map and explicit watch flag list to
|
|
|
|
// the watcher. Dedup for build_for_file_watcher and reset_for_file_watcher.
|
|
|
|
fn init_watcher(&self) {
|
|
|
|
let files_to_watch_sender = match &self.0.maybe_file_watcher_reporter {
|
|
|
|
Some(reporter) => &reporter.sender,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
if let Some(watch_paths) = self.options.watch_paths() {
|
|
|
|
files_to_watch_sender.send(watch_paths.clone()).unwrap();
|
|
|
|
}
|
|
|
|
if let Ok(Some(import_map_path)) = self
|
2022-06-29 11:51:11 -04:00
|
|
|
.options
|
2022-06-29 20:41:48 -04:00
|
|
|
.resolve_import_map_specifier()
|
2022-06-28 16:45:55 -04:00
|
|
|
.map(|ms| ms.and_then(|ref s| s.to_file_path().ok()))
|
2022-06-08 06:07:25 -04:00
|
|
|
{
|
2023-02-03 14:15:16 -05:00
|
|
|
files_to_watch_sender.send(vec![import_map_path]).unwrap();
|
2022-06-08 06:07:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn build_with_sender(
|
2022-06-29 11:51:11 -04:00
|
|
|
cli_options: Arc<CliOptions>,
|
2022-06-08 06:07:25 -04:00
|
|
|
maybe_sender: Option<tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>>,
|
|
|
|
) -> Result<Self, AnyError> {
|
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
|
|
|
let dir = cli_options.resolve_deno_dir()?;
|
|
|
|
let caches = Caches::default();
|
|
|
|
// Warm up the caches we know we'll likely need based on the CLI mode
|
|
|
|
match cli_options.sub_command() {
|
|
|
|
DenoSubcommand::Run(_) => {
|
|
|
|
_ = caches.dep_analysis_db(&dir);
|
|
|
|
_ = caches.node_analysis_db(&dir);
|
|
|
|
}
|
|
|
|
DenoSubcommand::Check(_) => {
|
|
|
|
_ = caches.dep_analysis_db(&dir);
|
|
|
|
_ = caches.node_analysis_db(&dir);
|
|
|
|
_ = caches.type_checking_cache_db(&dir);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2021-07-05 09:34:37 -04:00
|
|
|
let blob_store = BlobStore::default();
|
2021-05-22 12:08:24 -04:00
|
|
|
let broadcast_channel = InMemoryBroadcastChannel::default();
|
2021-07-06 13:42:52 -04:00
|
|
|
let shared_array_buffer_store = SharedArrayBufferStore::default();
|
2021-09-29 04:47:24 -04:00
|
|
|
let compiled_wasm_module_store = CompiledWasmModuleStore::default();
|
2022-11-25 19:04:30 -05:00
|
|
|
let deps_cache_location = dir.deps_folder_path();
|
2022-11-28 17:28:54 -05:00
|
|
|
let http_cache = HttpCache::new(&deps_cache_location);
|
2022-06-29 11:51:11 -04:00
|
|
|
let root_cert_store = cli_options.resolve_root_cert_store()?;
|
|
|
|
let cache_usage = cli_options.cache_setting();
|
2022-12-12 20:52:10 -05:00
|
|
|
let progress_bar = ProgressBar::new(ProgressBarStyle::TextOnly);
|
2022-11-18 17:28:14 -05:00
|
|
|
let http_client = HttpClient::new(
|
|
|
|
Some(root_cert_store.clone()),
|
2022-12-08 11:50:09 -05:00
|
|
|
cli_options.unsafely_ignore_certificate_errors().clone(),
|
2022-11-18 17:28:14 -05:00
|
|
|
)?;
|
2020-11-05 19:38:21 -05:00
|
|
|
let file_fetcher = FileFetcher::new(
|
2020-02-19 08:17:13 -05:00
|
|
|
http_cache,
|
2020-11-05 19:38:21 -05:00
|
|
|
cache_usage,
|
2022-06-29 11:51:11 -04:00
|
|
|
!cli_options.no_remote(),
|
2022-11-18 17:28:14 -05:00
|
|
|
http_client.clone(),
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store.clone(),
|
2022-09-09 15:57:39 -04:00
|
|
|
Some(progress_bar.clone()),
|
2023-01-25 16:51:04 -05:00
|
|
|
);
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2022-11-02 11:32:30 -04:00
|
|
|
let lockfile = cli_options.maybe_lock_file();
|
2020-05-29 10:32:15 -04:00
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
let npm_registry_url = CliNpmRegistryApi::default_url().to_owned();
|
2023-02-22 14:15:25 -05:00
|
|
|
let npm_cache = NpmCache::from_deno_dir(
|
|
|
|
&dir,
|
|
|
|
cli_options.cache_setting(),
|
|
|
|
http_client.clone(),
|
|
|
|
progress_bar.clone(),
|
|
|
|
);
|
2023-04-12 08:36:11 -04:00
|
|
|
let npm_api = CliNpmRegistryApi::new(
|
2023-03-12 23:32:59 -04:00
|
|
|
npm_registry_url.clone(),
|
2023-02-22 14:15:25 -05:00
|
|
|
npm_cache.clone(),
|
|
|
|
http_client.clone(),
|
|
|
|
progress_bar.clone(),
|
|
|
|
);
|
2023-03-12 23:32:59 -04:00
|
|
|
let npm_snapshot = cli_options
|
|
|
|
.resolve_npm_resolution_snapshot(&npm_api)
|
|
|
|
.await?;
|
|
|
|
let npm_resolution = NpmResolution::new(
|
|
|
|
npm_api.clone(),
|
|
|
|
npm_snapshot,
|
|
|
|
lockfile.as_ref().cloned(),
|
|
|
|
);
|
|
|
|
let npm_fs_resolver = create_npm_fs_resolver(
|
|
|
|
npm_cache,
|
2023-03-13 14:18:29 -04:00
|
|
|
&progress_bar,
|
2023-03-12 23:32:59 -04:00
|
|
|
npm_registry_url,
|
|
|
|
npm_resolution.clone(),
|
2023-02-22 20:16:16 -05:00
|
|
|
cli_options.node_modules_dir_path(),
|
2023-03-12 23:32:59 -04:00
|
|
|
);
|
|
|
|
let npm_resolver = NpmPackageResolver::new(
|
|
|
|
npm_resolution.clone(),
|
|
|
|
npm_fs_resolver,
|
2023-02-22 14:15:25 -05:00
|
|
|
lockfile.as_ref().cloned(),
|
2023-03-12 23:32:59 -04:00
|
|
|
);
|
2023-02-24 19:35:43 -05:00
|
|
|
let package_json_deps_installer = PackageJsonDepsInstaller::new(
|
2023-03-12 23:32:59 -04:00
|
|
|
npm_api.clone(),
|
|
|
|
npm_resolution.clone(),
|
2023-03-03 17:27:05 -05:00
|
|
|
cli_options.maybe_package_json_deps(),
|
2023-02-24 19:35:43 -05:00
|
|
|
);
|
2023-01-25 15:13:40 -05:00
|
|
|
let maybe_import_map = cli_options
|
|
|
|
.resolve_import_map(&file_fetcher)
|
|
|
|
.await?
|
|
|
|
.map(Arc::new);
|
2022-06-28 16:45:55 -04:00
|
|
|
let maybe_inspector_server =
|
2022-06-29 11:51:11 -04:00
|
|
|
cli_options.resolve_inspector_server().map(Arc::new);
|
2020-09-25 04:24:51 -04:00
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
let resolver = Arc::new(CliGraphResolver::new(
|
2022-11-02 10:47:02 -04:00
|
|
|
cli_options.to_maybe_jsx_import_source_config(),
|
|
|
|
maybe_import_map.clone(),
|
2023-02-22 14:15:25 -05:00
|
|
|
cli_options.no_npm(),
|
2023-03-12 23:32:59 -04:00
|
|
|
npm_api.clone(),
|
|
|
|
npm_resolution.clone(),
|
2023-02-24 19:35:43 -05:00
|
|
|
package_json_deps_installer.clone(),
|
2023-02-15 11:30:54 -05:00
|
|
|
));
|
2021-11-24 10:55:10 -05:00
|
|
|
|
2022-06-08 06:07:25 -04:00
|
|
|
let maybe_file_watcher_reporter =
|
|
|
|
maybe_sender.map(|sender| FileWatcherReporter {
|
|
|
|
sender,
|
|
|
|
file_paths: Arc::new(Mutex::new(vec![])),
|
|
|
|
});
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
let ts_config_result =
|
|
|
|
cli_options.resolve_ts_config_for_emit(TsConfigType::Emit)?;
|
|
|
|
if let Some(ignored_options) = ts_config_result.maybe_ignored_options {
|
|
|
|
warn!("{}", ignored_options);
|
|
|
|
}
|
|
|
|
let emit_cache = EmitCache::new(dir.gen_cache.clone());
|
2022-08-22 12:14:59 -04:00
|
|
|
let parsed_source_cache =
|
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
|
|
|
ParsedSourceCache::new(caches.dep_analysis_db(&dir));
|
2022-09-13 11:59:01 -04:00
|
|
|
let npm_cache = NpmCache::from_deno_dir(
|
2022-08-22 11:35:04 -04:00
|
|
|
&dir,
|
2022-09-13 11:59:01 -04:00
|
|
|
cli_options.cache_setting(),
|
2022-11-18 17:28:14 -05:00
|
|
|
http_client.clone(),
|
2022-09-13 11:59:01 -04:00
|
|
|
progress_bar.clone(),
|
|
|
|
);
|
2022-10-01 06:15:56 -04:00
|
|
|
let node_analysis_cache =
|
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
|
|
|
NodeAnalysisCache::new(caches.node_analysis_db(&dir));
|
2022-07-19 11:58:18 -04:00
|
|
|
|
2022-09-07 15:06:18 -04:00
|
|
|
let emit_options: deno_ast::EmitOptions = ts_config_result.ts_config.into();
|
2021-09-24 11:10:42 -04:00
|
|
|
Ok(ProcState(Arc::new(Inner {
|
2019-11-04 10:38:52 -05:00
|
|
|
dir,
|
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
|
|
|
caches,
|
2022-06-29 11:51:11 -04:00
|
|
|
options: cli_options,
|
2022-07-19 11:58:18 -04:00
|
|
|
emit_cache,
|
|
|
|
emit_options_hash: FastInsecureHasher::new()
|
2022-09-07 15:06:18 -04:00
|
|
|
.write_hashable(&emit_options)
|
2022-07-19 11:58:18 -04:00
|
|
|
.finish(),
|
2022-09-07 15:06:18 -04:00
|
|
|
emit_options,
|
2023-02-03 14:15:16 -05:00
|
|
|
file_fetcher: Arc::new(file_fetcher),
|
2022-12-12 21:30:44 -05:00
|
|
|
http_client,
|
2023-02-24 14:42:45 -05:00
|
|
|
graph_container: Default::default(),
|
2019-11-04 10:38:52 -05:00
|
|
|
lockfile,
|
2020-05-29 10:32:15 -04:00
|
|
|
maybe_import_map,
|
2020-09-25 04:24:51 -04:00
|
|
|
maybe_inspector_server,
|
2022-06-28 16:45:55 -04:00
|
|
|
root_cert_store,
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store,
|
2021-05-22 12:08:24 -04:00
|
|
|
broadcast_channel,
|
2021-07-06 13:42:52 -04:00
|
|
|
shared_array_buffer_store,
|
2021-09-29 04:47:24 -04:00
|
|
|
compiled_wasm_module_store,
|
2022-08-22 12:14:59 -04:00
|
|
|
parsed_source_cache,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver,
|
2022-06-08 06:07:25 -04:00
|
|
|
maybe_file_watcher_reporter,
|
2022-10-01 06:15:56 -04:00
|
|
|
node_analysis_cache,
|
2023-03-12 23:32:59 -04:00
|
|
|
npm_api,
|
2022-09-13 11:59:01 -04:00
|
|
|
npm_cache,
|
2022-08-20 11:31:33 -04:00
|
|
|
npm_resolver,
|
2023-03-12 23:32:59 -04:00
|
|
|
npm_resolution,
|
2023-02-24 19:35:43 -05:00
|
|
|
package_json_deps_installer,
|
2022-08-20 11:31:33 -04:00
|
|
|
cjs_resolutions: Default::default(),
|
2022-09-09 15:57:39 -04:00
|
|
|
progress_bar,
|
2021-09-24 11:10:42 -04:00
|
|
|
})))
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2021-11-15 18:25:52 -05:00
|
|
|
/// This method must be called for a module or a static importer of that
|
|
|
|
/// module before attempting to `load()` it from a `JsRuntime`. It will
|
2021-12-16 05:45:41 -05:00
|
|
|
/// populate `self.graph_data` in memory with the necessary source code, write
|
|
|
|
/// emits where necessary or report any module graph / type checking errors.
|
2022-11-10 16:03:28 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub async fn prepare_module_load(
|
2021-09-24 11:10:42 -04:00
|
|
|
&self,
|
2022-03-10 20:33:02 -05:00
|
|
|
roots: Vec<ModuleSpecifier>,
|
2021-10-10 17:26:22 -04:00
|
|
|
is_dynamic: bool,
|
2022-06-28 16:45:55 -04:00
|
|
|
lib: TsTypeLib,
|
2023-01-07 11:25:34 -05:00
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2021-04-28 14:17:04 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-11-23 13:34:44 -05:00
|
|
|
log::debug!("Preparing module load.");
|
2022-10-24 12:40:27 -04:00
|
|
|
let _pb_clear_guard = self.progress_bar.clear_guard();
|
2022-10-28 15:53:55 -04:00
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
let mut cache = cache::FetchCacher::new(
|
2022-07-19 11:58:18 -04:00
|
|
|
self.emit_cache.clone(),
|
2021-10-10 17:26:22 -04:00
|
|
|
self.file_fetcher.clone(),
|
2023-03-22 10:15:53 -04:00
|
|
|
self.options.resolve_file_header_overrides(),
|
2023-01-05 12:33:09 -05:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2023-02-22 20:16:16 -05:00
|
|
|
self.options.node_modules_dir_specifier(),
|
2021-10-10 17:26:22 -04:00
|
|
|
);
|
2022-06-29 11:51:11 -04:00
|
|
|
let maybe_imports = self.options.to_maybe_imports()?;
|
2023-02-22 14:15:25 -05:00
|
|
|
let graph_resolver = self.resolver.as_graph_resolver();
|
|
|
|
let graph_npm_resolver = self.resolver.as_graph_npm_resolver();
|
2022-06-08 06:07:25 -04:00
|
|
|
let maybe_file_watcher_reporter: Option<&dyn deno_graph::source::Reporter> =
|
|
|
|
if let Some(reporter) = &self.maybe_file_watcher_reporter {
|
|
|
|
Some(reporter)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2022-08-22 12:14:59 -04:00
|
|
|
let analyzer = self.parsed_source_cache.as_analyzer();
|
2023-02-09 22:00:23 -05:00
|
|
|
|
2022-11-23 13:34:44 -05:00
|
|
|
log::debug!("Creating module graph.");
|
2023-02-24 14:42:45 -05:00
|
|
|
let mut graph_update_permit =
|
|
|
|
self.graph_container.acquire_update_permit().await;
|
|
|
|
let graph = graph_update_permit.graph_mut();
|
2023-02-09 22:00:23 -05:00
|
|
|
|
|
|
|
// Determine any modules that have already been emitted this session and
|
|
|
|
// should be skipped.
|
|
|
|
let reload_exclusions: HashSet<ModuleSpecifier> =
|
|
|
|
graph.specifiers().map(|(s, _)| s.clone()).collect();
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
build_graph_with_npm_resolution(
|
2023-02-24 14:42:45 -05:00
|
|
|
graph,
|
2023-04-11 18:10:51 -04:00
|
|
|
&self.resolver,
|
2023-02-22 14:15:25 -05:00
|
|
|
&self.npm_resolver,
|
|
|
|
roots.clone(),
|
|
|
|
&mut cache,
|
|
|
|
deno_graph::BuildOptions {
|
|
|
|
is_dynamic,
|
|
|
|
imports: maybe_imports,
|
|
|
|
resolver: Some(graph_resolver),
|
|
|
|
npm_resolver: Some(graph_npm_resolver),
|
|
|
|
module_analyzer: Some(&*analyzer),
|
|
|
|
reporter: maybe_file_watcher_reporter,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2022-02-27 08:38:45 -05:00
|
|
|
|
2022-12-06 14:12:51 -05:00
|
|
|
// If there is a lockfile, validate the integrity of all the modules.
|
|
|
|
if let Some(lockfile) = &self.lockfile {
|
2023-02-24 14:42:45 -05:00
|
|
|
graph_lock_or_exit(graph, &mut lockfile.lock());
|
2022-12-06 14:12:51 -05:00
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2023-02-24 14:42:45 -05:00
|
|
|
graph_valid_with_cli_options(graph, &roots, &self.options)?;
|
|
|
|
// save the graph and get a reference to the new graph
|
|
|
|
let graph = graph_update_permit.commit();
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
if graph.has_node_specifier
|
2023-01-24 09:05:54 -05:00
|
|
|
&& self.options.type_check_mode() != TypeCheckMode::None
|
|
|
|
{
|
|
|
|
self
|
|
|
|
.npm_resolver
|
|
|
|
.inject_synthetic_types_node_package()
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2022-10-24 12:40:27 -04:00
|
|
|
drop(_pb_clear_guard);
|
2022-09-09 15:57:39 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
// type check if necessary
|
2023-02-09 22:00:23 -05:00
|
|
|
if self.options.type_check_mode() != TypeCheckMode::None
|
2023-02-24 14:42:45 -05:00
|
|
|
&& !self.graph_container.is_type_checked(&roots, lib)
|
2023-02-09 22:00:23 -05:00
|
|
|
{
|
2022-11-23 13:34:44 -05:00
|
|
|
log::debug!("Type checking.");
|
2022-07-19 11:58:18 -04:00
|
|
|
let maybe_config_specifier = self.options.maybe_config_file_specifier();
|
2023-02-24 14:42:45 -05:00
|
|
|
let graph = Arc::new(graph.segment(&roots));
|
2022-09-02 10:54:40 -04:00
|
|
|
let options = check::CheckOptions {
|
2022-07-19 11:58:18 -04:00
|
|
|
type_check_mode: self.options.type_check_mode(),
|
|
|
|
debug: self.options.log_level() == Some(log::Level::Debug),
|
|
|
|
maybe_config_specifier,
|
|
|
|
ts_config: self
|
|
|
|
.options
|
|
|
|
.resolve_ts_config_for_emit(TsConfigType::Check { lib })?
|
|
|
|
.ts_config,
|
|
|
|
log_checks: true,
|
|
|
|
reload: self.options.reload_flag()
|
2023-01-24 08:23:19 -05:00
|
|
|
&& !roots.iter().all(|r| reload_exclusions.contains(r)),
|
2021-12-16 05:45:41 -05:00
|
|
|
};
|
2022-07-19 11:58:18 -04:00
|
|
|
let check_cache =
|
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
|
|
|
TypeCheckCache::new(self.caches.type_checking_cache_db(&self.dir));
|
2023-02-09 22:00:23 -05:00
|
|
|
let check_result =
|
|
|
|
check::check(graph, &check_cache, &self.npm_resolver, options)?;
|
2023-02-24 14:42:45 -05:00
|
|
|
self.graph_container.set_type_checked(&roots, lib);
|
2022-07-19 11:58:18 -04:00
|
|
|
if !check_result.diagnostics.is_empty() {
|
|
|
|
return Err(anyhow!(check_result.diagnostics));
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
log::debug!("{}", check_result.stats);
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// any updates to the lockfile should be updated now
|
2021-04-28 14:17:04 -04:00
|
|
|
if let Some(ref lockfile) = self.lockfile {
|
2021-07-06 23:48:01 -04:00
|
|
|
let g = lockfile.lock();
|
2021-04-28 14:17:04 -04:00
|
|
|
g.write()?;
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:34:44 -05:00
|
|
|
log::debug!("Prepared module load.");
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
/// Helper around prepare_module_load that loads and type checks
|
|
|
|
/// the provided files.
|
|
|
|
pub async fn load_and_type_check_files(
|
|
|
|
&self,
|
|
|
|
files: &[String],
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let lib = self.options.ts_type_lib_window();
|
|
|
|
|
|
|
|
let specifiers = files
|
|
|
|
.iter()
|
2023-03-13 21:12:09 -04:00
|
|
|
.map(|file| resolve_url_or_path(file, self.options.initial_cwd()))
|
2022-12-09 09:40:48 -05:00
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
self
|
|
|
|
.prepare_module_load(
|
|
|
|
specifiers,
|
|
|
|
false,
|
|
|
|
lib,
|
2023-01-07 11:25:34 -05:00
|
|
|
PermissionsContainer::allow_all(),
|
|
|
|
PermissionsContainer::allow_all(),
|
2022-12-09 09:40:48 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
fn handle_node_resolve_result(
|
|
|
|
&self,
|
2022-09-06 06:57:28 -04:00
|
|
|
result: Result<Option<node::NodeResolution>, AnyError>,
|
2022-08-20 11:31:33 -04:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
let response = match result? {
|
|
|
|
Some(response) => response,
|
2022-10-21 11:20:18 -04:00
|
|
|
None => return Err(generic_error("not found")),
|
2022-08-20 11:31:33 -04:00
|
|
|
};
|
2022-09-06 06:57:28 -04:00
|
|
|
if let NodeResolution::CommonJs(specifier) = &response {
|
2022-08-20 11:31:33 -04:00
|
|
|
// remember that this was a common js resolution
|
|
|
|
self.cjs_resolutions.lock().insert(specifier.clone());
|
2022-09-06 06:57:28 -04:00
|
|
|
} else if let NodeResolution::BuiltIn(specifier) = &response {
|
|
|
|
return node::resolve_builtin_node_module(specifier);
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2022-09-06 06:57:28 -04:00
|
|
|
Ok(response.into_url())
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn resolve(
|
2021-09-24 11:10:42 -04:00
|
|
|
&self,
|
2021-10-10 17:26:22 -04:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2023-01-10 08:35:44 -05:00
|
|
|
permissions: &mut PermissionsContainer,
|
2021-10-10 17:26:22 -04:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2023-03-14 13:18:01 -04:00
|
|
|
// TODO(bartlomieju): ideally we shouldn't need to call `current_dir()` on each
|
|
|
|
// call - maybe it should be caller's responsibility to pass it as an arg?
|
|
|
|
let cwd = std::env::current_dir().context("Unable to get CWD")?;
|
|
|
|
let referrer_result = deno_core::resolve_url_or_path(referrer, &cwd);
|
|
|
|
|
|
|
|
if let Ok(referrer) = referrer_result.as_ref() {
|
|
|
|
if self.npm_resolver.in_npm_package(referrer) {
|
2022-08-20 11:31:33 -04:00
|
|
|
// we're in an npm package, so use node resolution
|
|
|
|
return self
|
|
|
|
.handle_node_resolve_result(node::node_resolve(
|
|
|
|
specifier,
|
2023-03-14 13:18:01 -04:00
|
|
|
referrer,
|
2022-11-30 18:07:32 -05:00
|
|
|
NodeResolutionMode::Execution,
|
2022-08-20 11:31:33 -04:00
|
|
|
&self.npm_resolver,
|
2023-01-10 08:35:44 -05:00
|
|
|
permissions,
|
2022-08-20 11:31:33 -04:00
|
|
|
))
|
|
|
|
.with_context(|| {
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("Could not resolve '{specifier}' from '{referrer}'.")
|
2022-08-20 11:31:33 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:42:45 -05:00
|
|
|
let graph = self.graph_container.graph();
|
2023-03-14 13:18:01 -04:00
|
|
|
let maybe_resolved = match graph.get(referrer) {
|
2023-02-23 09:48:29 -05:00
|
|
|
Some(Module::Esm(module)) => {
|
|
|
|
module.dependencies.get(specifier).map(|d| &d.maybe_code)
|
|
|
|
}
|
2021-11-23 16:30:52 -05:00
|
|
|
_ => None,
|
2021-10-19 10:01:46 -04:00
|
|
|
};
|
|
|
|
|
2021-11-23 16:30:52 -05:00
|
|
|
match maybe_resolved {
|
2023-02-23 09:48:29 -05:00
|
|
|
Some(Resolution::Ok(resolved)) => {
|
2023-02-09 22:00:23 -05:00
|
|
|
let specifier = &resolved.specifier;
|
2023-02-14 10:48:27 -05:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
return match graph.get(specifier) {
|
2023-02-23 09:48:29 -05:00
|
|
|
Some(Module::Npm(module)) => self
|
|
|
|
.handle_node_resolve_result(node::node_resolve_npm_reference(
|
|
|
|
&module.nv_reference,
|
|
|
|
NodeResolutionMode::Execution,
|
|
|
|
&self.npm_resolver,
|
|
|
|
permissions,
|
|
|
|
))
|
|
|
|
.with_context(|| {
|
|
|
|
format!("Could not resolve '{}'.", module.nv_reference)
|
|
|
|
}),
|
2023-02-22 14:15:25 -05:00
|
|
|
Some(Module::Node(module)) => {
|
|
|
|
node::resolve_builtin_node_module(&module.module_name)
|
|
|
|
}
|
|
|
|
Some(Module::Esm(module)) => Ok(module.specifier.clone()),
|
|
|
|
Some(Module::Json(module)) => Ok(module.specifier.clone()),
|
2023-02-24 19:35:43 -05:00
|
|
|
Some(Module::External(module)) => {
|
|
|
|
Ok(node::resolve_specifier_into_node_modules(&module.specifier))
|
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
None => Ok(specifier.clone()),
|
|
|
|
};
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2023-02-23 09:48:29 -05:00
|
|
|
Some(Resolution::Err(err)) => {
|
2021-11-23 16:30:52 -05:00
|
|
|
return Err(custom_error(
|
|
|
|
"TypeError",
|
|
|
|
format!("{}\n", err.to_string_with_range()),
|
|
|
|
))
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2023-02-23 09:48:29 -05:00
|
|
|
Some(Resolution::None) | None => {}
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2021-10-19 10:01:46 -04:00
|
|
|
|
2023-01-24 09:05:54 -05:00
|
|
|
// Built-in Node modules
|
|
|
|
if let Some(module_name) = specifier.strip_prefix("node:") {
|
|
|
|
return node::resolve_builtin_node_module(module_name);
|
|
|
|
}
|
|
|
|
|
2021-11-24 10:55:10 -05:00
|
|
|
// FIXME(bartlomieju): this is a hacky way to provide compatibility with REPL
|
|
|
|
// and `Deno.core.evalContext` API. Ideally we should always have a referrer filled
|
|
|
|
// but sadly that's not the case due to missing APIs in V8.
|
2022-12-13 07:53:32 -05:00
|
|
|
let is_repl = matches!(self.options.sub_command(), DenoSubcommand::Repl(_));
|
|
|
|
let referrer = if referrer.is_empty() && is_repl {
|
2023-03-14 13:18:01 -04:00
|
|
|
deno_core::resolve_path("./$deno$repl.ts", &cwd)?
|
2020-09-24 18:31:17 -04:00
|
|
|
} else {
|
2023-03-14 13:18:01 -04:00
|
|
|
referrer_result?
|
2020-10-22 20:50:15 -04:00
|
|
|
};
|
2021-11-24 10:55:10 -05:00
|
|
|
|
2022-12-13 07:53:32 -05:00
|
|
|
// FIXME(bartlomieju): this is another hack way to provide NPM specifier
|
|
|
|
// support in REPL. This should be fixed.
|
2023-02-15 11:30:54 -05:00
|
|
|
let resolution = self.resolver.resolve(specifier, &referrer);
|
|
|
|
|
2022-12-13 07:53:32 -05:00
|
|
|
if is_repl {
|
2023-02-15 11:30:54 -05:00
|
|
|
let specifier = resolution
|
2022-12-13 07:53:32 -05:00
|
|
|
.as_ref()
|
2023-02-15 11:30:54 -05:00
|
|
|
.ok()
|
|
|
|
.map(Cow::Borrowed)
|
|
|
|
.or_else(|| ModuleSpecifier::parse(specifier).ok().map(Cow::Owned));
|
2022-12-13 07:53:32 -05:00
|
|
|
if let Some(specifier) = specifier {
|
2023-02-21 12:03:48 -05:00
|
|
|
if let Ok(reference) =
|
|
|
|
NpmPackageReqReference::from_specifier(&specifier)
|
|
|
|
{
|
2023-03-12 23:32:59 -04:00
|
|
|
let reference =
|
|
|
|
self.npm_resolution.pkg_req_ref_to_nv_ref(reference)?;
|
2022-12-13 07:53:32 -05:00
|
|
|
return self
|
|
|
|
.handle_node_resolve_result(node::node_resolve_npm_reference(
|
|
|
|
&reference,
|
|
|
|
deno_runtime::deno_node::NodeResolutionMode::Execution,
|
|
|
|
&self.npm_resolver,
|
2023-01-10 08:35:44 -05:00
|
|
|
permissions,
|
2022-12-13 07:53:32 -05:00
|
|
|
))
|
2023-01-27 10:43:16 -05:00
|
|
|
.with_context(|| format!("Could not resolve '{reference}'."));
|
2022-12-13 07:53:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
resolution
|
2020-05-29 10:32:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
pub fn cache_module_emits(&self) -> Result<(), AnyError> {
|
2023-02-09 22:00:23 -05:00
|
|
|
let graph = self.graph();
|
|
|
|
for module in graph.modules() {
|
2023-02-22 14:15:25 -05:00
|
|
|
if let Module::Esm(module) = module {
|
|
|
|
let is_emittable = matches!(
|
2023-02-09 22:00:23 -05:00
|
|
|
module.media_type,
|
2022-08-23 10:30:14 -04:00
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Tsx
|
|
|
|
);
|
2023-02-22 14:15:25 -05:00
|
|
|
if is_emittable {
|
2022-08-23 10:30:14 -04:00
|
|
|
emit_parsed_source(
|
|
|
|
&self.emit_cache,
|
|
|
|
&self.parsed_source_cache,
|
2023-02-09 22:00:23 -05:00
|
|
|
&module.specifier,
|
|
|
|
module.media_type,
|
2023-02-22 14:15:25 -05:00
|
|
|
&module.source,
|
2022-08-23 10:30:14 -04:00
|
|
|
&self.emit_options,
|
|
|
|
self.emit_options_hash,
|
|
|
|
)?;
|
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
2021-10-19 10:01:46 -04:00
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
Ok(())
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2022-07-01 11:50:16 -04:00
|
|
|
|
2022-10-20 13:23:21 -04:00
|
|
|
/// Creates the default loader used for creating a graph.
|
2023-02-22 20:16:16 -05:00
|
|
|
pub fn create_graph_loader(&self) -> cache::FetchCacher {
|
|
|
|
cache::FetchCacher::new(
|
2022-07-19 11:58:18 -04:00
|
|
|
self.emit_cache.clone(),
|
2022-07-01 11:50:16 -04:00
|
|
|
self.file_fetcher.clone(),
|
2023-03-22 10:15:53 -04:00
|
|
|
self.options.resolve_file_header_overrides(),
|
2023-01-07 11:25:34 -05:00
|
|
|
PermissionsContainer::allow_all(),
|
|
|
|
PermissionsContainer::allow_all(),
|
2023-02-22 20:16:16 -05:00
|
|
|
self.options.node_modules_dir_specifier(),
|
|
|
|
)
|
2022-10-20 13:23:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_graph(
|
|
|
|
&self,
|
2023-01-24 08:23:19 -05:00
|
|
|
roots: Vec<ModuleSpecifier>,
|
2022-10-20 13:23:21 -04:00
|
|
|
) -> Result<deno_graph::ModuleGraph, AnyError> {
|
2023-02-22 20:16:16 -05:00
|
|
|
let mut cache = self.create_graph_loader();
|
2022-10-20 13:23:21 -04:00
|
|
|
self.create_graph_with_loader(roots, &mut cache).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_graph_with_loader(
|
|
|
|
&self,
|
2023-01-24 08:23:19 -05:00
|
|
|
roots: Vec<ModuleSpecifier>,
|
2022-10-20 13:23:21 -04:00
|
|
|
loader: &mut dyn Loader,
|
|
|
|
) -> Result<deno_graph::ModuleGraph, AnyError> {
|
2022-07-01 11:50:16 -04:00
|
|
|
let maybe_imports = self.options.to_maybe_imports()?;
|
2022-11-02 10:47:02 -04:00
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
let cli_resolver = CliGraphResolver::new(
|
2022-11-02 10:47:02 -04:00
|
|
|
self.options.to_maybe_jsx_import_source_config(),
|
|
|
|
self.maybe_import_map.clone(),
|
2023-02-22 14:15:25 -05:00
|
|
|
self.options.no_npm(),
|
2023-03-12 23:32:59 -04:00
|
|
|
self.npm_api.clone(),
|
|
|
|
self.npm_resolution.clone(),
|
2023-02-24 19:35:43 -05:00
|
|
|
self.package_json_deps_installer.clone(),
|
2022-11-02 10:47:02 -04:00
|
|
|
);
|
2023-02-15 11:30:54 -05:00
|
|
|
let graph_resolver = cli_resolver.as_graph_resolver();
|
2023-02-22 14:15:25 -05:00
|
|
|
let graph_npm_resolver = cli_resolver.as_graph_npm_resolver();
|
2022-08-22 12:14:59 -04:00
|
|
|
let analyzer = self.parsed_source_cache.as_analyzer();
|
2022-07-01 11:50:16 -04:00
|
|
|
|
2023-02-09 22:00:23 -05:00
|
|
|
let mut graph = ModuleGraph::default();
|
2023-02-22 14:15:25 -05:00
|
|
|
build_graph_with_npm_resolution(
|
|
|
|
&mut graph,
|
2023-04-11 18:10:51 -04:00
|
|
|
&self.resolver,
|
2023-02-22 14:15:25 -05:00
|
|
|
&self.npm_resolver,
|
|
|
|
roots,
|
|
|
|
loader,
|
|
|
|
deno_graph::BuildOptions {
|
|
|
|
is_dynamic: false,
|
|
|
|
imports: maybe_imports,
|
|
|
|
resolver: Some(graph_resolver),
|
|
|
|
npm_resolver: Some(graph_npm_resolver),
|
|
|
|
module_analyzer: Some(&*analyzer),
|
|
|
|
reporter: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
if graph.has_node_specifier
|
2023-01-24 09:05:54 -05:00
|
|
|
&& self.options.type_check_mode() != TypeCheckMode::None
|
|
|
|
{
|
|
|
|
self
|
|
|
|
.npm_resolver
|
|
|
|
.inject_synthetic_types_node_package()
|
|
|
|
.await?;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(graph)
|
2022-07-01 11:50:16 -04:00
|
|
|
}
|
2023-02-09 22:00:23 -05:00
|
|
|
|
|
|
|
pub fn graph(&self) -> Arc<ModuleGraph> {
|
2023-02-24 14:42:45 -05:00
|
|
|
self.graph_container.graph()
|
2023-02-15 13:44:52 -05:00
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:28:10 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2022-06-08 06:07:25 -04:00
|
|
|
struct FileWatcherReporter {
|
|
|
|
sender: tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>,
|
|
|
|
file_paths: Arc<Mutex<Vec<PathBuf>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_graph::source::Reporter for FileWatcherReporter {
|
|
|
|
fn on_load(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
modules_done: usize,
|
|
|
|
modules_total: usize,
|
|
|
|
) {
|
|
|
|
let mut file_paths = self.file_paths.lock();
|
|
|
|
if specifier.scheme() == "file" {
|
|
|
|
file_paths.push(specifier.to_file_path().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
if modules_done == modules_total {
|
|
|
|
self.sender.send(file_paths.drain(..).collect()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|