1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

refactor: remove module graph setup from ModuleLoader (#22442)

`ModuleLoader` was doing too much duplicate work.
This commit is contained in:
David Sherret 2024-02-16 17:10:06 -05:00 committed by GitHub
parent 67a4231bb6
commit 828d9b8485
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 72 additions and 124 deletions

View file

@ -657,12 +657,8 @@ impl CliFactory {
self.fs().clone(), self.fs().clone(),
self.graph_container().clone(), self.graph_container().clone(),
self.maybe_lockfile().clone(), self.maybe_lockfile().clone(),
self.maybe_file_watcher_reporter().clone(),
self.module_graph_builder().await?.clone(), self.module_graph_builder().await?.clone(),
self.module_info_cache()?.clone(),
self.parsed_source_cache().clone(),
self.text_only_progress_bar().clone(), self.text_only_progress_bar().clone(),
self.resolver().await?.clone(),
self.type_checker().await?.clone(), self.type_checker().await?.clone(),
))) )))
}) })

View file

@ -206,6 +206,7 @@ pub fn graph_lock_or_exit(graph: &ModuleGraph, lockfile: &mut Lockfile) {
pub struct CreateGraphOptions<'a> { pub struct CreateGraphOptions<'a> {
pub graph_kind: GraphKind, pub graph_kind: GraphKind,
pub roots: Vec<ModuleSpecifier>, pub roots: Vec<ModuleSpecifier>,
pub is_dynamic: bool,
/// Whether to do fast check on workspace members. This is mostly only /// Whether to do fast check on workspace members. This is mostly only
/// useful when publishing. /// useful when publishing.
pub workspace_fast_check: bool, pub workspace_fast_check: bool,
@ -279,6 +280,7 @@ impl ModuleGraphBuilder {
) -> Result<deno_graph::ModuleGraph, AnyError> { ) -> Result<deno_graph::ModuleGraph, AnyError> {
self self
.create_graph_with_options(CreateGraphOptions { .create_graph_with_options(CreateGraphOptions {
is_dynamic: false,
graph_kind, graph_kind,
roots, roots,
loader: Some(loader), loader: Some(loader),
@ -291,54 +293,10 @@ impl ModuleGraphBuilder {
&self, &self,
options: CreateGraphOptions<'_>, options: CreateGraphOptions<'_>,
) -> Result<deno_graph::ModuleGraph, AnyError> { ) -> Result<deno_graph::ModuleGraph, AnyError> {
enum MutLoaderRef<'a> {
Borrowed(&'a mut dyn Loader),
Owned(cache::FetchCacher),
}
impl<'a> MutLoaderRef<'a> {
pub fn as_mut_loader(&mut self) -> &mut dyn Loader {
match self {
Self::Borrowed(loader) => *loader,
Self::Owned(loader) => loader,
}
}
}
let parser = self.parsed_source_cache.as_capturing_parser();
let analyzer = self.module_info_cache.as_module_analyzer(&parser);
let maybe_imports = self.options.to_maybe_imports()?;
let cli_resolver = self.resolver.clone();
let graph_resolver = cli_resolver.as_graph_resolver();
let graph_npm_resolver = cli_resolver.as_graph_npm_resolver();
let maybe_file_watcher_reporter = self
.maybe_file_watcher_reporter
.as_ref()
.map(|r| r.as_reporter());
let mut loader = match options.loader {
Some(loader) => MutLoaderRef::Borrowed(loader),
None => MutLoaderRef::Owned(self.create_graph_loader()),
};
let mut graph = ModuleGraph::new(options.graph_kind); let mut graph = ModuleGraph::new(options.graph_kind);
self self
.build_graph_with_npm_resolution( .build_graph_with_npm_resolution(&mut graph, options)
&mut graph,
options.roots,
loader.as_mut_loader(),
deno_graph::BuildOptions {
is_dynamic: false,
imports: maybe_imports,
resolver: Some(graph_resolver),
file_system: Some(&DenoGraphFsAdapter(self.fs.as_ref())),
npm_resolver: Some(graph_npm_resolver),
module_analyzer: Some(&analyzer),
module_parser: Some(&parser),
reporter: maybe_file_watcher_reporter,
workspace_fast_check: options.workspace_fast_check,
workspace_members: self.get_deno_graph_workspace_members()?,
},
)
.await?; .await?;
if let Some(npm_resolver) = self.npm_resolver.as_managed() { if let Some(npm_resolver) = self.npm_resolver.as_managed() {
@ -354,38 +312,16 @@ impl ModuleGraphBuilder {
&self, &self,
roots: Vec<ModuleSpecifier>, roots: Vec<ModuleSpecifier>,
) -> Result<Arc<deno_graph::ModuleGraph>, AnyError> { ) -> Result<Arc<deno_graph::ModuleGraph>, AnyError> {
let mut cache = self.create_graph_loader();
let maybe_imports = self.options.to_maybe_imports()?;
let cli_resolver = self.resolver.clone();
let graph_resolver = cli_resolver.as_graph_resolver();
let graph_npm_resolver = cli_resolver.as_graph_npm_resolver();
let parser = self.parsed_source_cache.as_capturing_parser();
let analyzer = self.module_info_cache.as_module_analyzer(&parser);
let graph_kind = self.options.type_check_mode().as_graph_kind(); let graph_kind = self.options.type_check_mode().as_graph_kind();
let mut graph = ModuleGraph::new(graph_kind);
let maybe_file_watcher_reporter = self
.maybe_file_watcher_reporter
.as_ref()
.map(|r| r.as_reporter());
self let graph = self
.build_graph_with_npm_resolution( .create_graph_with_options(CreateGraphOptions {
&mut graph, is_dynamic: false,
graph_kind,
roots, roots,
&mut cache, loader: None,
deno_graph::BuildOptions { workspace_fast_check: false,
is_dynamic: false, })
imports: maybe_imports,
file_system: Some(&DenoGraphFsAdapter(self.fs.as_ref())),
resolver: Some(graph_resolver),
npm_resolver: Some(graph_npm_resolver),
module_analyzer: Some(&analyzer),
module_parser: Some(&parser),
reporter: maybe_file_watcher_reporter,
workspace_fast_check: false,
workspace_members: self.get_deno_graph_workspace_members()?,
},
)
.await?; .await?;
let graph = Arc::new(graph); let graph = Arc::new(graph);
@ -441,6 +377,60 @@ impl ModuleGraphBuilder {
} }
pub async fn build_graph_with_npm_resolution<'a>( pub async fn build_graph_with_npm_resolution<'a>(
&self,
graph: &mut ModuleGraph,
options: CreateGraphOptions<'a>,
) -> Result<(), AnyError> {
enum MutLoaderRef<'a> {
Borrowed(&'a mut dyn Loader),
Owned(cache::FetchCacher),
}
impl<'a> MutLoaderRef<'a> {
pub fn as_mut_loader(&mut self) -> &mut dyn Loader {
match self {
Self::Borrowed(loader) => *loader,
Self::Owned(loader) => loader,
}
}
}
let maybe_imports = self.options.to_maybe_imports()?;
let parser = self.parsed_source_cache.as_capturing_parser();
let analyzer = self.module_info_cache.as_module_analyzer(&parser);
let mut loader = match options.loader {
Some(loader) => MutLoaderRef::Borrowed(loader),
None => MutLoaderRef::Owned(self.create_graph_loader()),
};
let cli_resolver = self.resolver.clone();
let graph_resolver = cli_resolver.as_graph_resolver();
let graph_npm_resolver = cli_resolver.as_graph_npm_resolver();
let maybe_file_watcher_reporter = self
.maybe_file_watcher_reporter
.as_ref()
.map(|r| r.as_reporter());
self
.build_graph_with_npm_resolution_and_build_options(
graph,
options.roots,
loader.as_mut_loader(),
deno_graph::BuildOptions {
is_dynamic: options.is_dynamic,
imports: maybe_imports,
resolver: Some(graph_resolver),
file_system: Some(&DenoGraphFsAdapter(self.fs.as_ref())),
npm_resolver: Some(graph_npm_resolver),
module_analyzer: Some(&analyzer),
module_parser: Some(&parser),
reporter: maybe_file_watcher_reporter,
workspace_fast_check: options.workspace_fast_check,
workspace_members: self.get_deno_graph_workspace_members()?,
},
)
.await
}
async fn build_graph_with_npm_resolution_and_build_options<'a>(
&self, &self,
graph: &mut ModuleGraph, graph: &mut ModuleGraph,
roots: Vec<ModuleSpecifier>, roots: Vec<ModuleSpecifier>,
@ -841,7 +831,7 @@ impl deno_graph::source::Reporter for FileWatcherReporter {
} }
} }
pub fn workspace_config_to_workspace_members( fn workspace_config_to_workspace_members(
workspace_config: &deno_config::WorkspaceConfig, workspace_config: &deno_config::WorkspaceConfig,
) -> Result<Vec<deno_graph::WorkspaceMember>, AnyError> { ) -> Result<Vec<deno_graph::WorkspaceMember>, AnyError> {
workspace_config workspace_config

View file

@ -3,14 +3,11 @@
use crate::args::CliOptions; use crate::args::CliOptions;
use crate::args::DenoSubcommand; use crate::args::DenoSubcommand;
use crate::args::TsTypeLib; use crate::args::TsTypeLib;
use crate::cache::ModuleInfoCache;
use crate::cache::ParsedSourceCache; use crate::cache::ParsedSourceCache;
use crate::emit::Emitter; use crate::emit::Emitter;
use crate::graph_util::graph_lock_or_exit; use crate::graph_util::graph_lock_or_exit;
use crate::graph_util::graph_valid_with_cli_options; use crate::graph_util::graph_valid_with_cli_options;
use crate::graph_util::workspace_config_to_workspace_members; use crate::graph_util::CreateGraphOptions;
use crate::graph_util::DenoGraphFsAdapter;
use crate::graph_util::FileWatcherReporter;
use crate::graph_util::ModuleGraphBuilder; use crate::graph_util::ModuleGraphBuilder;
use crate::graph_util::ModuleGraphContainer; use crate::graph_util::ModuleGraphContainer;
use crate::node; use crate::node;
@ -68,12 +65,8 @@ pub struct ModuleLoadPreparer {
fs: Arc<dyn deno_fs::FileSystem>, fs: Arc<dyn deno_fs::FileSystem>,
graph_container: Arc<ModuleGraphContainer>, graph_container: Arc<ModuleGraphContainer>,
lockfile: Option<Arc<Mutex<Lockfile>>>, lockfile: Option<Arc<Mutex<Lockfile>>>,
maybe_file_watcher_reporter: Option<FileWatcherReporter>,
module_graph_builder: Arc<ModuleGraphBuilder>, module_graph_builder: Arc<ModuleGraphBuilder>,
module_info_cache: Arc<ModuleInfoCache>,
parsed_source_cache: Arc<ParsedSourceCache>,
progress_bar: ProgressBar, progress_bar: ProgressBar,
resolver: Arc<CliGraphResolver>,
type_checker: Arc<TypeChecker>, type_checker: Arc<TypeChecker>,
} }
@ -84,12 +77,8 @@ impl ModuleLoadPreparer {
fs: Arc<dyn deno_fs::FileSystem>, fs: Arc<dyn deno_fs::FileSystem>,
graph_container: Arc<ModuleGraphContainer>, graph_container: Arc<ModuleGraphContainer>,
lockfile: Option<Arc<Mutex<Lockfile>>>, lockfile: Option<Arc<Mutex<Lockfile>>>,
maybe_file_watcher_reporter: Option<FileWatcherReporter>,
module_graph_builder: Arc<ModuleGraphBuilder>, module_graph_builder: Arc<ModuleGraphBuilder>,
module_info_cache: Arc<ModuleInfoCache>,
parsed_source_cache: Arc<ParsedSourceCache>,
progress_bar: ProgressBar, progress_bar: ProgressBar,
resolver: Arc<CliGraphResolver>,
type_checker: Arc<TypeChecker>, type_checker: Arc<TypeChecker>,
) -> Self { ) -> Self {
Self { Self {
@ -97,12 +86,8 @@ impl ModuleLoadPreparer {
fs, fs,
graph_container, graph_container,
lockfile, lockfile,
maybe_file_watcher_reporter,
module_graph_builder, module_graph_builder,
module_info_cache,
parsed_source_cache,
progress_bar, progress_bar,
resolver,
type_checker, type_checker,
} }
} }
@ -123,23 +108,6 @@ impl ModuleLoadPreparer {
let _pb_clear_guard = self.progress_bar.clear_guard(); let _pb_clear_guard = self.progress_bar.clear_guard();
let mut cache = self.module_graph_builder.create_fetch_cacher(permissions); let mut cache = self.module_graph_builder.create_fetch_cacher(permissions);
let maybe_imports = self.options.to_maybe_imports()?;
let maybe_workspace_config = self.options.maybe_workspace_config();
let workspace_members = if let Some(wc) = maybe_workspace_config {
workspace_config_to_workspace_members(wc)?
} else {
vec![]
};
let graph_resolver = self.resolver.as_graph_resolver();
let graph_npm_resolver = self.resolver.as_graph_npm_resolver();
let maybe_file_watcher_reporter = self
.maybe_file_watcher_reporter
.as_ref()
.map(|r| r.as_reporter());
let parser = self.parsed_source_cache.as_capturing_parser();
let analyzer = self.module_info_cache.as_module_analyzer(&parser);
log::debug!("Creating module graph."); log::debug!("Creating module graph.");
let mut graph_update_permit = let mut graph_update_permit =
self.graph_container.acquire_update_permit().await; self.graph_container.acquire_update_permit().await;
@ -154,19 +122,12 @@ impl ModuleLoadPreparer {
.module_graph_builder .module_graph_builder
.build_graph_with_npm_resolution( .build_graph_with_npm_resolution(
graph, graph,
roots.clone(), CreateGraphOptions {
&mut cache,
deno_graph::BuildOptions {
is_dynamic, is_dynamic,
imports: maybe_imports, graph_kind: graph.graph_kind(),
file_system: Some(&DenoGraphFsAdapter(self.fs.as_ref())), roots: roots.clone(),
resolver: Some(graph_resolver), loader: Some(&mut cache),
npm_resolver: Some(graph_npm_resolver),
module_parser: Some(&parser),
module_analyzer: Some(&analyzer),
reporter: maybe_file_watcher_reporter,
workspace_fast_check: false, workspace_fast_check: false,
workspace_members,
}, },
) )
.await?; .await?;

View file

@ -768,6 +768,7 @@ async fn build_and_check_graph_for_publish(
let graph = Arc::new( let graph = Arc::new(
module_graph_builder module_graph_builder
.create_graph_with_options(crate::graph_util::CreateGraphOptions { .create_graph_with_options(crate::graph_util::CreateGraphOptions {
is_dynamic: false,
// All because we're going to use this same graph to determine the publish order later // All because we're going to use this same graph to determine the publish order later
graph_kind: deno_graph::GraphKind::All, graph_kind: deno_graph::GraphKind::All,
roots: packages roots: packages