1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

perf(lsp): cleanup document dependencies (#23426)

This commit is contained in:
Nayeem Rahman 2024-04-19 01:51:16 +01:00 committed by GitHub
parent 90688edcc2
commit 8e77f091ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 122 additions and 115 deletions

24
Cargo.lock generated
View file

@ -1166,9 +1166,9 @@ dependencies = [
[[package]]
name = "deno_ast"
version = "0.37.0"
version = "0.37.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67f9b18b22c707b59cf1ee9cb6f0afbfcf0d03a59b9244ddadacf6a4dc7cfeba"
checksum = "b2be70f80fee64edfbbc4a61c50f18b4a0fbff0b2657d26f6f0443e1db279af9"
dependencies = [
"anyhow",
"base64",
@ -1381,9 +1381,9 @@ dependencies = [
[[package]]
name = "deno_doc"
version = "0.124.0"
version = "0.125.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e1215167b2d2a7595cf32737ff9c7802d7faf58dfbe8c9e5d8f07d4c3af4987"
checksum = "43a44dfda00cc831fdc693d295cb90cb30b34bc28894d1b4ab023614ab5e0a3b"
dependencies = [
"ammonia",
"anyhow",
@ -1405,9 +1405,9 @@ dependencies = [
[[package]]
name = "deno_emit"
version = "0.39.1"
version = "0.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8928b70466b99a4fa6ad1e2334f5268eb319d1ddc7607b1ccbd6260cc5b45e6d"
checksum = "03ae8a1338bd3860f68fbfd99bbbc8a9c4ebe78e4b298f62980d2fc3547f04bb"
dependencies = [
"anyhow",
"base64",
@ -1477,9 +1477,9 @@ dependencies = [
[[package]]
name = "deno_graph"
version = "0.72.0"
version = "0.73.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "508791f6140a8b5b1ac40bac1842db04281b61db4b64c5fd5bf9f1f7259f328a"
checksum = "046123e4f8de81c7db363d0869d783a5dcd6ceb007b07c6ba4fdf3cc0a82314e"
dependencies = [
"anyhow",
"async-trait",
@ -1621,9 +1621,9 @@ dependencies = [
[[package]]
name = "deno_media_type"
version = "0.1.3"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edf9879493856d1622be70f396b0b0d3e519538dd6501b7c609ecbaa7e2194d2"
checksum = "a8978229b82552bf8457a0125aa20863f023619cfc21ebb007b1e571d68fd85b"
dependencies = [
"data-url",
"serde",
@ -2530,9 +2530,9 @@ dependencies = [
[[package]]
name = "eszip"
version = "0.67.0"
version = "0.68.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d43c01fc33df17895d3313cf01bf88dde2522ab19da95cddb881be1cb2642ac"
checksum = "96de7a1c16d3d63e7c84e14da935cebb4b7f451d4508b3ff2ae0e1c313059cd1"
dependencies = [
"anyhow",
"base64",

View file

@ -43,7 +43,7 @@ license = "MIT"
repository = "https://github.com/denoland/deno"
[workspace.dependencies]
deno_ast = { version = "=0.37.0", features = ["transpiling"] }
deno_ast = { version = "=0.37.1", features = ["transpiling"] }
deno_core = { version = "0.275.0" }
deno_bench_util = { version = "0.141.0", path = "./bench_util" }

View file

@ -66,9 +66,9 @@ deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "proposa
deno_cache_dir = { workspace = true }
deno_config = "=0.15.0"
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_doc = { version = "=0.124.0", features = ["html"] }
deno_emit = "=0.39.1"
deno_graph = { version = "=0.72.0", features = ["tokio_executor"] }
deno_doc = { version = "=0.125.0", features = ["html"] }
deno_emit = "=0.40.0"
deno_graph = { version = "=0.73.1", features = ["tokio_executor"] }
deno_lint = { version = "=0.58.3", features = ["docs"] }
deno_lockfile.workspace = true
deno_npm = "=0.17.0"
@ -76,7 +76,7 @@ deno_runtime = { workspace = true, features = ["include_js_files_for_snapshottin
deno_semver = "=0.5.4"
deno_task_shell = "=0.16.0"
deno_terminal.workspace = true
eszip = "=0.67.0"
eszip = "=0.68.0"
napi_sym.workspace = true
async-trait.workspace = true

View file

@ -185,6 +185,7 @@ pub fn ts_config_to_transpile_and_emit_options(
jsx_fragment_factory: options.jsx_fragment_factory,
jsx_import_source: options.jsx_import_source,
precompile_jsx,
precompile_jsx_skip_elements: None,
transform_jsx,
var_decl_imports: false,
},

4
cli/cache/mod.rs vendored
View file

@ -192,7 +192,7 @@ impl Loader for FetchCacher {
}
fn load(
&mut self,
&self,
specifier: &ModuleSpecifier,
options: deno_graph::source::LoadOptions,
) -> LoadFuture {
@ -278,7 +278,7 @@ impl Loader for FetchCacher {
}
fn cache_module_info(
&mut self,
&self,
specifier: &ModuleSpecifier,
source: &Arc<[u8]>,
module_info: &deno_graph::ModuleInfo,

View file

@ -453,6 +453,7 @@ impl ModuleGraphBuilder {
deno_graph::BuildOptions {
is_dynamic: options.is_dynamic,
jsr_url_provider: &CliJsrUrlProvider,
passthrough_jsr_specifiers: false,
executor: Default::default(),
imports: maybe_imports,
resolver: Some(graph_resolver),

View file

@ -211,42 +211,19 @@ impl AssetOrDocument {
}
}
#[derive(Debug, Default)]
struct DocumentDependencies {
deps: IndexMap<String, deno_graph::Dependency>,
maybe_types_dependency: Option<deno_graph::TypesDependency>,
}
impl DocumentDependencies {
pub fn from_maybe_module(maybe_module: &Option<ModuleResult>) -> Self {
if let Some(Ok(module)) = &maybe_module {
Self::from_module(module)
} else {
Self::default()
}
}
pub fn from_module(module: &deno_graph::JsModule) -> Self {
Self {
deps: module.dependencies.clone(),
maybe_types_dependency: module.maybe_types_dependency.clone(),
}
}
}
type ModuleResult = Result<deno_graph::JsModule, deno_graph::ModuleGraphError>;
type ParsedSourceResult = Result<ParsedSource, deno_ast::ParseDiagnostic>;
#[derive(Debug)]
pub struct Document {
/// Contains the last-known-good set of dependencies from parsing the module.
dependencies: Arc<DocumentDependencies>,
dependencies: Arc<IndexMap<String, deno_graph::Dependency>>,
maybe_types_dependency: Option<Arc<deno_graph::TypesDependency>>,
fs_version: String,
line_index: Arc<LineIndex>,
maybe_headers: Option<HashMap<String, String>>,
maybe_language_id: Option<LanguageId>,
maybe_lsp_version: Option<i32>,
maybe_module: Option<ModuleResult>,
// this is a lazily constructed value based on the state of the document,
// so having a mutex to hold it is ok
maybe_navigation_tree: Mutex<Option<Arc<tsc::NavigationTree>>>,
@ -283,17 +260,23 @@ impl Document {
resolver,
npm_resolver,
);
let dependencies =
Arc::new(DocumentDependencies::from_maybe_module(&maybe_module));
let maybe_module = maybe_module.and_then(Result::ok);
let dependencies = maybe_module
.as_ref()
.map(|m| Arc::new(m.dependencies.clone()))
.unwrap_or_default();
let maybe_types_dependency = maybe_module
.as_ref()
.and_then(|m| Some(Arc::new(m.maybe_types_dependency.clone()?)));
let line_index = Arc::new(LineIndex::new(text_info.text_str()));
Arc::new(Document {
dependencies,
maybe_types_dependency,
fs_version,
line_index,
maybe_headers,
maybe_language_id: None,
maybe_lsp_version: None,
maybe_module,
maybe_navigation_tree: Mutex::new(None),
maybe_parsed_source: maybe_parsed_source
.filter(|_| specifier.scheme() == "file"),
@ -309,39 +292,59 @@ impl Document {
maybe_node_resolver: Option<&CliNodeResolver>,
npm_resolver: &dyn deno_graph::source::NpmResolver,
) -> Option<Arc<Self>> {
let mut parsed_source_result = match &self.maybe_parsed_source {
Some(parsed_source_result) => parsed_source_result.clone(),
None => return None, // nothing to change
};
let media_type = resolve_media_type(
&self.specifier,
self.maybe_headers.as_ref(),
self.maybe_language_id,
maybe_node_resolver,
);
// reparse if the media type has changed
if let Ok(parsed_source) = &parsed_source_result {
if parsed_source.media_type() != media_type {
parsed_source_result =
parse_source(&self.specifier, self.text_info.clone(), media_type);
}
let dependencies;
let maybe_types_dependency;
let maybe_parsed_source;
if media_type != self.media_type {
let parsed_source_result =
parse_source(&self.specifier, self.text_info.clone(), media_type);
let maybe_module = analyze_module(
&self.specifier,
&parsed_source_result,
self.maybe_headers.as_ref(),
resolver,
npm_resolver,
)
.ok();
dependencies = maybe_module
.as_ref()
.map(|m| Arc::new(m.dependencies.clone()))
.unwrap_or_default();
maybe_types_dependency = maybe_module
.as_ref()
.and_then(|m| Some(Arc::new(m.maybe_types_dependency.clone()?)));
maybe_parsed_source = Some(parsed_source_result);
} else {
dependencies = Arc::new(
self
.dependencies
.iter()
.map(|(s, d)| {
(
s.clone(),
d.with_new_resolver(s, Some(resolver), Some(npm_resolver)),
)
})
.collect(),
);
maybe_types_dependency = self.maybe_types_dependency.as_ref().map(|d| {
Arc::new(d.with_new_resolver(Some(resolver), Some(npm_resolver)))
});
maybe_parsed_source = self.maybe_parsed_source.clone();
}
let maybe_module = Some(analyze_module(
&self.specifier,
&parsed_source_result,
self.maybe_headers.as_ref(),
resolver,
npm_resolver,
));
let dependencies =
Arc::new(DocumentDependencies::from_maybe_module(&maybe_module));
Some(Arc::new(Self {
// updated properties
dependencies,
maybe_module,
maybe_types_dependency,
maybe_navigation_tree: Mutex::new(None),
maybe_parsed_source: Some(parsed_source_result),
maybe_parsed_source: maybe_parsed_source
.filter(|_| self.specifier.scheme() == "file"),
// maintain - this should all be copies/clones
fs_version: self.fs_version.clone(),
line_index: self.line_index.clone(),
@ -385,18 +388,24 @@ impl Document {
} else {
(None, None)
};
let dependencies =
Arc::new(DocumentDependencies::from_maybe_module(&maybe_module));
let maybe_module = maybe_module.and_then(Result::ok);
let dependencies = maybe_module
.as_ref()
.map(|m| Arc::new(m.dependencies.clone()))
.unwrap_or_default();
let maybe_types_dependency = maybe_module
.as_ref()
.and_then(|m| Some(Arc::new(m.maybe_types_dependency.clone()?)));
let line_index = Arc::new(LineIndex::new(text_info.text_str()));
Arc::new(Self {
dependencies,
maybe_types_dependency,
fs_version: calculate_fs_version(cache, &specifier)
.unwrap_or_else(|| "1".to_string()),
line_index,
maybe_language_id: Some(language_id),
maybe_lsp_version: Some(version),
maybe_headers,
maybe_module,
maybe_navigation_tree: Mutex::new(None),
maybe_parsed_source: maybe_parsed_source
.filter(|_| specifier.scheme() == "file"),
@ -448,11 +457,15 @@ impl Document {
} else {
(None, None)
};
let dependencies = if let Some(Ok(module)) = &maybe_module {
Arc::new(DocumentDependencies::from_module(module))
} else {
self.dependencies.clone() // use the last known good
};
let maybe_module = maybe_module.and_then(Result::ok);
let dependencies = maybe_module
.as_ref()
.map(|m| Arc::new(m.dependencies.clone()))
.unwrap_or_else(|| self.dependencies.clone());
let maybe_types_dependency = maybe_module
.as_ref()
.and_then(|m| Some(Arc::new(m.maybe_types_dependency.clone()?)))
.or_else(|| self.maybe_types_dependency.clone());
let line_index = if index_valid == IndexValid::All {
line_index
} else {
@ -463,10 +476,10 @@ impl Document {
fs_version: self.fs_version.clone(),
maybe_language_id: self.maybe_language_id,
dependencies,
maybe_types_dependency,
text_info,
line_index,
maybe_headers: self.maybe_headers.clone(),
maybe_module,
maybe_parsed_source: maybe_parsed_source
.filter(|_| self.specifier.scheme() == "file"),
maybe_lsp_version: Some(version),
@ -482,10 +495,10 @@ impl Document {
.unwrap_or_else(|| "1".to_string()),
maybe_language_id: self.maybe_language_id,
dependencies: self.dependencies.clone(),
maybe_types_dependency: self.maybe_types_dependency.clone(),
text_info: self.text_info.clone(),
line_index: self.line_index.clone(),
maybe_headers: self.maybe_headers.clone(),
maybe_module: self.maybe_module.clone(),
maybe_parsed_source: self.maybe_parsed_source.clone(),
maybe_lsp_version: self.maybe_lsp_version,
maybe_navigation_tree: Mutex::new(None),
@ -541,11 +554,11 @@ impl Document {
self.maybe_lsp_version.is_some()
}
pub fn maybe_types_dependency(&self) -> Resolution {
if let Some(types_dep) = self.dependencies.maybe_types_dependency.as_ref() {
types_dep.dependency.clone()
pub fn maybe_types_dependency(&self) -> &Resolution {
if let Some(types_dep) = self.maybe_types_dependency.as_deref() {
&types_dep.dependency
} else {
Resolution::None
&Resolution::None
}
}
@ -562,10 +575,6 @@ impl Document {
self.maybe_lsp_version
}
fn maybe_js_module(&self) -> Option<&ModuleResult> {
self.maybe_module.as_ref()
}
pub fn maybe_parsed_source(
&self,
) -> Option<Result<deno_ast::ParsedSource, deno_ast::ParseDiagnostic>> {
@ -591,7 +600,7 @@ impl Document {
}
pub fn dependencies(&self) -> &IndexMap<String, deno_graph::Dependency> {
&self.dependencies.deps
self.dependencies.as_ref()
}
/// If the supplied position is within a dependency range, return the resolved
@ -601,12 +610,11 @@ impl Document {
&self,
position: &lsp::Position,
) -> Option<(String, deno_graph::Dependency, deno_graph::Range)> {
let module = self.maybe_js_module()?.as_ref().ok()?;
let position = deno_graph::Position {
line: position.line as usize,
character: position.character as usize,
};
module.dependencies.iter().find_map(|(s, dep)| {
self.dependencies().iter().find_map(|(s, dep)| {
dep
.includes(&position)
.map(|r| (s.clone(), dep.clone(), r.clone()))
@ -1207,7 +1215,8 @@ impl Documents {
referrer: &ModuleSpecifier,
maybe_npm: Option<&StateNpmSnapshot>,
) -> Vec<Option<(ModuleSpecifier, MediaType)>> {
let dependencies = self.get(referrer).map(|d| d.dependencies.clone());
let document = self.get(referrer);
let dependencies = document.as_ref().map(|d| d.dependencies());
let mut results = Vec::new();
for specifier in specifiers {
if let Some(npm) = maybe_npm {
@ -1236,7 +1245,7 @@ impl Documents {
results.push(None);
}
} else if let Some(dep) =
dependencies.as_ref().and_then(|d| d.deps.get(specifier))
dependencies.as_ref().and_then(|d| d.get(specifier))
{
if let Some(specifier) = dep.maybe_type.maybe_specifier() {
results.push(self.resolve_dependency(specifier, maybe_npm, referrer));
@ -1540,12 +1549,7 @@ impl Documents {
let Some(doc) = self.get(specifier) else {
return Some((specifier.clone(), MediaType::from_specifier(specifier)));
};
let maybe_module = doc.maybe_js_module().and_then(|r| r.as_ref().ok());
let maybe_types_dependency = maybe_module
.and_then(|m| m.maybe_types_dependency.as_ref().map(|d| &d.dependency));
if let Some(specifier) =
maybe_types_dependency.and_then(|d| d.maybe_specifier())
{
if let Some(specifier) = doc.maybe_types_dependency().maybe_specifier() {
self.resolve_dependency(specifier, maybe_npm, referrer)
} else {
let media_type = doc.media_type();
@ -1643,7 +1647,7 @@ impl<'a> OpenDocumentsGraphLoader<'a> {
impl<'a> deno_graph::source::Loader for OpenDocumentsGraphLoader<'a> {
fn load(
&mut self,
&self,
specifier: &ModuleSpecifier,
options: deno_graph::source::LoadOptions,
) -> deno_graph::source::LoadFuture {
@ -1662,7 +1666,7 @@ impl<'a> deno_graph::source::Loader for OpenDocumentsGraphLoader<'a> {
}
fn cache_module_info(
&mut self,
&self,
specifier: &deno_ast::ModuleSpecifier,
source: &Arc<[u8]>,
module_info: &deno_graph::ModuleInfo,
@ -1717,7 +1721,7 @@ fn analyze_module(
match parsed_source_result {
Ok(parsed_source) => Ok(deno_graph::parse_module_from_ast(
deno_graph::ParseModuleFromAstOptions {
graph_kind: deno_graph::GraphKind::All,
graph_kind: deno_graph::GraphKind::TypesOnly,
specifier,
maybe_headers,
parsed_source,

View file

@ -1610,10 +1610,9 @@ impl Inner {
let hover = if let Some((_, dep, range)) = asset_or_doc
.get_maybe_dependency(&params.text_document_position_params.position)
{
let dep_maybe_types_dependency = dep
.get_code()
.and_then(|s| self.documents.get(s))
.map(|d| d.maybe_types_dependency());
let dep_doc = dep.get_code().and_then(|s| self.documents.get(s));
let dep_maybe_types_dependency =
dep_doc.as_ref().map(|d| d.maybe_types_dependency());
let value = match (dep.maybe_code.is_none(), dep.maybe_type.is_none(), &dep_maybe_types_dependency) {
(false, false, None) => format!(
"**Resolved Dependency**\n\n**Code**: {}\n\n**Types**: {}\n",

View file

@ -37,7 +37,7 @@ async fn generate_doc_nodes_for_builtin_types(
let source_file_specifier =
ModuleSpecifier::parse("internal://lib.deno.d.ts").unwrap();
let content = get_types_declaration_file_text();
let mut loader = deno_graph::source::MemoryLoader::new(
let loader = deno_graph::source::MemoryLoader::new(
vec![(
source_file_specifier.to_string(),
deno_graph::source::Source::Module {
@ -52,7 +52,7 @@ async fn generate_doc_nodes_for_builtin_types(
graph
.build(
vec![source_file_specifier.clone()],
&mut loader,
&loader,
deno_graph::BuildOptions {
module_analyzer: analyzer,
file_system: &NullFileSystem,
@ -60,6 +60,7 @@ async fn generate_doc_nodes_for_builtin_types(
imports: Vec::new(),
executor: Default::default(),
jsr_url_provider: Default::default(),
passthrough_jsr_specifiers: false,
npm_resolver: None,
reporter: None,
resolver: None,

View file

@ -627,6 +627,7 @@ impl ReplSession {
imports_not_used_as_values: ImportsNotUsedAsValues::Preserve,
transform_jsx: true,
precompile_jsx: false,
precompile_jsx_skip_elements: None,
jsx_automatic: self.jsx.import_source.is_some(),
jsx_development: false,
jsx_factory: self.jsx.factory.clone(),

View file

@ -112,7 +112,7 @@ impl TestLoader {
impl Loader for TestLoader {
fn load(
&mut self,
&self,
specifier: &ModuleSpecifier,
_options: deno_graph::source::LoadOptions,
) -> LoadFuture {
@ -298,7 +298,7 @@ fn build_resolver(
async fn build_test_graph(
roots: Vec<ModuleSpecifier>,
mut loader: TestLoader,
loader: TestLoader,
resolver: &dyn deno_graph::source::Resolver,
analyzer: &dyn deno_graph::ModuleAnalyzer,
) -> ModuleGraph {
@ -306,7 +306,7 @@ async fn build_test_graph(
graph
.build(
roots,
&mut loader,
&loader,
deno_graph::BuildOptions {
resolver: Some(resolver),
module_analyzer: analyzer,

View file

@ -957,7 +957,7 @@ mod tests {
impl deno_graph::source::Loader for MockLoader {
fn load(
&mut self,
&self,
specifier: &ModuleSpecifier,
_options: deno_graph::source::LoadOptions,
) -> deno_graph::source::LoadFuture {
@ -987,10 +987,10 @@ mod tests {
.unwrap_or_else(|| ModuleSpecifier::parse("file:///main.ts").unwrap());
let hash_data = maybe_hash_data.unwrap_or(0);
let fixtures = test_util::testdata_path().join("tsc2");
let mut loader = MockLoader { fixtures };
let loader = MockLoader { fixtures };
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
graph
.build(vec![specifier], &mut loader, Default::default())
.build(vec![specifier], &loader, Default::default())
.await;
let state = State::new(
Arc::new(graph),
@ -1013,10 +1013,10 @@ mod tests {
) -> Result<Response, AnyError> {
let hash_data = 123; // something random
let fixtures = test_util::testdata_path().join("tsc2");
let mut loader = MockLoader { fixtures };
let loader = MockLoader { fixtures };
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
graph
.build(vec![specifier.clone()], &mut loader, Default::default())
.build(vec![specifier.clone()], &loader, Default::default())
.await;
let config = TsConfig::new(json!({
"allowJs": true,