1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

perf(cli): conditionally load typescript declaration files (#19392)

Closes #18583
This commit is contained in:
David Sherret 2023-06-06 17:07:46 -04:00 committed by GitHub
parent 455b0eb8bb
commit 2aba4365ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 156 additions and 67 deletions

View file

@ -8,6 +8,7 @@ use crate::args::Lockfile;
use crate::args::PackageJsonDepsProvider;
use crate::args::StorageKeyResolver;
use crate::args::TsConfigType;
use crate::args::TypeCheckMode;
use crate::cache::Caches;
use crate::cache::DenoDir;
use crate::cache::DenoDirProvider;
@ -47,6 +48,7 @@ use crate::worker::HasNodeSpecifierChecker;
use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
use deno_graph::GraphKind;
use deno_runtime::deno_fs;
use deno_runtime::deno_node::analyze::NodeCodeTranslator;
use deno_runtime::deno_node::NodeResolver;
@ -537,7 +539,19 @@ impl CliFactory {
}
pub fn graph_container(&self) -> &Arc<ModuleGraphContainer> {
self.services.graph_container.get_or_init(Default::default)
self.services.graph_container.get_or_init(|| {
let graph_kind = match self.options.sub_command() {
DenoSubcommand::Cache(_) => GraphKind::All,
_ => {
if self.options.type_check_mode() == TypeCheckMode::None {
GraphKind::CodeOnly
} else {
GraphKind::All
}
}
};
Arc::new(ModuleGraphContainer::new(graph_kind))
})
}
pub fn maybe_inspector_server(&self) -> &Option<Arc<InspectorServer>> {

View file

@ -23,6 +23,7 @@ use deno_core::ModuleSpecifier;
use deno_core::TaskQueue;
use deno_core::TaskQueuePermit;
use deno_graph::source::Loader;
use deno_graph::GraphKind;
use deno_graph::Module;
use deno_graph::ModuleError;
use deno_graph::ModuleGraph;
@ -200,6 +201,7 @@ impl ModuleGraphBuilder {
pub async fn create_graph_with_loader(
&self,
graph_kind: GraphKind,
roots: Vec<ModuleSpecifier>,
loader: &mut dyn Loader,
) -> Result<deno_graph::ModuleGraph, AnyError> {
@ -210,7 +212,7 @@ impl ModuleGraphBuilder {
let graph_npm_resolver = cli_resolver.as_graph_npm_resolver();
let analyzer = self.parsed_source_cache.as_analyzer();
let mut graph = ModuleGraph::default();
let mut graph = ModuleGraph::new(graph_kind);
self
.build_graph_with_npm_resolution(
&mut graph,
@ -249,7 +251,13 @@ impl ModuleGraphBuilder {
let graph_resolver = cli_resolver.as_graph_resolver();
let graph_npm_resolver = cli_resolver.as_graph_npm_resolver();
let analyzer = self.parsed_source_cache.as_analyzer();
let mut graph = ModuleGraph::default();
let should_type_check =
self.options.type_check_mode() != TypeCheckMode::None;
let graph_kind = match should_type_check {
true => GraphKind::All,
false => GraphKind::CodeOnly,
};
let mut graph = ModuleGraph::new(graph_kind);
self
.build_graph_with_npm_resolution(
&mut graph,
@ -272,7 +280,7 @@ impl ModuleGraphBuilder {
graph_lock_or_exit(&graph, &mut lockfile.lock());
}
if self.options.type_check_mode() != TypeCheckMode::None {
if should_type_check {
self
.type_checker
.check(
@ -338,10 +346,13 @@ impl ModuleGraphBuilder {
pub async fn create_graph(
&self,
graph_kind: GraphKind,
roots: Vec<ModuleSpecifier>,
) -> Result<deno_graph::ModuleGraph, AnyError> {
let mut cache = self.create_graph_loader();
self.create_graph_with_loader(roots, &mut cache).await
self
.create_graph_with_loader(graph_kind, roots, &mut cache)
.await
}
}
@ -404,15 +415,15 @@ fn get_resolution_error_bare_specifier(
}
}
#[derive(Default, Debug)]
#[derive(Debug)]
struct GraphData {
graph: Arc<ModuleGraph>,
checked_libs: HashMap<TsTypeLib, HashSet<ModuleSpecifier>>,
}
/// Holds the `ModuleGraph` and what parts of it are type checked.
#[derive(Default)]
pub struct ModuleGraphContainer {
graph_kind: GraphKind,
// Allow only one request to update the graph data at a time,
// but allow other requests to read from it at any time even
// while another request is updating the data.
@ -421,8 +432,19 @@ pub struct ModuleGraphContainer {
}
impl ModuleGraphContainer {
pub fn new(graph_kind: GraphKind) -> Self {
Self {
graph_kind,
update_queue: Default::default(),
graph_data: Arc::new(RwLock::new(GraphData {
graph: Arc::new(ModuleGraph::new(graph_kind)),
checked_libs: Default::default(),
})),
}
}
pub fn clear(&self) {
self.graph_data.write().graph = Default::default();
self.graph_data.write().graph = Arc::new(ModuleGraph::new(self.graph_kind));
}
/// Acquires a permit to modify the module graph without other code

View file

@ -11,6 +11,7 @@ use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::task::spawn;
use deno_core::ModuleSpecifier;
use deno_graph::GraphKind;
use deno_lockfile::Lockfile;
use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot;
use deno_npm::NpmSystemInfo;
@ -273,7 +274,7 @@ impl LanguageServer {
open_docs: &open_docs,
};
let graph = module_graph_builder
.create_graph_with_loader(roots.clone(), &mut loader)
.create_graph_with_loader(GraphKind::All, roots.clone(), &mut loader)
.await?;
graph_util::graph_valid(
&graph,

View file

@ -3,6 +3,7 @@
use deno_core::url::Url;
use test_util as util;
use util::assert_contains;
use util::assert_not_contains;
use util::env_vars_for_npm_tests;
use util::TestContext;
@ -250,3 +251,18 @@ itest!(bench_no_lock {
cwd: Some("lockfile/basic"),
output: "lockfile/basic/bench.nolock.out",
});
#[test]
fn conditionally_loads_type_graph() {
let context = TestContext::default();
let output = context
.new_command()
.args("bench --reload -L debug run/type_directives_js_main.js")
.run();
output.assert_matches_text("[WILDCARD] - FileFetcher::fetch() - specifier: file:///[WILDCARD]/subdir/type_reference.d.ts[WILDCARD]");
let output = context
.new_command()
.args("bench --reload -L debug --no-check run/type_directives_js_main.js")
.run();
assert_not_contains!(output.combined_output(), "type_reference.d.ts");
}

View file

@ -1,6 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use test_util::env_vars_for_npm_tests;
use test_util::TestContext;
use test_util::TestContextBuilder;
itest!(_036_import_map_fetch {
@ -181,3 +182,12 @@ fn cache_put_overwrite() {
output.assert_matches_text("res1\n");
output.assert_exit_code(0);
}
#[test]
fn loads_type_graph() {
let output = TestContext::default()
.new_command()
.args("cache --reload -L debug run/type_directives_js_main.js")
.run();
output.assert_matches_text("[WILDCARD] - FileFetcher::fetch() - specifier: file:///[WILDCARD]/subdir/type_reference.d.ts[WILDCARD]");
}

View file

@ -12,6 +12,7 @@ use test_util::TempDir;
use trust_dns_client::serialize::txt::Lexer;
use trust_dns_client::serialize::txt::Parser;
use util::assert_contains;
use util::assert_not_contains;
use util::env_vars_for_npm_tests_no_sync_download;
use util::TestContext;
use util::TestContextBuilder;
@ -1277,11 +1278,20 @@ itest!(type_directives_02 {
output: "run/type_directives_02.ts.out",
});
itest!(type_directives_js_main {
args: "run --reload -L debug run/type_directives_js_main.js",
output: "run/type_directives_js_main.js.out",
exit_code: 0,
});
#[test]
fn type_directives_js_main() {
let context = TestContext::default();
let output = context
.new_command()
.args("run --reload -L debug --check run/type_directives_js_main.js")
.run();
output.assert_matches_text("[WILDCARD] - FileFetcher::fetch() - specifier: file:///[WILDCARD]/subdir/type_reference.d.ts[WILDCARD]");
let output = context
.new_command()
.args("run --reload -L debug run/type_directives_js_main.js")
.run();
assert_not_contains!(output.combined_output(), "type_reference.d.ts");
}
itest!(type_directives_redirect {
args: "run --reload --check run/type_directives_redirect.ts",

View file

@ -3,6 +3,7 @@
use deno_core::url::Url;
use test_util as util;
use util::assert_contains;
use util::assert_not_contains;
use util::env_vars_for_npm_tests;
use util::wildcard_match;
use util::TestContext;
@ -566,3 +567,18 @@ fn test_with_glob_config_and_flags() {
assert_contains!(output, "glob/data/test1.js");
assert_contains!(output, "glob/data/test1.ts");
}
#[test]
fn conditionally_loads_type_graph() {
let context = TestContext::default();
let output = context
.new_command()
.args("test --reload -L debug run/type_directives_js_main.js")
.run();
output.assert_matches_text("[WILDCARD] - FileFetcher::fetch() - specifier: file:///[WILDCARD]/subdir/type_reference.d.ts[WILDCARD]");
let output = context
.new_command()
.args("test --reload -L debug --no-check run/type_directives_js_main.js")
.run();
assert_not_contains!(output.combined_output(), "type_reference.d.ts");
}

View file

@ -1,3 +0,0 @@
[WILDCARD]
DEBUG RS - [WILDCARD] - FileFetcher::fetch() - specifier: file:///[WILDCARD]/subdir/type_reference.d.ts
[WILDCARD]

View file

@ -31,6 +31,7 @@ use deno_core::task::spawn;
use deno_core::task::spawn_blocking;
use deno_core::v8;
use deno_core::ModuleSpecifier;
use deno_graph::GraphKind;
use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::tokio_util::create_and_run_current_thread;
@ -693,7 +694,11 @@ pub async fn run_benchmarks_with_watch(
// file would have impact on other files, which is undesirable.
let permissions =
Permissions::from_options(&cli_options.permissions_options())?;
let no_check = cli_options.type_check_mode() == TypeCheckMode::None;
let type_check = cli_options.type_check_mode() != TypeCheckMode::None;
let graph_kind = match type_check {
true => GraphKind::All,
false => GraphKind::CodeOnly,
};
let resolver = |changed: Option<Vec<PathBuf>>| {
let paths_to_watch = bench_options.files.include.clone();
@ -714,7 +719,7 @@ pub async fn run_benchmarks_with_watch(
bench_modules.clone()
};
let graph = module_graph_builder
.create_graph(bench_modules.clone())
.create_graph(graph_kind, bench_modules.clone())
.await?;
graph_valid_with_cli_options(&graph, &bench_modules, &cli_options)?;
@ -726,32 +731,19 @@ pub async fn run_benchmarks_with_watch(
// This needs to be accessible to skip getting dependencies if they're already there,
// otherwise this will cause a stack overflow with circular dependencies
output: &mut HashSet<&'a ModuleSpecifier>,
no_check: bool,
) {
if let Some(module) = maybe_module.and_then(|m| m.esm()) {
for dep in module.dependencies.values() {
if let Some(specifier) = &dep.get_code() {
if !output.contains(specifier) {
output.insert(specifier);
get_dependencies(
graph,
graph.get(specifier),
output,
no_check,
);
get_dependencies(graph, graph.get(specifier), output);
}
}
if !no_check {
if let Some(specifier) = &dep.get_type() {
if !output.contains(specifier) {
output.insert(specifier);
get_dependencies(
graph,
graph.get(specifier),
output,
no_check,
);
}
if let Some(specifier) = &dep.get_type() {
if !output.contains(specifier) {
output.insert(specifier);
get_dependencies(graph, graph.get(specifier), output);
}
}
}
@ -761,7 +753,7 @@ pub async fn run_benchmarks_with_watch(
// This bench module and all it's dependencies
let mut modules = HashSet::new();
modules.insert(&specifier);
get_dependencies(&graph, graph.get(&specifier), &mut modules, no_check);
get_dependencies(&graph, graph.get(&specifier), &mut modules);
paths_to_watch.extend(
modules

View file

@ -2,6 +2,7 @@
use crate::args::CompileFlags;
use crate::args::Flags;
use crate::args::TypeCheckMode;
use crate::factory::CliFactory;
use crate::standalone::is_standalone_binary;
use crate::util::path::path_has_trailing_slash;
@ -10,6 +11,7 @@ use deno_core::anyhow::Context;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::resolve_url_or_path;
use deno_graph::GraphKind;
use deno_runtime::colors;
use std::path::Path;
use std::path::PathBuf;
@ -44,10 +46,20 @@ pub async fn compile(
let graph = Arc::try_unwrap(
module_graph_builder
.create_graph_and_maybe_check(module_roots)
.create_graph_and_maybe_check(module_roots.clone())
.await?,
)
.unwrap();
let graph = if cli_options.type_check_mode() == TypeCheckMode::None {
graph
} else {
// In this case, the previous graph creation did type checking, which will
// create a module graph with types information in it. We don't want to
// store that in the eszip so create a code only module graph from scratch.
module_graph_builder
.create_graph(GraphKind::CodeOnly, module_roots)
.await?
};
let parser = parsed_source_cache.as_capturing_parser();
let eszip = eszip::EszipV2::from_graph(graph, &parser, Default::default())?;

View file

@ -16,6 +16,7 @@ use deno_core::error::AnyError;
use deno_core::resolve_path;
use deno_core::resolve_url_or_path;
use deno_doc as doc;
use deno_graph::GraphKind;
use deno_graph::ModuleSpecifier;
use std::path::PathBuf;
@ -43,7 +44,7 @@ pub async fn print_docs(
Vec::new(),
);
let analyzer = deno_graph::CapturingModuleAnalyzer::default();
let mut graph = deno_graph::ModuleGraph::default();
let mut graph = deno_graph::ModuleGraph::new(GraphKind::TypesOnly);
graph
.build(
vec![source_file_specifier.clone()],
@ -87,7 +88,7 @@ pub async fn print_docs(
file_fetcher.insert_cached(root);
let graph = module_graph_builder
.create_graph(vec![root_specifier.clone()])
.create_graph(GraphKind::TypesOnly, vec![root_specifier.clone()])
.await?;
if let Some(lockfile) = maybe_lockfile {

View file

@ -11,6 +11,7 @@ use deno_core::resolve_url_or_path;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_graph::Dependency;
use deno_graph::GraphKind;
use deno_graph::Module;
use deno_graph::ModuleError;
use deno_graph::ModuleGraph;
@ -43,7 +44,7 @@ pub async fn info(flags: Flags, info_flags: InfoFlags) -> Result<(), AnyError> {
let mut loader = module_graph_builder.create_graph_loader();
loader.enable_loading_cache_info(); // for displaying the cache information
let graph = module_graph_builder
.create_graph_with_loader(vec![specifier], &mut loader)
.create_graph_with_loader(GraphKind::All, vec![specifier], &mut loader)
.await?;
if let Some(lockfile) = maybe_lockfile {

View file

@ -39,6 +39,7 @@ use deno_core::task::spawn_blocking;
use deno_core::url::Url;
use deno_core::v8;
use deno_core::ModuleSpecifier;
use deno_graph::GraphKind;
use deno_runtime::deno_io::Stdio;
use deno_runtime::deno_io::StdioPipe;
use deno_runtime::fmt_errors::format_js_error;
@ -1706,7 +1707,11 @@ pub async fn run_tests_with_watch(
// file would have impact on other files, which is undesirable.
let permissions =
Permissions::from_options(&cli_options.permissions_options())?;
let no_check = cli_options.type_check_mode() == TypeCheckMode::None;
let type_check = cli_options.type_check_mode() != TypeCheckMode::None;
let graph_kind = match type_check {
true => GraphKind::All,
false => GraphKind::CodeOnly,
};
let log_level = cli_options.log_level();
let resolver = |changed: Option<Vec<PathBuf>>| {
@ -1731,7 +1736,7 @@ pub async fn run_tests_with_watch(
test_modules.clone()
};
let graph = module_graph_builder
.create_graph(test_modules.clone())
.create_graph(graph_kind, test_modules.clone())
.await?;
graph_valid_with_cli_options(&graph, &test_modules, &cli_options)?;
@ -1743,32 +1748,19 @@ pub async fn run_tests_with_watch(
// This needs to be accessible to skip getting dependencies if they're already there,
// otherwise this will cause a stack overflow with circular dependencies
output: &mut HashSet<&'a ModuleSpecifier>,
no_check: bool,
) {
if let Some(module) = maybe_module.and_then(|m| m.esm()) {
for dep in module.dependencies.values() {
if let Some(specifier) = &dep.get_code() {
if !output.contains(specifier) {
output.insert(specifier);
get_dependencies(
graph,
graph.get(specifier),
output,
no_check,
);
get_dependencies(graph, graph.get(specifier), output);
}
}
if !no_check {
if let Some(specifier) = &dep.get_type() {
if !output.contains(specifier) {
output.insert(specifier);
get_dependencies(
graph,
graph.get(specifier),
output,
no_check,
);
}
if let Some(specifier) = &dep.get_type() {
if !output.contains(specifier) {
output.insert(specifier);
get_dependencies(graph, graph.get(specifier), output);
}
}
}
@ -1778,7 +1770,7 @@ pub async fn run_tests_with_watch(
// This test module and all it's dependencies
let mut modules = HashSet::new();
modules.insert(&specifier);
get_dependencies(&graph, graph.get(&specifier), &mut modules, no_check);
get_dependencies(&graph, graph.get(&specifier), &mut modules);
paths_to_watch.extend(
modules

View file

@ -10,6 +10,7 @@ use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::resolve_url_or_path;
use deno_graph::GraphKind;
use log::warn;
use crate::args::CliOptions;
@ -371,7 +372,9 @@ async fn create_graph(
.map(|p| resolve_url_or_path(p, initial_cwd))
.collect::<Result<Vec<_>, _>>()?;
module_graph_builder.create_graph(entry_points).await
module_graph_builder
.create_graph(GraphKind::All, entry_points)
.await
}
#[cfg(test)]

View file

@ -16,6 +16,7 @@ use deno_core::serde_json;
use deno_graph::source::LoadFuture;
use deno_graph::source::LoadResponse;
use deno_graph::source::Loader;
use deno_graph::GraphKind;
use deno_graph::ModuleGraph;
use import_map::ImportMap;
@ -279,7 +280,7 @@ async fn build_test_graph(
Default::default(),
)
});
let mut graph = ModuleGraph::default();
let mut graph = ModuleGraph::new(GraphKind::All);
graph
.build(
roots,

View file

@ -839,6 +839,7 @@ mod tests {
use crate::args::TsConfig;
use deno_core::futures::future;
use deno_core::OpState;
use deno_graph::GraphKind;
use deno_graph::ModuleGraph;
use std::fs;
@ -882,7 +883,7 @@ mod tests {
let hash_data = maybe_hash_data.unwrap_or(0);
let fixtures = test_util::testdata_path().join("tsc2");
let mut loader = MockLoader { fixtures };
let mut graph = ModuleGraph::default();
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
graph
.build(vec![specifier], &mut loader, Default::default())
.await;
@ -908,7 +909,7 @@ mod tests {
let hash_data = 123; // something random
let fixtures = test_util::testdata_path().join("tsc2");
let mut loader = MockLoader { fixtures };
let mut graph = ModuleGraph::default();
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
graph
.build(vec![specifier.clone()], &mut loader, Default::default())
.await;