2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-01-22 05:03:16 -05:00
|
|
|
|
2022-02-01 21:04:26 -05:00
|
|
|
use super::cache::calculate_fs_version;
|
2023-07-08 16:06:45 -04:00
|
|
|
use super::cache::calculate_fs_version_at_path;
|
2023-09-28 16:43:45 -04:00
|
|
|
use super::language_server::StateNpmSnapshot;
|
2021-01-22 05:03:16 -05:00
|
|
|
use super::text::LineIndex;
|
2021-06-04 17:31:44 -04:00
|
|
|
use super::tsc;
|
2021-11-12 11:42:04 -05:00
|
|
|
use super::tsc::AssetDocument;
|
2021-01-22 05:03:16 -05:00
|
|
|
|
2023-02-23 10:58:10 -05:00
|
|
|
use crate::args::package_json;
|
2023-03-03 17:27:05 -05:00
|
|
|
use crate::args::package_json::PackageJsonDeps;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::ConfigFile;
|
2023-01-28 10:18:32 -05:00
|
|
|
use crate::args::JsxImportSourceConfig;
|
|
|
|
use crate::cache::FastInsecureHasher;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::cache::HttpCache;
|
2021-10-28 19:56:01 -04:00
|
|
|
use crate::file_fetcher::get_source_from_bytes;
|
2023-07-01 18:52:30 -04:00
|
|
|
use crate::file_fetcher::get_source_from_data_url;
|
2021-10-28 19:56:01 -04:00
|
|
|
use crate::file_fetcher::map_content_type;
|
2023-04-01 15:10:30 -04:00
|
|
|
use crate::lsp::logging::lsp_warn;
|
2023-09-30 12:06:38 -04:00
|
|
|
use crate::npm::CliNpmResolver;
|
2023-02-15 11:30:54 -05:00
|
|
|
use crate::resolver::CliGraphResolver;
|
2023-08-17 12:14:22 -04:00
|
|
|
use crate::resolver::CliGraphResolverOptions;
|
2023-12-07 15:59:13 -05:00
|
|
|
use crate::resolver::SloppyImportsFsEntry;
|
|
|
|
use crate::resolver::SloppyImportsResolution;
|
|
|
|
use crate::resolver::SloppyImportsResolver;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::path::specifier_to_file_path;
|
|
|
|
use crate::util::text_encoding;
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2022-08-22 12:14:59 -04:00
|
|
|
use deno_ast::ParsedSource;
|
2021-10-28 19:56:01 -04:00
|
|
|
use deno_ast::SourceTextInfo;
|
2024-01-15 19:15:39 -05:00
|
|
|
use deno_config::glob::FilePatterns;
|
|
|
|
use deno_config::glob::PathOrPattern;
|
|
|
|
use deno_config::glob::PathOrPatternSet;
|
2021-01-22 05:03:16 -05:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-10-20 13:23:21 -04:00
|
|
|
use deno_core::futures::future;
|
2023-09-07 09:09:16 -04:00
|
|
|
use deno_core::futures::FutureExt;
|
2021-10-28 19:56:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
|
|
|
use deno_core::url;
|
2021-01-22 05:03:16 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-10-24 09:37:02 -04:00
|
|
|
use deno_graph::source::ResolutionMode;
|
2022-08-09 17:27:22 -04:00
|
|
|
use deno_graph::GraphImport;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::Resolution;
|
2023-10-25 14:39:00 -04:00
|
|
|
use deno_runtime::deno_fs::RealFs;
|
2023-04-21 21:02:46 -04:00
|
|
|
use deno_runtime::deno_node;
|
|
|
|
use deno_runtime::deno_node::NodeResolution;
|
2022-11-30 18:07:32 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2023-10-25 14:39:00 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2023-02-23 10:58:10 -05:00
|
|
|
use deno_runtime::deno_node::PackageJson;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageReq;
|
2023-09-07 09:09:16 -04:00
|
|
|
use indexmap::IndexMap;
|
2023-03-30 17:47:53 -04:00
|
|
|
use lsp::Url;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2023-05-10 20:06:59 -04:00
|
|
|
use package_json::PackageJsonDepsProvider;
|
2023-12-06 19:03:18 -05:00
|
|
|
use std::borrow::Cow;
|
2021-01-22 05:03:16 -05:00
|
|
|
use std::collections::HashMap;
|
2021-06-03 07:13:53 -04:00
|
|
|
use std::collections::HashSet;
|
2022-11-29 19:32:18 -05:00
|
|
|
use std::collections::VecDeque;
|
2021-10-28 19:56:01 -04:00
|
|
|
use std::fs;
|
2023-03-30 17:47:53 -04:00
|
|
|
use std::fs::ReadDir;
|
2021-01-22 05:03:16 -05:00
|
|
|
use std::ops::Range;
|
2021-10-28 19:56:01 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2024-01-08 12:18:42 -05:00
|
|
|
use std::rc::Rc;
|
2021-06-02 06:29:58 -04:00
|
|
|
use std::str::FromStr;
|
2021-09-07 10:39:32 -04:00
|
|
|
use std::sync::Arc;
|
2022-04-03 00:17:30 -04:00
|
|
|
use tower_lsp::lsp_types as lsp;
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
static JS_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
|
|
|
([(
|
|
|
|
"content-type".to_string(),
|
|
|
|
"application/javascript".to_string(),
|
|
|
|
)])
|
2023-01-16 15:27:41 -05:00
|
|
|
.into_iter()
|
2021-12-18 16:14:42 -05:00
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
static JSX_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
|
|
|
([("content-type".to_string(), "text/jsx".to_string())])
|
2023-01-16 15:27:41 -05:00
|
|
|
.into_iter()
|
2021-12-18 16:14:42 -05:00
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
static TS_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
|
|
|
([(
|
|
|
|
"content-type".to_string(),
|
|
|
|
"application/typescript".to_string(),
|
|
|
|
)])
|
2023-01-16 15:27:41 -05:00
|
|
|
.into_iter()
|
2021-12-18 16:14:42 -05:00
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
static TSX_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
|
|
|
([("content-type".to_string(), "text/tsx".to_string())])
|
2023-01-16 15:27:41 -05:00
|
|
|
.into_iter()
|
2021-12-18 16:14:42 -05:00
|
|
|
.collect()
|
|
|
|
});
|
2021-06-02 06:29:58 -04:00
|
|
|
|
2023-11-24 17:35:33 -05:00
|
|
|
pub const DOCUMENT_SCHEMES: [&str; 5] =
|
|
|
|
["data", "blob", "file", "http", "https"];
|
2023-09-29 15:44:59 -04:00
|
|
|
|
2022-12-20 15:19:35 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub enum LanguageId {
|
2021-06-02 06:29:58 -04:00
|
|
|
JavaScript,
|
|
|
|
Jsx,
|
|
|
|
TypeScript,
|
|
|
|
Tsx,
|
|
|
|
Json,
|
|
|
|
JsonC,
|
|
|
|
Markdown,
|
2021-08-18 23:19:12 -04:00
|
|
|
Unknown,
|
2021-06-02 06:29:58 -04:00
|
|
|
}
|
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
impl LanguageId {
|
2022-12-20 15:19:35 -05:00
|
|
|
pub fn as_media_type(&self) -> MediaType {
|
|
|
|
match self {
|
|
|
|
LanguageId::JavaScript => MediaType::JavaScript,
|
|
|
|
LanguageId::Jsx => MediaType::Jsx,
|
|
|
|
LanguageId::TypeScript => MediaType::TypeScript,
|
|
|
|
LanguageId::Tsx => MediaType::Tsx,
|
|
|
|
LanguageId::Json => MediaType::Json,
|
|
|
|
LanguageId::JsonC => MediaType::Json,
|
|
|
|
LanguageId::Markdown | LanguageId::Unknown => MediaType::Unknown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_extension(&self) -> Option<&'static str> {
|
|
|
|
match self {
|
|
|
|
LanguageId::JavaScript => Some("js"),
|
|
|
|
LanguageId::Jsx => Some("jsx"),
|
|
|
|
LanguageId::TypeScript => Some("ts"),
|
|
|
|
LanguageId::Tsx => Some("tsx"),
|
|
|
|
LanguageId::Json => Some("json"),
|
|
|
|
LanguageId::JsonC => Some("jsonc"),
|
|
|
|
LanguageId::Markdown => Some("md"),
|
|
|
|
LanguageId::Unknown => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
fn as_headers(&self) -> Option<&HashMap<String, String>> {
|
|
|
|
match self {
|
|
|
|
Self::JavaScript => Some(&JS_HEADERS),
|
|
|
|
Self::Jsx => Some(&JSX_HEADERS),
|
|
|
|
Self::TypeScript => Some(&TS_HEADERS),
|
|
|
|
Self::Tsx => Some(&TSX_HEADERS),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_diagnosable(&self) -> bool {
|
|
|
|
matches!(
|
|
|
|
self,
|
|
|
|
Self::JavaScript | Self::Jsx | Self::TypeScript | Self::Tsx
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 06:29:58 -04:00
|
|
|
impl FromStr for LanguageId {
|
|
|
|
type Err = AnyError;
|
|
|
|
|
2021-08-18 23:19:12 -04:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2021-06-02 06:29:58 -04:00
|
|
|
match s {
|
|
|
|
"javascript" => Ok(Self::JavaScript),
|
2021-10-28 19:56:01 -04:00
|
|
|
"javascriptreact" | "jsx" => Ok(Self::Jsx),
|
2021-06-02 06:29:58 -04:00
|
|
|
"typescript" => Ok(Self::TypeScript),
|
2021-10-28 19:56:01 -04:00
|
|
|
"typescriptreact" | "tsx" => Ok(Self::Tsx),
|
2021-06-02 06:29:58 -04:00
|
|
|
"json" => Ok(Self::Json),
|
|
|
|
"jsonc" => Ok(Self::JsonC),
|
|
|
|
"markdown" => Ok(Self::Markdown),
|
2021-08-18 23:19:12 -04:00
|
|
|
_ => Ok(Self::Unknown),
|
2021-06-02 06:29:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
enum IndexValid {
|
|
|
|
All,
|
|
|
|
UpTo(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexValid {
|
|
|
|
fn covers(&self, line: u32) -> bool {
|
|
|
|
match *self {
|
|
|
|
IndexValid::UpTo(to) => to > line,
|
|
|
|
IndexValid::All => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2022-03-02 16:06:38 -05:00
|
|
|
pub enum AssetOrDocument {
|
2021-11-12 11:42:04 -05:00
|
|
|
Document(Document),
|
|
|
|
Asset(AssetDocument),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssetOrDocument {
|
2022-03-02 16:06:38 -05:00
|
|
|
pub fn specifier(&self) -> &ModuleSpecifier {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(asset) => asset.specifier(),
|
|
|
|
AssetOrDocument::Document(doc) => doc.specifier(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn document(&self) -> Option<&Document> {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(_) => None,
|
|
|
|
AssetOrDocument::Document(doc) => Some(doc),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
pub fn text(&self) -> Arc<str> {
|
2021-11-12 11:42:04 -05:00
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(a) => a.text(),
|
|
|
|
AssetOrDocument::Document(d) => d.0.text_info.text(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn line_index(&self) -> Arc<LineIndex> {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(a) => a.line_index(),
|
|
|
|
AssetOrDocument::Document(d) => d.line_index(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_navigation_tree(&self) -> Option<Arc<tsc::NavigationTree>> {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(a) => a.maybe_navigation_tree(),
|
|
|
|
AssetOrDocument::Document(d) => d.maybe_navigation_tree(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 14:41:37 -04:00
|
|
|
pub fn media_type(&self) -> MediaType {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(_) => MediaType::TypeScript, // assets are always TypeScript
|
|
|
|
AssetOrDocument::Document(d) => d.media_type(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn get_maybe_dependency(
|
|
|
|
&self,
|
|
|
|
position: &lsp::Position,
|
|
|
|
) -> Option<(String, deno_graph::Dependency, deno_graph::Range)> {
|
|
|
|
self
|
|
|
|
.document()
|
2022-02-24 20:03:12 -05:00
|
|
|
.and_then(|d| d.get_maybe_dependency(position))
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_parsed_source(
|
|
|
|
&self,
|
2022-08-22 12:14:59 -04:00
|
|
|
) -> Option<Result<deno_ast::ParsedSource, deno_ast::Diagnostic>> {
|
2022-02-24 20:03:12 -05:00
|
|
|
self.document().and_then(|d| d.maybe_parsed_source())
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
pub fn document_lsp_version(&self) -> Option<i32> {
|
2022-02-24 20:03:12 -05:00
|
|
|
self.document().and_then(|d| d.maybe_lsp_version())
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
2022-03-02 16:06:38 -05:00
|
|
|
|
|
|
|
pub fn is_open(&self) -> bool {
|
|
|
|
self.document().map(|d| d.is_open()).unwrap_or(false)
|
|
|
|
}
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct DocumentDependencies {
|
2023-02-22 14:15:25 -05:00
|
|
|
deps: IndexMap<String, deno_graph::Dependency>,
|
2023-02-09 22:00:23 -05:00
|
|
|
maybe_types_dependency: Option<deno_graph::TypesDependency>,
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentDependencies {
|
2023-01-28 10:18:32 -05:00
|
|
|
pub fn from_maybe_module(maybe_module: &Option<ModuleResult>) -> Self {
|
2022-10-21 11:20:18 -04:00
|
|
|
if let Some(Ok(module)) = &maybe_module {
|
|
|
|
Self::from_module(module)
|
|
|
|
} else {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
pub fn from_module(module: &deno_graph::EsmModule) -> Self {
|
2023-11-24 17:35:33 -05:00
|
|
|
Self {
|
2022-10-21 11:20:18 -04:00
|
|
|
deps: module.dependencies.clone(),
|
|
|
|
maybe_types_dependency: module.maybe_types_dependency.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
type ModuleResult = Result<deno_graph::EsmModule, deno_graph::ModuleGraphError>;
|
2023-01-28 10:18:32 -05:00
|
|
|
type ParsedSourceResult = Result<ParsedSource, deno_ast::Diagnostic>;
|
2022-08-22 12:14:59 -04:00
|
|
|
|
2022-12-19 20:22:17 -05:00
|
|
|
#[derive(Debug)]
|
2021-11-12 11:42:04 -05:00
|
|
|
struct DocumentInner {
|
2022-12-19 20:22:17 -05:00
|
|
|
/// Contains the last-known-good set of dependencies from parsing the module.
|
2022-10-21 11:20:18 -04:00
|
|
|
dependencies: Arc<DocumentDependencies>,
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version: String,
|
2021-10-28 19:56:01 -04:00
|
|
|
line_index: Arc<LineIndex>,
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_headers: Option<HashMap<String, String>>,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_language_id: Option<LanguageId>,
|
|
|
|
maybe_lsp_version: Option<i32>,
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_module: Option<ModuleResult>,
|
2022-12-19 20:22:17 -05:00
|
|
|
// 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>>>,
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_parsed_source: Option<ParsedSourceResult>,
|
2021-03-24 20:13:37 -04:00
|
|
|
specifier: ModuleSpecifier,
|
2021-11-12 11:42:04 -05:00
|
|
|
text_info: SourceTextInfo,
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2022-03-02 16:06:38 -05:00
|
|
|
pub struct Document(Arc<DocumentInner>);
|
2021-11-12 11:42:04 -05:00
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
impl Document {
|
|
|
|
fn new(
|
2021-06-02 06:29:58 -04:00
|
|
|
specifier: ModuleSpecifier,
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version: String,
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_headers: Option<HashMap<String, String>>,
|
|
|
|
text_info: SourceTextInfo,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver: &dyn deno_graph::source::NpmResolver,
|
2021-06-02 06:29:58 -04:00
|
|
|
) -> Self {
|
2021-10-28 19:56:01 -04:00
|
|
|
// we only ever do `Document::new` on on disk resources that are supposed to
|
|
|
|
// be diagnosable, unlike `Document::open`, so it is safe to unconditionally
|
|
|
|
// parse the module.
|
2023-01-28 10:18:32 -05:00
|
|
|
let (maybe_parsed_source, maybe_module) = parse_and_analyze_module(
|
2021-10-28 19:56:01 -04:00
|
|
|
&specifier,
|
2023-01-28 10:18:32 -05:00
|
|
|
text_info.clone(),
|
|
|
|
maybe_headers.as_ref(),
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver,
|
2022-08-22 12:14:59 -04:00
|
|
|
);
|
2022-10-21 11:20:18 -04:00
|
|
|
let dependencies =
|
|
|
|
Arc::new(DocumentDependencies::from_maybe_module(&maybe_module));
|
2021-11-12 11:42:04 -05:00
|
|
|
let line_index = Arc::new(LineIndex::new(text_info.text_str()));
|
|
|
|
Self(Arc::new(DocumentInner {
|
2021-11-16 17:23:25 -05:00
|
|
|
dependencies,
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version,
|
2021-10-28 19:56:01 -04:00
|
|
|
line_index,
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_headers,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_language_id: None,
|
|
|
|
maybe_lsp_version: None,
|
|
|
|
maybe_module,
|
2022-12-19 20:22:17 -05:00
|
|
|
maybe_navigation_tree: Mutex::new(None),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_parsed_source,
|
2021-11-12 11:42:04 -05:00
|
|
|
text_info,
|
2021-03-24 20:13:37 -04:00
|
|
|
specifier,
|
2021-11-12 11:42:04 -05:00
|
|
|
}))
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
|
|
|
|
2023-01-28 10:18:32 -05:00
|
|
|
fn maybe_with_new_resolver(
|
|
|
|
&self,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver: &dyn deno_graph::source::NpmResolver,
|
2023-01-28 10:18:32 -05:00
|
|
|
) -> Option<Self> {
|
|
|
|
let parsed_source_result = match &self.0.maybe_parsed_source {
|
|
|
|
Some(parsed_source_result) => parsed_source_result.clone(),
|
|
|
|
None => return None, // nothing to change
|
|
|
|
};
|
|
|
|
let maybe_module = Some(analyze_module(
|
|
|
|
&self.0.specifier,
|
|
|
|
&parsed_source_result,
|
|
|
|
self.0.maybe_headers.as_ref(),
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver,
|
2023-01-28 10:18:32 -05:00
|
|
|
));
|
|
|
|
let dependencies =
|
|
|
|
Arc::new(DocumentDependencies::from_maybe_module(&maybe_module));
|
|
|
|
Some(Self(Arc::new(DocumentInner {
|
|
|
|
// updated properties
|
|
|
|
dependencies,
|
|
|
|
maybe_module,
|
|
|
|
maybe_navigation_tree: Mutex::new(None),
|
|
|
|
maybe_parsed_source: Some(parsed_source_result),
|
|
|
|
// maintain - this should all be copies/clones
|
|
|
|
fs_version: self.0.fs_version.clone(),
|
|
|
|
line_index: self.0.line_index.clone(),
|
|
|
|
maybe_headers: self.0.maybe_headers.clone(),
|
|
|
|
maybe_language_id: self.0.maybe_language_id,
|
|
|
|
maybe_lsp_version: self.0.maybe_lsp_version,
|
|
|
|
text_info: self.0.text_info.clone(),
|
|
|
|
specifier: self.0.specifier.clone(),
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
fn open(
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
version: i32,
|
|
|
|
language_id: LanguageId,
|
2022-05-20 16:40:55 -04:00
|
|
|
content: Arc<str>,
|
2023-10-12 10:37:56 -04:00
|
|
|
cache: &Arc<dyn HttpCache>,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver: &dyn deno_graph::source::NpmResolver,
|
2021-11-12 11:42:04 -05:00
|
|
|
) -> Self {
|
|
|
|
let maybe_headers = language_id.as_headers();
|
2023-01-28 10:18:32 -05:00
|
|
|
let text_info = SourceTextInfo::new(content);
|
|
|
|
let (maybe_parsed_source, maybe_module) = if language_id.is_diagnosable() {
|
|
|
|
parse_and_analyze_module(
|
2021-11-12 11:42:04 -05:00
|
|
|
&specifier,
|
2023-01-28 10:18:32 -05:00
|
|
|
text_info.clone(),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_headers,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver,
|
2022-08-22 12:14:59 -04:00
|
|
|
)
|
2021-11-12 11:42:04 -05:00
|
|
|
} else {
|
2022-08-22 12:14:59 -04:00
|
|
|
(None, None)
|
2021-11-12 11:42:04 -05:00
|
|
|
};
|
2022-10-21 11:20:18 -04:00
|
|
|
let dependencies =
|
|
|
|
Arc::new(DocumentDependencies::from_maybe_module(&maybe_module));
|
2023-01-28 10:18:32 -05:00
|
|
|
let line_index = Arc::new(LineIndex::new(text_info.text_str()));
|
2021-11-12 11:42:04 -05:00
|
|
|
Self(Arc::new(DocumentInner {
|
2021-11-16 17:23:25 -05:00
|
|
|
dependencies,
|
2023-10-12 10:37:56 -04:00
|
|
|
fs_version: calculate_fs_version(cache, &specifier)
|
|
|
|
.unwrap_or_else(|| "1".to_string()),
|
2021-11-12 11:42:04 -05:00
|
|
|
line_index,
|
|
|
|
maybe_language_id: Some(language_id),
|
|
|
|
maybe_lsp_version: Some(version),
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_headers: maybe_headers.map(ToOwned::to_owned),
|
2021-11-12 11:42:04 -05:00
|
|
|
maybe_module,
|
2022-12-19 20:22:17 -05:00
|
|
|
maybe_navigation_tree: Mutex::new(None),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_parsed_source,
|
2023-01-28 10:18:32 -05:00
|
|
|
text_info,
|
2021-11-12 11:42:04 -05:00
|
|
|
specifier,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_change(
|
|
|
|
&self,
|
2021-10-28 19:56:01 -04:00
|
|
|
version: i32,
|
|
|
|
changes: Vec<lsp::TextDocumentContentChangeEvent>,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver: &dyn deno_graph::source::NpmResolver,
|
2021-11-12 11:42:04 -05:00
|
|
|
) -> Result<Document, AnyError> {
|
|
|
|
let mut content = self.0.text_info.text_str().to_string();
|
|
|
|
let mut line_index = self.0.line_index.clone();
|
2021-01-22 05:03:16 -05:00
|
|
|
let mut index_valid = IndexValid::All;
|
2021-10-28 19:56:01 -04:00
|
|
|
for change in changes {
|
2021-01-22 05:03:16 -05:00
|
|
|
if let Some(range) = change.range {
|
|
|
|
if !index_valid.covers(range.start.line) {
|
2021-10-28 19:56:01 -04:00
|
|
|
line_index = Arc::new(LineIndex::new(&content));
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
index_valid = IndexValid::UpTo(range.start.line);
|
|
|
|
let range = line_index.get_text_range(range)?;
|
|
|
|
content.replace_range(Range::<usize>::from(range), &change.text);
|
|
|
|
} else {
|
2021-09-07 10:39:32 -04:00
|
|
|
content = change.text;
|
2021-01-22 05:03:16 -05:00
|
|
|
index_valid = IndexValid::UpTo(0);
|
|
|
|
}
|
|
|
|
}
|
2023-01-28 10:18:32 -05:00
|
|
|
let text_info = SourceTextInfo::from_string(content);
|
|
|
|
let (maybe_parsed_source, maybe_module) = if self
|
2021-11-12 11:42:04 -05:00
|
|
|
.0
|
2021-10-28 19:56:01 -04:00
|
|
|
.maybe_language_id
|
|
|
|
.as_ref()
|
|
|
|
.map(|li| li.is_diagnosable())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
let maybe_headers = self
|
2021-11-12 11:42:04 -05:00
|
|
|
.0
|
2021-10-28 19:56:01 -04:00
|
|
|
.maybe_language_id
|
|
|
|
.as_ref()
|
2022-02-24 20:03:12 -05:00
|
|
|
.and_then(|li| li.as_headers());
|
2023-01-28 10:18:32 -05:00
|
|
|
parse_and_analyze_module(
|
2021-11-12 11:42:04 -05:00
|
|
|
&self.0.specifier,
|
2023-01-28 10:18:32 -05:00
|
|
|
text_info.clone(),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_headers,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver,
|
2022-08-22 12:14:59 -04:00
|
|
|
)
|
2021-10-28 19:56:01 -04:00
|
|
|
} else {
|
2022-08-22 12:14:59 -04:00
|
|
|
(None, None)
|
2021-11-12 11:42:04 -05:00
|
|
|
};
|
2021-11-16 17:23:25 -05:00
|
|
|
let dependencies = if let Some(Ok(module)) = &maybe_module {
|
2022-10-21 11:20:18 -04:00
|
|
|
Arc::new(DocumentDependencies::from_module(module))
|
2021-11-16 17:23:25 -05:00
|
|
|
} else {
|
2022-10-21 11:20:18 -04:00
|
|
|
self.0.dependencies.clone() // use the last known good
|
2021-11-16 17:23:25 -05:00
|
|
|
};
|
2021-11-12 11:42:04 -05:00
|
|
|
let line_index = if index_valid == IndexValid::All {
|
2021-09-07 10:39:32 -04:00
|
|
|
line_index
|
2021-01-22 05:03:16 -05:00
|
|
|
} else {
|
2021-11-16 17:23:25 -05:00
|
|
|
Arc::new(LineIndex::new(text_info.text_str()))
|
2021-01-22 05:03:16 -05:00
|
|
|
};
|
2021-11-12 11:42:04 -05:00
|
|
|
Ok(Document(Arc::new(DocumentInner {
|
2022-12-19 20:22:17 -05:00
|
|
|
specifier: self.0.specifier.clone(),
|
|
|
|
fs_version: self.0.fs_version.clone(),
|
2022-12-20 15:19:35 -05:00
|
|
|
maybe_language_id: self.0.maybe_language_id,
|
2021-11-16 17:23:25 -05:00
|
|
|
dependencies,
|
|
|
|
text_info,
|
2021-11-12 11:42:04 -05:00
|
|
|
line_index,
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_headers: self.0.maybe_headers.clone(),
|
2021-11-12 11:42:04 -05:00
|
|
|
maybe_module,
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_parsed_source,
|
2021-11-12 11:42:04 -05:00
|
|
|
maybe_lsp_version: Some(version),
|
2022-12-19 20:22:17 -05:00
|
|
|
maybe_navigation_tree: Mutex::new(None),
|
2021-11-12 11:42:04 -05:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2023-10-12 10:37:56 -04:00
|
|
|
pub fn saved(&self, cache: &Arc<dyn HttpCache>) -> Document {
|
|
|
|
Document(Arc::new(DocumentInner {
|
|
|
|
specifier: self.0.specifier.clone(),
|
|
|
|
fs_version: calculate_fs_version(cache, &self.0.specifier)
|
|
|
|
.unwrap_or_else(|| "1".to_string()),
|
|
|
|
maybe_language_id: self.0.maybe_language_id,
|
|
|
|
dependencies: self.0.dependencies.clone(),
|
|
|
|
text_info: self.0.text_info.clone(),
|
|
|
|
line_index: self.0.line_index.clone(),
|
|
|
|
maybe_headers: self.0.maybe_headers.clone(),
|
|
|
|
maybe_module: self.0.maybe_module.clone(),
|
|
|
|
maybe_parsed_source: self.0.maybe_parsed_source.clone(),
|
|
|
|
maybe_lsp_version: self.0.maybe_lsp_version,
|
|
|
|
maybe_navigation_tree: Mutex::new(None),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn specifier(&self) -> &ModuleSpecifier {
|
|
|
|
&self.0.specifier
|
2021-06-21 02:43:35 -04:00
|
|
|
}
|
2021-06-24 19:06:51 -04:00
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
pub fn content(&self) -> Arc<str> {
|
2021-11-12 11:42:04 -05:00
|
|
|
self.0.text_info.text()
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn text_info(&self) -> SourceTextInfo {
|
|
|
|
self.0.text_info.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn line_index(&self) -> Arc<LineIndex> {
|
|
|
|
self.0.line_index.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fs_version(&self) -> &str {
|
|
|
|
self.0.fs_version.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn script_version(&self) -> String {
|
|
|
|
self
|
|
|
|
.maybe_lsp_version()
|
2023-10-17 00:13:06 -04:00
|
|
|
.map(|v| format!("{}+{v}", self.fs_version()))
|
2023-03-15 17:46:36 -04:00
|
|
|
.unwrap_or_else(|| self.fs_version().to_string())
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_diagnosable(&self) -> bool {
|
2021-10-28 19:56:01 -04:00
|
|
|
matches!(
|
|
|
|
self.media_type(),
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Jsx
|
2021-12-09 17:12:21 -05:00
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
2021-10-28 19:56:01 -04:00
|
|
|
| MediaType::TypeScript
|
|
|
|
| MediaType::Tsx
|
2021-12-09 17:12:21 -05:00
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
2021-10-28 19:56:01 -04:00
|
|
|
| MediaType::Dts
|
2021-12-09 17:12:21 -05:00
|
|
|
| MediaType::Dmts
|
|
|
|
| MediaType::Dcts
|
2021-10-28 19:56:01 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn is_open(&self) -> bool {
|
|
|
|
self.0.maybe_lsp_version.is_some()
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:00:23 -05:00
|
|
|
pub fn maybe_types_dependency(&self) -> Resolution {
|
|
|
|
if let Some(types_dep) = self.0.dependencies.maybe_types_dependency.as_ref()
|
2022-10-21 11:20:18 -04:00
|
|
|
{
|
2023-02-09 22:00:23 -05:00
|
|
|
types_dep.dependency.clone()
|
2022-01-31 17:33:57 -05:00
|
|
|
} else {
|
2023-02-09 22:00:23 -05:00
|
|
|
Resolution::None
|
2022-01-31 17:33:57 -05:00
|
|
|
}
|
2021-11-07 19:50:48 -05:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn media_type(&self) -> MediaType {
|
|
|
|
if let Some(Ok(module)) = &self.0.maybe_module {
|
2022-12-20 15:19:35 -05:00
|
|
|
return module.media_type;
|
|
|
|
}
|
2023-03-21 11:46:40 -04:00
|
|
|
let specifier_media_type = MediaType::from_specifier(&self.0.specifier);
|
2022-12-20 15:19:35 -05:00
|
|
|
if specifier_media_type != MediaType::Unknown {
|
|
|
|
return specifier_media_type;
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2022-12-20 15:19:35 -05:00
|
|
|
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.maybe_language_id
|
|
|
|
.map(|id| id.as_media_type())
|
|
|
|
.unwrap_or(MediaType::Unknown)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_language_id(&self) -> Option<LanguageId> {
|
|
|
|
self.0.maybe_language_id
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
/// Returns the current language server client version if any.
|
|
|
|
pub fn maybe_lsp_version(&self) -> Option<i32> {
|
|
|
|
self.0.maybe_lsp_version
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
fn maybe_esm_module(&self) -> Option<&ModuleResult> {
|
2021-11-12 11:42:04 -05:00
|
|
|
self.0.maybe_module.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_parsed_source(
|
|
|
|
&self,
|
2022-08-22 12:14:59 -04:00
|
|
|
) -> Option<Result<deno_ast::ParsedSource, deno_ast::Diagnostic>> {
|
|
|
|
self.0.maybe_parsed_source.clone()
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_navigation_tree(&self) -> Option<Arc<tsc::NavigationTree>> {
|
2022-12-19 20:22:17 -05:00
|
|
|
self.0.maybe_navigation_tree.lock().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_navigation_tree_if_version(
|
|
|
|
&self,
|
|
|
|
tree: Arc<tsc::NavigationTree>,
|
|
|
|
script_version: &str,
|
|
|
|
) {
|
|
|
|
// Ensure we are updating the same document that the navigation tree was
|
|
|
|
// created for. Note: this should not be racy between the version check
|
|
|
|
// and setting the navigation tree, because the document is immutable
|
|
|
|
// and this is enforced by it being wrapped in an Arc.
|
|
|
|
if self.script_version() == script_version {
|
|
|
|
*self.0.maybe_navigation_tree.lock() = Some(tree);
|
|
|
|
}
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
pub fn dependencies(&self) -> &IndexMap<String, deno_graph::Dependency> {
|
2022-10-21 11:20:18 -04:00
|
|
|
&self.0.dependencies.deps
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the supplied position is within a dependency range, return the resolved
|
|
|
|
/// string specifier for the dependency, the resolved dependency and the range
|
|
|
|
/// in the source document of the specifier.
|
|
|
|
pub fn get_maybe_dependency(
|
|
|
|
&self,
|
|
|
|
position: &lsp::Position,
|
|
|
|
) -> Option<(String, deno_graph::Dependency, deno_graph::Range)> {
|
2023-02-22 14:15:25 -05:00
|
|
|
let module = self.maybe_esm_module()?.as_ref().ok()?;
|
2021-11-12 11:42:04 -05:00
|
|
|
let position = deno_graph::Position {
|
|
|
|
line: position.line as usize,
|
|
|
|
character: position.character as usize,
|
2021-10-28 19:56:01 -04:00
|
|
|
};
|
2021-11-12 11:42:04 -05:00
|
|
|
module.dependencies.iter().find_map(|(s, dep)| {
|
|
|
|
dep
|
|
|
|
.includes(&position)
|
|
|
|
.map(|r| (s.clone(), dep.clone(), r.clone()))
|
|
|
|
})
|
2021-06-24 19:06:51 -04:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:00:23 -05:00
|
|
|
pub fn to_hover_text(result: &Resolution) -> String {
|
2021-10-28 19:56:01 -04:00
|
|
|
match result {
|
2023-02-09 22:00:23 -05:00
|
|
|
Resolution::Ok(resolved) => {
|
|
|
|
let specifier = &resolved.specifier;
|
|
|
|
match specifier.scheme() {
|
|
|
|
"data" => "_(a data url)_".to_string(),
|
|
|
|
"blob" => "_(a blob url)_".to_string(),
|
|
|
|
_ => format!(
|
|
|
|
"{}​{}",
|
|
|
|
&specifier[..url::Position::AfterScheme],
|
|
|
|
&specifier[url::Position::AfterScheme..],
|
|
|
|
)
|
|
|
|
.replace('@', "​@"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Resolution::Err(_) => "_[errored]_".to_string(),
|
|
|
|
Resolution::None => "_[missing]_".to_string(),
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn to_lsp_range(range: &deno_graph::Range) -> lsp::Range {
|
2021-10-28 19:56:01 -04:00
|
|
|
lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: range.start.line as u32,
|
|
|
|
character: range.start.character as u32,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: range.end.line as u32,
|
|
|
|
character: range.end.character as u32,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recurse and collect specifiers that appear in the dependent map.
|
|
|
|
fn recurse_dependents(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
map: &HashMap<ModuleSpecifier, HashSet<ModuleSpecifier>>,
|
|
|
|
dependents: &mut HashSet<ModuleSpecifier>,
|
|
|
|
) {
|
|
|
|
if let Some(deps) = map.get(specifier) {
|
|
|
|
for dep in deps {
|
|
|
|
if !dependents.contains(dep) {
|
|
|
|
dependents.insert(dep.clone());
|
|
|
|
recurse_dependents(dep, map, dependents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
#[derive(Debug)]
|
2023-12-06 19:03:18 -05:00
|
|
|
struct RedirectResolver {
|
2023-08-01 20:49:09 -04:00
|
|
|
cache: Arc<dyn HttpCache>,
|
2021-11-18 13:50:24 -05:00
|
|
|
redirects: Mutex<HashMap<ModuleSpecifier, ModuleSpecifier>>,
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 19:03:18 -05:00
|
|
|
impl RedirectResolver {
|
2023-08-01 20:49:09 -04:00
|
|
|
pub fn new(cache: Arc<dyn HttpCache>) -> Self {
|
2021-10-28 19:56:01 -04:00
|
|
|
Self {
|
2023-06-10 11:09:45 -04:00
|
|
|
cache,
|
2021-11-18 13:50:24 -05:00
|
|
|
redirects: Mutex::new(HashMap::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
|
|
|
let scheme = specifier.scheme();
|
2023-09-29 15:44:59 -04:00
|
|
|
if !DOCUMENT_SCHEMES.contains(&scheme) {
|
2021-11-18 13:50:24 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-09-29 15:44:59 -04:00
|
|
|
if scheme == "http" || scheme == "https" {
|
2021-11-18 13:50:24 -05:00
|
|
|
let mut redirects = self.redirects.lock();
|
|
|
|
if let Some(specifier) = redirects.get(specifier) {
|
|
|
|
Some(specifier.clone())
|
|
|
|
} else {
|
|
|
|
let redirect = self.resolve_remote(specifier, 10)?;
|
|
|
|
redirects.insert(specifier.clone(), redirect.clone());
|
|
|
|
Some(redirect)
|
|
|
|
}
|
2023-09-29 15:44:59 -04:00
|
|
|
} else {
|
|
|
|
Some(specifier.clone())
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_remote(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
redirect_limit: usize,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
2023-07-08 16:06:45 -04:00
|
|
|
if redirect_limit > 0 {
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key = self.cache.cache_item_key(specifier).ok()?;
|
2023-07-08 16:06:45 -04:00
|
|
|
let headers = self
|
|
|
|
.cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_metadata(&cache_key)
|
2021-11-18 13:50:24 -05:00
|
|
|
.ok()
|
2023-08-01 20:49:09 -04:00
|
|
|
.flatten()
|
2021-11-18 13:50:24 -05:00
|
|
|
.map(|m| m.headers)?;
|
|
|
|
if let Some(location) = headers.get("location") {
|
|
|
|
let redirect =
|
|
|
|
deno_core::resolve_import(location, specifier.as_str()).ok()?;
|
|
|
|
self.resolve_remote(&redirect, redirect_limit - 1)
|
|
|
|
} else {
|
|
|
|
Some(specifier.clone())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct FileSystemDocuments {
|
|
|
|
docs: HashMap<ModuleSpecifier, Document>,
|
|
|
|
dirty: bool,
|
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
impl FileSystemDocuments {
|
2022-11-30 14:19:32 -05:00
|
|
|
pub fn get(
|
2022-11-29 19:32:18 -05:00
|
|
|
&mut self,
|
2023-08-01 20:49:09 -04:00
|
|
|
cache: &Arc<dyn HttpCache>,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2022-11-29 19:32:18 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver: &dyn deno_graph::source::NpmResolver,
|
2022-11-29 19:32:18 -05:00
|
|
|
) -> Option<Document> {
|
2023-07-01 18:52:30 -04:00
|
|
|
let fs_version = if specifier.scheme() == "data" {
|
|
|
|
Some("1".to_string())
|
|
|
|
} else {
|
2023-07-08 16:06:45 -04:00
|
|
|
calculate_fs_version(cache, specifier)
|
2023-07-01 18:52:30 -04:00
|
|
|
};
|
2022-11-29 19:32:18 -05:00
|
|
|
let file_system_doc = self.docs.get(specifier);
|
|
|
|
if file_system_doc.map(|d| d.fs_version().to_string()) != fs_version {
|
|
|
|
// attempt to update the file on the file system
|
2023-10-20 00:02:08 -04:00
|
|
|
self.refresh_document(cache, resolver, specifier, npm_resolver)
|
2022-11-29 19:32:18 -05:00
|
|
|
} else {
|
|
|
|
file_system_doc.cloned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 09:21:59 -05:00
|
|
|
/// Adds or updates a document by reading the document from the file system
|
|
|
|
/// returning the document.
|
2021-11-18 13:50:24 -05:00
|
|
|
fn refresh_document(
|
|
|
|
&mut self,
|
2023-08-01 20:49:09 -04:00
|
|
|
cache: &Arc<dyn HttpCache>,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2022-01-24 03:01:33 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver: &dyn deno_graph::source::NpmResolver,
|
2021-11-18 13:50:24 -05:00
|
|
|
) -> Option<Document> {
|
2021-10-28 19:56:01 -04:00
|
|
|
let doc = if specifier.scheme() == "file" {
|
2023-07-08 16:06:45 -04:00
|
|
|
let path = specifier_to_file_path(specifier).ok()?;
|
|
|
|
let fs_version = calculate_fs_version_at_path(&path)?;
|
2023-07-01 18:52:30 -04:00
|
|
|
let bytes = fs::read(path).ok()?;
|
2021-10-28 19:56:01 -04:00
|
|
|
let maybe_charset =
|
|
|
|
Some(text_encoding::detect_charset(&bytes).to_string());
|
2022-05-20 16:40:55 -04:00
|
|
|
let content = get_source_from_bytes(bytes, maybe_charset).ok()?;
|
2021-10-28 19:56:01 -04:00
|
|
|
Document::new(
|
|
|
|
specifier.clone(),
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version,
|
2021-10-28 19:56:01 -04:00
|
|
|
None,
|
2023-01-28 10:18:32 -05:00
|
|
|
SourceTextInfo::from_string(content),
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver,
|
2021-10-28 19:56:01 -04:00
|
|
|
)
|
2023-07-01 18:52:30 -04:00
|
|
|
} else if specifier.scheme() == "data" {
|
|
|
|
let (source, _) = get_source_from_data_url(specifier).ok()?;
|
|
|
|
Document::new(
|
|
|
|
specifier.clone(),
|
|
|
|
"1".to_string(),
|
|
|
|
None,
|
|
|
|
SourceTextInfo::from_string(source),
|
|
|
|
resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver,
|
2023-07-01 18:52:30 -04:00
|
|
|
)
|
2021-10-28 19:56:01 -04:00
|
|
|
} else {
|
2023-07-08 16:06:45 -04:00
|
|
|
let fs_version = calculate_fs_version(cache, specifier)?;
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key = cache.cache_item_key(specifier).ok()?;
|
|
|
|
let bytes = cache.read_file_bytes(&cache_key).ok()??;
|
|
|
|
let specifier_metadata = cache.read_metadata(&cache_key).ok()??;
|
2023-01-26 17:24:03 -05:00
|
|
|
let maybe_content_type = specifier_metadata.headers.get("content-type");
|
2022-01-24 03:01:33 -05:00
|
|
|
let (_, maybe_charset) = map_content_type(specifier, maybe_content_type);
|
2023-01-28 10:18:32 -05:00
|
|
|
let maybe_headers = Some(specifier_metadata.headers);
|
2022-05-20 16:40:55 -04:00
|
|
|
let content = get_source_from_bytes(bytes, maybe_charset).ok()?;
|
2021-10-28 19:56:01 -04:00
|
|
|
Document::new(
|
|
|
|
specifier.clone(),
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_headers,
|
2023-01-28 10:18:32 -05:00
|
|
|
SourceTextInfo::from_string(content),
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver,
|
2021-10-28 19:56:01 -04:00
|
|
|
)
|
|
|
|
};
|
|
|
|
self.dirty = true;
|
2022-01-25 09:21:59 -05:00
|
|
|
self.docs.insert(specifier.clone(), doc.clone());
|
|
|
|
Some(doc)
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
pub struct UpdateDocumentConfigOptions<'a> {
|
2024-01-15 19:15:39 -05:00
|
|
|
pub enabled_paths: PathOrPatternSet,
|
|
|
|
pub disabled_paths: PathOrPatternSet,
|
2023-05-11 17:17:14 -04:00
|
|
|
pub document_preload_limit: usize,
|
|
|
|
pub maybe_import_map: Option<Arc<import_map::ImportMap>>,
|
|
|
|
pub maybe_config_file: Option<&'a ConfigFile>,
|
|
|
|
pub maybe_package_json: Option<&'a PackageJson>,
|
2023-10-25 14:39:00 -04:00
|
|
|
pub node_resolver: Option<Arc<NodeResolver>>,
|
2023-09-30 12:06:38 -04:00
|
|
|
pub npm_resolver: Option<Arc<dyn CliNpmResolver>>,
|
2023-05-11 17:17:14 -04:00
|
|
|
}
|
|
|
|
|
2023-03-29 16:25:48 -04:00
|
|
|
/// Specify the documents to include on a `documents.documents(...)` call.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum DocumentsFilter {
|
|
|
|
/// Includes all the documents (diagnosable & non-diagnosable, open & file system).
|
|
|
|
All,
|
|
|
|
/// Includes all the diagnosable documents (open & file system).
|
|
|
|
AllDiagnosable,
|
|
|
|
/// Includes only the diagnosable documents that are open.
|
|
|
|
OpenDiagnosable,
|
|
|
|
}
|
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct Documents {
|
2021-11-18 13:50:24 -05:00
|
|
|
/// The DENO_DIR that the documents looks for non-file based modules.
|
2023-08-01 20:49:09 -04:00
|
|
|
cache: Arc<dyn HttpCache>,
|
2021-11-18 13:50:24 -05:00
|
|
|
/// A flag that indicates that stated data is potentially invalid and needs to
|
|
|
|
/// be recalculated before being considered valid.
|
|
|
|
dirty: bool,
|
|
|
|
/// A map where the key is a specifier and the value is a set of specifiers
|
|
|
|
/// that depend on the key.
|
|
|
|
dependents_map: Arc<HashMap<ModuleSpecifier, HashSet<ModuleSpecifier>>>,
|
|
|
|
/// A map of documents that are "open" in the language server.
|
|
|
|
open_docs: HashMap<ModuleSpecifier, Document>,
|
|
|
|
/// Documents stored on the file system.
|
|
|
|
file_system_docs: Arc<Mutex<FileSystemDocuments>>,
|
2023-01-28 10:18:32 -05:00
|
|
|
/// Hash of the config used for resolution. When the hash changes we update
|
|
|
|
/// dependencies.
|
|
|
|
resolver_config_hash: u64,
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Any imports to the context supplied by configuration files. This is like
|
|
|
|
/// the imports into the a module graph in CLI.
|
2023-03-11 11:43:45 -05:00
|
|
|
imports: Arc<IndexMap<ModuleSpecifier, GraphImport>>,
|
2022-11-02 10:47:02 -04:00
|
|
|
/// A resolver that takes into account currently loaded import map and JSX
|
|
|
|
/// settings.
|
2023-04-14 16:22:33 -04:00
|
|
|
resolver: Arc<CliGraphResolver>,
|
2023-02-23 10:58:10 -05:00
|
|
|
/// The npm package requirements found in npm specifiers.
|
2023-08-21 05:53:52 -04:00
|
|
|
npm_specifier_reqs: Arc<Vec<PackageReq>>,
|
2023-01-24 09:05:54 -05:00
|
|
|
/// Gets if any document had a node: specifier such that a @types/node package
|
|
|
|
/// should be injected.
|
|
|
|
has_injected_types_node_package: bool,
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Resolves a specifier to its final redirected to specifier.
|
2023-12-06 19:03:18 -05:00
|
|
|
redirect_resolver: Arc<RedirectResolver>,
|
|
|
|
/// If --unstable-sloppy-imports is enabled.
|
|
|
|
unstable_sloppy_imports: bool,
|
2024-01-11 12:07:44 -05:00
|
|
|
project_version: usize,
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Documents {
|
2023-08-01 20:49:09 -04:00
|
|
|
pub fn new(cache: Arc<dyn HttpCache>) -> Self {
|
2021-11-18 13:50:24 -05:00
|
|
|
Self {
|
2023-06-10 11:09:45 -04:00
|
|
|
cache: cache.clone(),
|
2021-11-18 13:50:24 -05:00
|
|
|
dirty: true,
|
|
|
|
dependents_map: Default::default(),
|
|
|
|
open_docs: HashMap::default(),
|
|
|
|
file_system_docs: Default::default(),
|
2023-01-28 10:18:32 -05:00
|
|
|
resolver_config_hash: 0,
|
2021-11-18 13:50:24 -05:00
|
|
|
imports: Default::default(),
|
2023-10-25 14:39:00 -04:00
|
|
|
resolver: Arc::new(CliGraphResolver::new(CliGraphResolverOptions {
|
|
|
|
fs: Arc::new(RealFs),
|
|
|
|
node_resolver: None,
|
|
|
|
npm_resolver: None,
|
|
|
|
cjs_resolutions: None,
|
|
|
|
package_json_deps_provider: Arc::new(PackageJsonDepsProvider::default()),
|
|
|
|
maybe_jsx_import_source_config: None,
|
|
|
|
maybe_import_map: None,
|
|
|
|
maybe_vendor_dir: None,
|
|
|
|
bare_node_builtins_enabled: false,
|
2023-12-06 19:03:18 -05:00
|
|
|
sloppy_imports_resolver: None,
|
2023-10-25 14:39:00 -04:00
|
|
|
})),
|
2023-02-23 10:58:10 -05:00
|
|
|
npm_specifier_reqs: Default::default(),
|
2023-01-24 09:05:54 -05:00
|
|
|
has_injected_types_node_package: false,
|
2023-12-06 19:03:18 -05:00
|
|
|
redirect_resolver: Arc::new(RedirectResolver::new(cache)),
|
|
|
|
unstable_sloppy_imports: false,
|
2024-01-11 12:07:44 -05:00
|
|
|
project_version: 0,
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 11:43:45 -05:00
|
|
|
pub fn module_graph_imports(&self) -> impl Iterator<Item = &ModuleSpecifier> {
|
|
|
|
self
|
|
|
|
.imports
|
|
|
|
.values()
|
|
|
|
.flat_map(|i| i.dependencies.values())
|
|
|
|
.flat_map(|value| value.get_type().or_else(|| value.get_code()))
|
|
|
|
}
|
|
|
|
|
2024-01-11 12:07:44 -05:00
|
|
|
pub fn project_version(&self) -> String {
|
|
|
|
self.project_version.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn increment_project_version(&mut self) {
|
|
|
|
self.project_version += 1;
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// "Open" a document from the perspective of the editor, meaning that
|
|
|
|
/// requests for information from the document will come from the in-memory
|
|
|
|
/// representation received from the language server client, versus reading
|
|
|
|
/// information from the disk.
|
|
|
|
pub fn open(
|
|
|
|
&mut self,
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
version: i32,
|
|
|
|
language_id: LanguageId,
|
2022-05-20 16:40:55 -04:00
|
|
|
content: Arc<str>,
|
2021-11-18 13:50:24 -05:00
|
|
|
) -> Document {
|
2023-02-15 11:30:54 -05:00
|
|
|
let resolver = self.get_resolver();
|
2023-10-20 00:02:08 -04:00
|
|
|
let npm_resolver = self.get_npm_resolver();
|
2021-11-18 13:50:24 -05:00
|
|
|
let document = Document::open(
|
|
|
|
specifier.clone(),
|
|
|
|
version,
|
|
|
|
language_id,
|
|
|
|
content,
|
2023-10-12 10:37:56 -04:00
|
|
|
&self.cache,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver,
|
2021-11-18 13:50:24 -05:00
|
|
|
);
|
2024-01-11 12:07:44 -05:00
|
|
|
{
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
file_system_docs.docs.remove(&specifier);
|
|
|
|
file_system_docs.dirty = true;
|
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
self.open_docs.insert(specifier, document.clone());
|
2024-01-11 12:07:44 -05:00
|
|
|
self.increment_project_version();
|
2021-11-18 13:50:24 -05:00
|
|
|
self.dirty = true;
|
|
|
|
document
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply language server content changes to an open document.
|
|
|
|
pub fn change(
|
2021-01-22 05:03:16 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
version: i32,
|
2021-10-28 19:56:01 -04:00
|
|
|
changes: Vec<lsp::TextDocumentContentChangeEvent>,
|
2021-11-12 11:42:04 -05:00
|
|
|
) -> Result<Document, AnyError> {
|
2021-11-18 13:50:24 -05:00
|
|
|
let doc = self
|
|
|
|
.open_docs
|
|
|
|
.get(specifier)
|
|
|
|
.cloned()
|
|
|
|
.or_else(|| {
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
file_system_docs.docs.remove(specifier)
|
|
|
|
})
|
2023-03-15 17:46:36 -04:00
|
|
|
.map(Ok)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
Err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!("The specifier \"{specifier}\" was not found."),
|
|
|
|
))
|
|
|
|
})?;
|
2021-10-28 19:56:01 -04:00
|
|
|
self.dirty = true;
|
2023-10-20 00:02:08 -04:00
|
|
|
let doc = doc.with_change(
|
|
|
|
version,
|
|
|
|
changes,
|
|
|
|
self.get_resolver(),
|
|
|
|
self.get_npm_resolver(),
|
|
|
|
)?;
|
2021-11-18 13:50:24 -05:00
|
|
|
self.open_docs.insert(doc.specifier().clone(), doc.clone());
|
2024-01-11 12:07:44 -05:00
|
|
|
self.increment_project_version();
|
2021-11-12 11:42:04 -05:00
|
|
|
Ok(doc)
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
|
2023-10-12 10:37:56 -04:00
|
|
|
pub fn save(&mut self, specifier: &ModuleSpecifier) {
|
|
|
|
let doc = self.open_docs.get(specifier).cloned().or_else(|| {
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
file_system_docs.docs.remove(specifier)
|
|
|
|
});
|
|
|
|
let Some(doc) = doc else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
self.dirty = true;
|
|
|
|
let doc = doc.saved(&self.cache);
|
|
|
|
self.open_docs.insert(doc.specifier().clone(), doc.clone());
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Close an open document, this essentially clears any editor state that is
|
|
|
|
/// being held, and the document store will revert to the file system if
|
|
|
|
/// information about the document is required.
|
|
|
|
pub fn close(&mut self, specifier: &ModuleSpecifier) -> Result<(), AnyError> {
|
2023-10-12 10:37:56 -04:00
|
|
|
if let Some(document) = self.open_docs.remove(specifier) {
|
2024-01-11 12:07:44 -05:00
|
|
|
{
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
file_system_docs.docs.insert(specifier.clone(), document);
|
|
|
|
}
|
|
|
|
self.increment_project_version();
|
2023-10-12 10:37:56 -04:00
|
|
|
self.dirty = true;
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2021-09-07 10:39:32 -04:00
|
|
|
Ok(())
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return `true` if the provided specifier can be resolved to a document,
|
|
|
|
/// otherwise `false`.
|
|
|
|
pub fn contains_import(
|
|
|
|
&self,
|
2021-10-28 19:56:01 -04:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
) -> bool {
|
2023-10-24 09:37:02 -04:00
|
|
|
let maybe_specifier = self
|
|
|
|
.get_resolver()
|
2023-12-06 19:03:18 -05:00
|
|
|
.resolve(
|
|
|
|
specifier,
|
|
|
|
&deno_graph::Range {
|
|
|
|
specifier: referrer.clone(),
|
|
|
|
start: deno_graph::Position::zeroed(),
|
|
|
|
end: deno_graph::Position::zeroed(),
|
|
|
|
},
|
|
|
|
ResolutionMode::Types,
|
|
|
|
)
|
2023-10-24 09:37:02 -04:00
|
|
|
.ok();
|
2021-10-28 19:56:01 -04:00
|
|
|
if let Some(import_specifier) = maybe_specifier {
|
2022-01-25 09:21:59 -05:00
|
|
|
self.exists(&import_specifier)
|
2021-10-28 19:56:01 -04:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2023-12-06 19:03:18 -05:00
|
|
|
pub fn resolve_specifier(
|
2023-08-02 16:57:25 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
2023-12-06 19:03:18 -05:00
|
|
|
if self.unstable_sloppy_imports && specifier.scheme() == "file" {
|
|
|
|
Some(
|
|
|
|
self
|
|
|
|
.resolve_unstable_sloppy_import(specifier)
|
2023-12-08 09:57:06 -05:00
|
|
|
.into_specifier()
|
|
|
|
.into_owned(),
|
2023-12-06 19:03:18 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
self.redirect_resolver.resolve(specifier)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_unstable_sloppy_import<'a>(
|
|
|
|
&self,
|
|
|
|
specifier: &'a ModuleSpecifier,
|
2023-12-07 15:59:13 -05:00
|
|
|
) -> SloppyImportsResolution<'a> {
|
|
|
|
SloppyImportsResolver::resolve_with_stat_sync(specifier, |path| {
|
2023-12-06 19:03:18 -05:00
|
|
|
if let Ok(specifier) = ModuleSpecifier::from_file_path(path) {
|
|
|
|
if self.open_docs.contains_key(&specifier)
|
|
|
|
|| self.cache.contains(&specifier)
|
|
|
|
{
|
2023-12-07 15:59:13 -05:00
|
|
|
return Some(SloppyImportsFsEntry::File);
|
2023-12-06 19:03:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
path.metadata().ok().and_then(|m| {
|
|
|
|
if m.is_file() {
|
2023-12-07 15:59:13 -05:00
|
|
|
Some(SloppyImportsFsEntry::File)
|
2023-12-06 19:03:18 -05:00
|
|
|
} else if m.is_dir() {
|
2023-12-07 15:59:13 -05:00
|
|
|
Some(SloppyImportsFsEntry::Dir)
|
2023-12-06 19:03:18 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2023-08-02 16:57:25 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return `true` if the specifier can be resolved to a document.
|
2022-01-25 09:21:59 -05:00
|
|
|
pub fn exists(&self, specifier: &ModuleSpecifier) -> bool {
|
2023-12-06 19:03:18 -05:00
|
|
|
let specifier = self.resolve_specifier(specifier);
|
2022-01-25 09:21:59 -05:00
|
|
|
if let Some(specifier) = specifier {
|
|
|
|
if self.open_docs.contains_key(&specifier) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-01 18:52:30 -04:00
|
|
|
if specifier.scheme() == "data" {
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-08 16:06:45 -04:00
|
|
|
if specifier.scheme() == "file" {
|
|
|
|
return specifier_to_file_path(&specifier)
|
|
|
|
.map(|p| p.is_file())
|
|
|
|
.unwrap_or(false);
|
|
|
|
}
|
|
|
|
if self.cache.contains(&specifier) {
|
|
|
|
return true;
|
2022-01-25 09:21:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return an array of specifiers, if any, that are dependent upon the
|
|
|
|
/// supplied specifier. This is used to determine invalidation of diagnostics
|
|
|
|
/// when a module has been changed.
|
|
|
|
pub fn dependents(
|
2021-10-28 19:56:01 -04:00
|
|
|
&mut self,
|
2021-06-03 07:13:53 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Vec<ModuleSpecifier> {
|
2021-11-18 13:50:24 -05:00
|
|
|
self.calculate_dependents_if_dirty();
|
2021-06-03 07:13:53 -04:00
|
|
|
let mut dependents = HashSet::new();
|
2023-12-06 19:03:18 -05:00
|
|
|
if let Some(specifier) = self.resolve_specifier(specifier) {
|
2021-10-28 19:56:01 -04:00
|
|
|
recurse_dependents(&specifier, &self.dependents_map, &mut dependents);
|
|
|
|
dependents.into_iter().collect()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
2021-06-03 07:13:53 -04:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
/// Returns a collection of npm package requirements.
|
2023-08-21 05:53:52 -04:00
|
|
|
pub fn npm_package_reqs(&mut self) -> Arc<Vec<PackageReq>> {
|
2022-10-21 11:20:18 -04:00
|
|
|
self.calculate_dependents_if_dirty();
|
2023-03-30 10:43:16 -04:00
|
|
|
self.npm_specifier_reqs.clone()
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
2023-01-24 09:05:54 -05:00
|
|
|
/// Returns if a @types/node package was injected into the npm
|
|
|
|
/// resolver based on the state of the documents.
|
|
|
|
pub fn has_injected_types_node_package(&self) -> bool {
|
|
|
|
self.has_injected_types_node_package
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return a document for the specifier.
|
2022-02-01 21:04:26 -05:00
|
|
|
pub fn get(&self, original_specifier: &ModuleSpecifier) -> Option<Document> {
|
2023-12-06 19:03:18 -05:00
|
|
|
let specifier = self.resolve_specifier(original_specifier)?;
|
2021-11-18 13:50:24 -05:00
|
|
|
if let Some(document) = self.open_docs.get(&specifier) {
|
|
|
|
Some(document.clone())
|
2021-10-28 19:56:01 -04:00
|
|
|
} else {
|
2021-11-18 13:50:24 -05:00
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
2023-10-20 00:02:08 -04:00
|
|
|
file_system_docs.get(
|
|
|
|
&self.cache,
|
|
|
|
self.get_resolver(),
|
|
|
|
&specifier,
|
|
|
|
self.get_npm_resolver(),
|
|
|
|
)
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2023-03-29 16:25:48 -04:00
|
|
|
/// Return a collection of documents that are contained in the document store
|
|
|
|
/// based on the provided filter.
|
|
|
|
pub fn documents(&self, filter: DocumentsFilter) -> Vec<Document> {
|
|
|
|
match filter {
|
|
|
|
DocumentsFilter::OpenDiagnosable => self
|
2021-11-18 13:50:24 -05:00
|
|
|
.open_docs
|
|
|
|
.values()
|
|
|
|
.filter_map(|doc| {
|
2023-03-29 16:25:48 -04:00
|
|
|
if doc.is_diagnosable() {
|
2021-11-18 13:50:24 -05:00
|
|
|
Some(doc.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2023-03-29 16:25:48 -04:00
|
|
|
.collect(),
|
|
|
|
DocumentsFilter::AllDiagnosable | DocumentsFilter::All => {
|
|
|
|
let diagnosable_only =
|
|
|
|
matches!(filter, DocumentsFilter::AllDiagnosable);
|
|
|
|
// it is technically possible for a Document to end up in both the open
|
|
|
|
// and closed documents so we need to ensure we don't return duplicates
|
|
|
|
let mut seen_documents = HashSet::new();
|
|
|
|
let file_system_docs = self.file_system_docs.lock();
|
|
|
|
self
|
|
|
|
.open_docs
|
|
|
|
.values()
|
|
|
|
.chain(file_system_docs.docs.values())
|
|
|
|
.filter_map(|doc| {
|
|
|
|
// this prefers the open documents
|
|
|
|
if seen_documents.insert(doc.specifier().clone())
|
|
|
|
&& (!diagnosable_only || doc.is_diagnosable())
|
|
|
|
{
|
|
|
|
Some(doc.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// For a given set of string specifiers, resolve each one from the graph,
|
|
|
|
/// for a given referrer. This is used to provide resolution information to
|
|
|
|
/// tsc when type checking.
|
|
|
|
pub fn resolve(
|
|
|
|
&self,
|
2023-01-24 09:05:54 -05:00
|
|
|
specifiers: Vec<String>,
|
|
|
|
referrer_doc: &AssetOrDocument,
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm: Option<&StateNpmSnapshot>,
|
2023-01-24 09:05:54 -05:00
|
|
|
) -> Vec<Option<(ModuleSpecifier, MediaType)>> {
|
|
|
|
let referrer = referrer_doc.specifier();
|
|
|
|
let dependencies = match referrer_doc {
|
|
|
|
AssetOrDocument::Asset(_) => None,
|
|
|
|
AssetOrDocument::Document(doc) => Some(doc.0.dependencies.clone()),
|
|
|
|
};
|
2021-10-28 19:56:01 -04:00
|
|
|
let mut results = Vec::new();
|
2021-11-16 17:23:25 -05:00
|
|
|
for specifier in specifiers {
|
2023-09-28 16:43:45 -04:00
|
|
|
if let Some(npm) = maybe_npm {
|
|
|
|
if npm.node_resolver.in_npm_package(referrer) {
|
2022-10-21 11:20:18 -04:00
|
|
|
// we're in an npm package, so use node resolution
|
|
|
|
results.push(Some(NodeResolution::into_specifier_and_media_type(
|
2023-09-28 16:43:45 -04:00
|
|
|
npm
|
|
|
|
.node_resolver
|
2023-04-24 19:44:35 -04:00
|
|
|
.resolve(
|
2023-04-17 15:36:23 -04:00
|
|
|
&specifier,
|
|
|
|
referrer,
|
|
|
|
NodeResolutionMode::Types,
|
2023-04-24 21:07:48 -04:00
|
|
|
&PermissionsContainer::allow_all(),
|
2023-04-17 15:36:23 -04:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.flatten(),
|
2022-10-21 11:20:18 -04:00
|
|
|
)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 17:23:25 -05:00
|
|
|
if specifier.starts_with("asset:") {
|
2023-01-24 09:05:54 -05:00
|
|
|
if let Ok(specifier) = ModuleSpecifier::parse(&specifier) {
|
2023-03-21 11:46:40 -04:00
|
|
|
let media_type = MediaType::from_specifier(&specifier);
|
2021-11-16 17:23:25 -05:00
|
|
|
results.push(Some((specifier, media_type)));
|
|
|
|
} else {
|
|
|
|
results.push(None);
|
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
} else if let Some(dep) =
|
|
|
|
dependencies.as_ref().and_then(|d| d.deps.get(&specifier))
|
|
|
|
{
|
2023-02-09 22:00:23 -05:00
|
|
|
if let Some(specifier) = dep.maybe_type.maybe_specifier() {
|
2023-10-03 19:05:06 -04:00
|
|
|
results.push(self.resolve_dependency(specifier, maybe_npm, referrer));
|
2023-02-09 22:00:23 -05:00
|
|
|
} else if let Some(specifier) = dep.maybe_code.maybe_specifier() {
|
2023-10-03 19:05:06 -04:00
|
|
|
results.push(self.resolve_dependency(specifier, maybe_npm, referrer));
|
2021-01-22 05:03:16 -05:00
|
|
|
} else {
|
2021-10-28 19:56:01 -04:00
|
|
|
results.push(None);
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
2023-02-09 22:00:23 -05:00
|
|
|
} else if let Some(specifier) = self
|
|
|
|
.resolve_imports_dependency(&specifier)
|
|
|
|
.and_then(|r| r.maybe_specifier())
|
2021-11-16 17:23:25 -05:00
|
|
|
{
|
2023-10-03 19:05:06 -04:00
|
|
|
results.push(self.resolve_dependency(specifier, maybe_npm, referrer));
|
2023-02-22 14:15:25 -05:00
|
|
|
} else if let Ok(npm_req_ref) =
|
|
|
|
NpmPackageReqReference::from_str(&specifier)
|
|
|
|
{
|
2023-10-03 19:05:06 -04:00
|
|
|
results.push(node_resolve_npm_req_ref(
|
|
|
|
npm_req_ref,
|
|
|
|
maybe_npm,
|
|
|
|
referrer,
|
|
|
|
));
|
2021-11-16 17:23:25 -05:00
|
|
|
} else {
|
|
|
|
results.push(None);
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
results
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Update the location of the on disk cache for the document store.
|
2023-08-01 20:49:09 -04:00
|
|
|
pub fn set_cache(&mut self, cache: Arc<dyn HttpCache>) {
|
2021-10-28 19:56:01 -04:00
|
|
|
// TODO update resolved dependencies?
|
2023-06-10 11:09:45 -04:00
|
|
|
self.cache = cache.clone();
|
2023-12-06 19:03:18 -05:00
|
|
|
self.redirect_resolver = Arc::new(RedirectResolver::new(cache));
|
2021-10-28 19:56:01 -04:00
|
|
|
self.dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Tries to cache a navigation tree that is associated with the provided specifier
|
|
|
|
/// if the document stored has the same script version.
|
|
|
|
pub fn try_cache_navigation_tree(
|
2022-12-19 20:22:17 -05:00
|
|
|
&self,
|
2021-06-04 17:31:44 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2021-11-18 13:50:24 -05:00
|
|
|
script_version: &str,
|
2021-10-28 19:56:01 -04:00
|
|
|
navigation_tree: Arc<tsc::NavigationTree>,
|
2021-06-04 17:31:44 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-19 20:22:17 -05:00
|
|
|
if let Some(doc) = self.open_docs.get(specifier) {
|
|
|
|
doc.update_navigation_tree_if_version(navigation_tree, script_version)
|
2021-11-18 13:50:24 -05:00
|
|
|
} else {
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
if let Some(doc) = file_system_docs.docs.get_mut(specifier) {
|
2022-12-19 20:22:17 -05:00
|
|
|
doc.update_navigation_tree_if_version(navigation_tree, script_version);
|
2021-11-18 13:50:24 -05:00
|
|
|
} else {
|
|
|
|
return Err(custom_error(
|
|
|
|
"NotFound",
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("Specifier not found {specifier}"),
|
2021-11-18 13:50:24 -05:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
pub fn update_config(&mut self, options: UpdateDocumentConfigOptions) {
|
2024-01-15 19:15:39 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-01-28 10:18:32 -05:00
|
|
|
fn calculate_resolver_config_hash(
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: &PathOrPatternSet,
|
|
|
|
disabled_paths: &PathOrPatternSet,
|
2023-05-11 21:53:09 -04:00
|
|
|
document_preload_limit: usize,
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_import_map: Option<&import_map::ImportMap>,
|
|
|
|
maybe_jsx_config: Option<&JsxImportSourceConfig>,
|
2023-08-06 21:56:56 -04:00
|
|
|
maybe_vendor_dir: Option<bool>,
|
2023-03-03 17:27:05 -05:00
|
|
|
maybe_package_json_deps: Option<&PackageJsonDeps>,
|
2023-12-06 19:03:18 -05:00
|
|
|
maybe_unstable_flags: Option<&Vec<String>>,
|
2023-01-28 10:18:32 -05:00
|
|
|
) -> u64 {
|
2024-01-15 19:15:39 -05:00
|
|
|
fn get_pattern_set_vec(set: &PathOrPatternSet) -> Vec<Cow<'_, str>> {
|
|
|
|
let mut paths = set
|
|
|
|
.inner()
|
|
|
|
.iter()
|
|
|
|
.map(|p| match p {
|
|
|
|
PathOrPattern::Path(p) => {
|
|
|
|
Cow::Owned(p.to_string_lossy().to_string())
|
|
|
|
}
|
|
|
|
PathOrPattern::RemoteUrl(p) => Cow::Borrowed(p.as_str()),
|
|
|
|
PathOrPattern::Pattern(p) => Cow::Borrowed(p.as_str()),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
// ensure these are sorted so the hashing is deterministic
|
|
|
|
paths.sort_unstable();
|
|
|
|
paths
|
|
|
|
}
|
|
|
|
|
2023-01-28 10:18:32 -05:00
|
|
|
let mut hasher = FastInsecureHasher::default();
|
2023-07-10 17:45:09 -04:00
|
|
|
hasher.write_hashable(document_preload_limit);
|
2024-01-15 19:15:39 -05:00
|
|
|
hasher.write_hashable(&get_pattern_set_vec(enabled_paths));
|
|
|
|
hasher.write_hashable(&get_pattern_set_vec(disabled_paths));
|
2023-01-28 10:18:32 -05:00
|
|
|
if let Some(import_map) = maybe_import_map {
|
|
|
|
hasher.write_str(&import_map.to_json());
|
|
|
|
hasher.write_str(import_map.base_url().as_str());
|
|
|
|
}
|
2023-08-06 21:56:56 -04:00
|
|
|
hasher.write_hashable(maybe_vendor_dir);
|
2023-07-10 17:45:09 -04:00
|
|
|
hasher.write_hashable(maybe_jsx_config);
|
2023-12-06 19:03:18 -05:00
|
|
|
hasher.write_hashable(maybe_unstable_flags);
|
2023-04-06 18:46:44 -04:00
|
|
|
if let Some(package_json_deps) = &maybe_package_json_deps {
|
|
|
|
// We need to ensure the hashing is deterministic so explicitly type
|
|
|
|
// this in order to catch if the type of package_json_deps ever changes
|
2023-10-25 14:39:00 -04:00
|
|
|
// from a deterministic IndexMap to something else.
|
2023-10-04 23:05:12 -04:00
|
|
|
let package_json_deps: &IndexMap<_, _> = *package_json_deps;
|
2023-04-06 18:46:44 -04:00
|
|
|
for (key, value) in package_json_deps {
|
|
|
|
hasher.write_hashable(key);
|
|
|
|
match value {
|
|
|
|
Ok(value) => {
|
|
|
|
hasher.write_hashable(value);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
hasher.write_str(&err.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 18:52:31 -04:00
|
|
|
|
2023-01-28 10:18:32 -05:00
|
|
|
hasher.finish()
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
let maybe_package_json_deps =
|
|
|
|
options.maybe_package_json.map(|package_json| {
|
|
|
|
package_json::get_local_package_json_version_reqs(package_json)
|
|
|
|
});
|
|
|
|
let maybe_jsx_config = options
|
|
|
|
.maybe_config_file
|
2023-07-27 12:15:39 -04:00
|
|
|
.and_then(|cf| cf.to_maybe_jsx_import_source_config().ok().flatten());
|
2023-01-28 10:18:32 -05:00
|
|
|
let new_resolver_config_hash = calculate_resolver_config_hash(
|
2023-09-18 15:58:16 -04:00
|
|
|
&options.enabled_paths,
|
2024-01-15 19:15:39 -05:00
|
|
|
&options.disabled_paths,
|
2023-05-11 21:53:09 -04:00
|
|
|
options.document_preload_limit,
|
2023-05-11 17:17:14 -04:00
|
|
|
options.maybe_import_map.as_deref(),
|
2023-01-28 10:18:32 -05:00
|
|
|
maybe_jsx_config.as_ref(),
|
2023-08-06 21:56:56 -04:00
|
|
|
options.maybe_config_file.and_then(|c| c.vendor_dir_flag()),
|
2023-02-23 10:58:10 -05:00
|
|
|
maybe_package_json_deps.as_ref(),
|
2023-12-06 19:03:18 -05:00
|
|
|
options.maybe_config_file.map(|c| &c.json.unstable),
|
2023-01-28 10:18:32 -05:00
|
|
|
);
|
2023-05-10 20:06:59 -04:00
|
|
|
let deps_provider =
|
|
|
|
Arc::new(PackageJsonDepsProvider::new(maybe_package_json_deps));
|
2023-12-06 19:03:18 -05:00
|
|
|
let fs = Arc::new(RealFs);
|
2023-10-25 14:39:00 -04:00
|
|
|
self.resolver = Arc::new(CliGraphResolver::new(CliGraphResolverOptions {
|
2023-12-06 19:03:18 -05:00
|
|
|
fs: fs.clone(),
|
2023-10-25 14:39:00 -04:00
|
|
|
node_resolver: options.node_resolver,
|
|
|
|
npm_resolver: options.npm_resolver,
|
|
|
|
cjs_resolutions: None, // only used for runtime
|
|
|
|
package_json_deps_provider: deps_provider,
|
|
|
|
maybe_jsx_import_source_config: maybe_jsx_config,
|
|
|
|
maybe_import_map: options.maybe_import_map,
|
|
|
|
maybe_vendor_dir: options
|
|
|
|
.maybe_config_file
|
|
|
|
.and_then(|c| c.vendor_dir_path())
|
|
|
|
.as_ref(),
|
|
|
|
bare_node_builtins_enabled: options
|
|
|
|
.maybe_config_file
|
2023-12-06 19:03:18 -05:00
|
|
|
.map(|config| config.has_unstable("bare-node-builtins"))
|
2023-10-25 14:39:00 -04:00
|
|
|
.unwrap_or(false),
|
2023-12-06 19:03:18 -05:00
|
|
|
// Don't set this for the LSP because instead we'll use the OpenDocumentsLoader
|
|
|
|
// because it's much easier and we get diagnostics/quick fixes about a redirected
|
|
|
|
// specifier for free.
|
|
|
|
sloppy_imports_resolver: None,
|
2023-10-25 14:39:00 -04:00
|
|
|
}));
|
2023-12-06 19:03:18 -05:00
|
|
|
self.redirect_resolver =
|
|
|
|
Arc::new(RedirectResolver::new(self.cache.clone()));
|
2021-11-18 13:50:24 -05:00
|
|
|
self.imports = Arc::new(
|
2023-02-09 22:00:23 -05:00
|
|
|
if let Some(Ok(imports)) =
|
2023-05-11 17:17:14 -04:00
|
|
|
options.maybe_config_file.map(|cf| cf.to_maybe_imports())
|
2021-11-18 13:50:24 -05:00
|
|
|
{
|
|
|
|
imports
|
|
|
|
.into_iter()
|
2023-08-24 05:21:34 -04:00
|
|
|
.map(|(referrer, imports)| {
|
2023-10-20 00:02:08 -04:00
|
|
|
let graph_import = GraphImport::new(
|
|
|
|
&referrer,
|
|
|
|
imports,
|
|
|
|
Some(self.get_resolver()),
|
|
|
|
Some(self.get_npm_resolver()),
|
|
|
|
);
|
2023-08-24 05:21:34 -04:00
|
|
|
(referrer, graph_import)
|
2021-11-18 13:50:24 -05:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
2023-03-11 11:43:45 -05:00
|
|
|
IndexMap::new()
|
2021-11-18 13:50:24 -05:00
|
|
|
},
|
|
|
|
);
|
2023-12-06 19:03:18 -05:00
|
|
|
self.unstable_sloppy_imports = options
|
|
|
|
.maybe_config_file
|
|
|
|
.map(|c| c.has_unstable("sloppy-imports"))
|
|
|
|
.unwrap_or(false);
|
2023-01-28 10:18:32 -05:00
|
|
|
|
|
|
|
// only refresh the dependencies if the underlying configuration has changed
|
|
|
|
if self.resolver_config_hash != new_resolver_config_hash {
|
2023-05-11 17:17:14 -04:00
|
|
|
self.refresh_dependencies(
|
2023-09-18 15:58:16 -04:00
|
|
|
options.enabled_paths,
|
|
|
|
options.disabled_paths,
|
2023-05-11 17:17:14 -04:00
|
|
|
options.document_preload_limit,
|
|
|
|
);
|
2023-01-28 10:18:32 -05:00
|
|
|
self.resolver_config_hash = new_resolver_config_hash;
|
|
|
|
|
2024-01-11 12:07:44 -05:00
|
|
|
self.increment_project_version();
|
2023-07-26 18:52:31 -04:00
|
|
|
self.dirty = true;
|
|
|
|
self.calculate_dependents_if_dirty();
|
|
|
|
}
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
fn refresh_dependencies(
|
|
|
|
&mut self,
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: PathOrPatternSet,
|
|
|
|
disabled_paths: PathOrPatternSet,
|
2023-05-11 17:17:14 -04:00
|
|
|
document_preload_limit: usize,
|
|
|
|
) {
|
2023-02-15 11:30:54 -05:00
|
|
|
let resolver = self.resolver.as_graph_resolver();
|
2023-10-20 00:02:08 -04:00
|
|
|
let npm_resolver = self.resolver.as_graph_npm_resolver();
|
2023-01-28 10:18:32 -05:00
|
|
|
for doc in self.open_docs.values_mut() {
|
2023-10-20 00:02:08 -04:00
|
|
|
if let Some(new_doc) = doc.maybe_with_new_resolver(resolver, npm_resolver)
|
|
|
|
{
|
2023-01-28 10:18:32 -05:00
|
|
|
*doc = new_doc;
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 17:47:53 -04:00
|
|
|
|
|
|
|
// update the file system documents
|
|
|
|
let mut fs_docs = self.file_system_docs.lock();
|
2023-05-11 17:17:14 -04:00
|
|
|
if document_preload_limit > 0 {
|
|
|
|
let mut not_found_docs =
|
|
|
|
fs_docs.docs.keys().cloned().collect::<HashSet<_>>();
|
|
|
|
let open_docs = &mut self.open_docs;
|
|
|
|
|
|
|
|
log::debug!("Preloading documents from enabled urls...");
|
2023-06-13 15:48:53 -04:00
|
|
|
let mut finder =
|
|
|
|
PreloadDocumentFinder::new(PreloadDocumentFinderOptions {
|
|
|
|
enabled_paths,
|
|
|
|
disabled_paths,
|
|
|
|
limit: document_preload_limit,
|
|
|
|
});
|
2023-05-11 17:17:14 -04:00
|
|
|
for specifier in finder.by_ref() {
|
|
|
|
// mark this document as having been found
|
|
|
|
not_found_docs.remove(&specifier);
|
2023-04-01 12:02:44 -04:00
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
if !open_docs.contains_key(&specifier)
|
|
|
|
&& !fs_docs.docs.contains_key(&specifier)
|
|
|
|
{
|
2023-10-20 00:02:08 -04:00
|
|
|
fs_docs.refresh_document(
|
|
|
|
&self.cache,
|
|
|
|
resolver,
|
|
|
|
&specifier,
|
|
|
|
npm_resolver,
|
|
|
|
);
|
2023-05-11 17:17:14 -04:00
|
|
|
} else {
|
|
|
|
// update the existing entry to have the new resolver
|
|
|
|
if let Some(doc) = fs_docs.docs.get_mut(&specifier) {
|
2023-10-20 00:02:08 -04:00
|
|
|
if let Some(new_doc) =
|
|
|
|
doc.maybe_with_new_resolver(resolver, npm_resolver)
|
|
|
|
{
|
2023-05-11 17:17:14 -04:00
|
|
|
*doc = new_doc;
|
2023-04-01 12:02:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-11 17:17:14 -04:00
|
|
|
}
|
2023-03-30 17:47:53 -04:00
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
if finder.hit_limit() {
|
|
|
|
lsp_warn!(
|
|
|
|
concat!(
|
|
|
|
"Hit the language server document preload limit of {} file system entries. ",
|
|
|
|
"You may want to use the \"deno.enablePaths\" configuration setting to only have Deno ",
|
|
|
|
"partially enable a workspace or increase the limit via \"deno.documentPreloadLimit\". ",
|
|
|
|
"In cases where Deno ends up using too much memory, you may want to lower the limit."
|
|
|
|
),
|
|
|
|
document_preload_limit,
|
|
|
|
);
|
|
|
|
|
|
|
|
// since we hit the limit, just update everything to use the new resolver
|
|
|
|
for uri in not_found_docs {
|
|
|
|
if let Some(doc) = fs_docs.docs.get_mut(&uri) {
|
2023-10-20 00:02:08 -04:00
|
|
|
if let Some(new_doc) =
|
|
|
|
doc.maybe_with_new_resolver(resolver, npm_resolver)
|
|
|
|
{
|
2023-05-11 17:17:14 -04:00
|
|
|
*doc = new_doc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-01 12:02:44 -04:00
|
|
|
// clean up and remove any documents that weren't found
|
|
|
|
for uri in not_found_docs {
|
|
|
|
fs_docs.docs.remove(&uri);
|
|
|
|
}
|
|
|
|
}
|
2023-05-11 17:17:14 -04:00
|
|
|
} else {
|
|
|
|
// This log statement is used in the tests to ensure preloading doesn't
|
|
|
|
// happen, which is not useful in the repl and could be very expensive
|
|
|
|
// if the repl is launched from a directory with a lot of descendants.
|
|
|
|
log::debug!("Skipping document preload.");
|
|
|
|
|
|
|
|
// just update to use the new resolver
|
|
|
|
for doc in fs_docs.docs.values_mut() {
|
2023-10-20 00:02:08 -04:00
|
|
|
if let Some(new_doc) =
|
|
|
|
doc.maybe_with_new_resolver(resolver, npm_resolver)
|
|
|
|
{
|
2023-05-11 17:17:14 -04:00
|
|
|
*doc = new_doc;
|
2023-03-30 17:47:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-11 17:17:14 -04:00
|
|
|
|
2023-03-30 17:47:53 -04:00
|
|
|
fs_docs.dirty = true;
|
2023-01-28 10:18:32 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Iterate through the documents, building a map where the key is a unique
|
|
|
|
/// document and the value is a set of specifiers that depend on that
|
|
|
|
/// document.
|
|
|
|
fn calculate_dependents_if_dirty(&mut self) {
|
2022-11-29 19:32:18 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct DocAnalyzer {
|
|
|
|
dependents_map: HashMap<ModuleSpecifier, HashSet<ModuleSpecifier>>,
|
|
|
|
analyzed_specifiers: HashSet<ModuleSpecifier>,
|
|
|
|
pending_specifiers: VecDeque<ModuleSpecifier>,
|
2023-08-21 05:53:52 -04:00
|
|
|
npm_reqs: HashSet<PackageReq>,
|
2023-01-24 09:05:54 -05:00
|
|
|
has_node_builtin_specifier: bool,
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2022-11-29 19:32:18 -05:00
|
|
|
impl DocAnalyzer {
|
|
|
|
fn add(&mut self, dep: &ModuleSpecifier, specifier: &ModuleSpecifier) {
|
|
|
|
if !self.analyzed_specifiers.contains(dep) {
|
|
|
|
self.analyzed_specifiers.insert(dep.clone());
|
|
|
|
// perf: ensure this is not added to unless this specifier has never
|
|
|
|
// been analyzed in order to not cause an extra file system lookup
|
|
|
|
self.pending_specifiers.push_back(dep.clone());
|
2023-02-21 12:03:48 -05:00
|
|
|
if let Ok(reference) = NpmPackageReqReference::from_specifier(dep) {
|
2023-08-21 05:53:52 -04:00
|
|
|
self.npm_reqs.insert(reference.into_inner().req);
|
2022-11-29 19:32:18 -05:00
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
|
|
|
|
self
|
|
|
|
.dependents_map
|
2022-10-21 11:20:18 -04:00
|
|
|
.entry(dep.clone())
|
|
|
|
.or_default()
|
|
|
|
.insert(specifier.clone());
|
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
|
|
|
|
fn analyze_doc(&mut self, specifier: &ModuleSpecifier, doc: &Document) {
|
|
|
|
self.analyzed_specifiers.insert(specifier.clone());
|
2023-07-26 18:52:31 -04:00
|
|
|
for dependency in doc.dependencies().values() {
|
2022-11-29 19:32:18 -05:00
|
|
|
if let Some(dep) = dependency.get_code() {
|
2023-07-26 18:52:31 -04:00
|
|
|
if !self.has_node_builtin_specifier && dep.scheme() == "node" {
|
|
|
|
self.has_node_builtin_specifier = true;
|
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
self.add(dep, specifier);
|
|
|
|
}
|
|
|
|
if let Some(dep) = dependency.get_type() {
|
|
|
|
self.add(dep, specifier);
|
|
|
|
}
|
|
|
|
}
|
2023-02-09 22:00:23 -05:00
|
|
|
if let Some(dep) = doc.maybe_types_dependency().maybe_specifier() {
|
|
|
|
self.add(dep, specifier);
|
2022-11-29 19:32:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
if !file_system_docs.dirty && !self.dirty {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut doc_analyzer = DocAnalyzer::default();
|
|
|
|
// favor documents that are open in case a document exists in both collections
|
|
|
|
let documents = file_system_docs.docs.iter().chain(self.open_docs.iter());
|
|
|
|
for (specifier, doc) in documents {
|
|
|
|
doc_analyzer.analyze_doc(specifier, doc);
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
let resolver = self.get_resolver();
|
2023-10-20 00:02:08 -04:00
|
|
|
let npm_resolver = self.get_npm_resolver();
|
2022-11-29 19:32:18 -05:00
|
|
|
while let Some(specifier) = doc_analyzer.pending_specifiers.pop_front() {
|
2023-10-12 10:37:56 -04:00
|
|
|
if let Some(doc) = self.open_docs.get(&specifier) {
|
|
|
|
doc_analyzer.analyze_doc(&specifier, doc);
|
|
|
|
} else if let Some(doc) =
|
2023-10-20 00:02:08 -04:00
|
|
|
file_system_docs.get(&self.cache, resolver, &specifier, npm_resolver)
|
2022-11-29 19:32:18 -05:00
|
|
|
{
|
|
|
|
doc_analyzer.analyze_doc(&specifier, &doc);
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
|
2023-01-24 09:05:54 -05:00
|
|
|
let mut npm_reqs = doc_analyzer.npm_reqs;
|
|
|
|
// Ensure a @types/node package exists when any module uses a node: specifier.
|
|
|
|
// Unlike on the command line, here we just add @types/node to the npm package
|
|
|
|
// requirements since this won't end up in the lockfile.
|
|
|
|
self.has_injected_types_node_package = doc_analyzer
|
|
|
|
.has_node_builtin_specifier
|
|
|
|
&& !npm_reqs.iter().any(|r| r.name == "@types/node");
|
|
|
|
if self.has_injected_types_node_package {
|
2023-08-21 05:53:52 -04:00
|
|
|
npm_reqs.insert(PackageReq::from_str("@types/node").unwrap());
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
|
|
|
|
2022-11-29 19:32:18 -05:00
|
|
|
self.dependents_map = Arc::new(doc_analyzer.dependents_map);
|
2023-02-23 10:58:10 -05:00
|
|
|
self.npm_specifier_reqs = Arc::new({
|
|
|
|
let mut reqs = npm_reqs.into_iter().collect::<Vec<_>>();
|
|
|
|
reqs.sort();
|
|
|
|
reqs
|
|
|
|
});
|
2021-11-18 13:50:24 -05:00
|
|
|
self.dirty = false;
|
|
|
|
file_system_docs.dirty = false;
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
fn get_resolver(&self) -> &dyn deno_graph::source::Resolver {
|
|
|
|
self.resolver.as_graph_resolver()
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2023-10-20 00:02:08 -04:00
|
|
|
fn get_npm_resolver(&self) -> &dyn deno_graph::source::NpmResolver {
|
|
|
|
self.resolver.as_graph_npm_resolver()
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
fn resolve_dependency(
|
2021-10-28 19:56:01 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm: Option<&StateNpmSnapshot>,
|
2023-10-03 19:05:06 -04:00
|
|
|
referrer: &ModuleSpecifier,
|
2021-11-18 13:50:24 -05:00
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
2023-07-26 18:52:31 -04:00
|
|
|
if let Some(module_name) = specifier.as_str().strip_prefix("node:") {
|
|
|
|
if deno_node::is_builtin_node_module(module_name) {
|
|
|
|
// return itself for node: specifiers because during type checking
|
|
|
|
// we resolve to the ambient modules in the @types/node package
|
|
|
|
// rather than deno_std/node
|
|
|
|
return Some((specifier.clone(), MediaType::Dts));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
if let Ok(npm_ref) = NpmPackageReqReference::from_specifier(specifier) {
|
2023-10-03 19:05:06 -04:00
|
|
|
return node_resolve_npm_req_ref(npm_ref, maybe_npm, referrer);
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
let doc = self.get(specifier)?;
|
2023-02-22 14:15:25 -05:00
|
|
|
let maybe_module = doc.maybe_esm_module().and_then(|r| r.as_ref().ok());
|
2023-02-09 22:00:23 -05:00
|
|
|
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())
|
|
|
|
{
|
2023-10-03 19:05:06 -04:00
|
|
|
self.resolve_dependency(specifier, maybe_npm, referrer)
|
2021-11-18 13:50:24 -05:00
|
|
|
} else {
|
|
|
|
let media_type = doc.media_type();
|
2023-08-02 16:57:25 -04:00
|
|
|
Some((doc.specifier().clone(), media_type))
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Iterate through any "imported" modules, checking to see if a dependency
|
|
|
|
/// is available. This is used to provide "global" imports like the JSX import
|
|
|
|
/// source.
|
2023-02-09 22:00:23 -05:00
|
|
|
fn resolve_imports_dependency(&self, specifier: &str) -> Option<&Resolution> {
|
2022-08-09 17:27:22 -04:00
|
|
|
for graph_imports in self.imports.values() {
|
|
|
|
let maybe_dep = graph_imports.dependencies.get(specifier);
|
2021-11-18 13:50:24 -05:00
|
|
|
if maybe_dep.is_some() {
|
2022-01-31 17:33:57 -05:00
|
|
|
return maybe_dep.map(|d| &d.maybe_type);
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
fn node_resolve_npm_req_ref(
|
|
|
|
npm_req_ref: NpmPackageReqReference,
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm: Option<&StateNpmSnapshot>,
|
2023-10-03 19:05:06 -04:00
|
|
|
referrer: &ModuleSpecifier,
|
2023-02-22 14:15:25 -05:00
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm.map(|npm| {
|
2023-02-22 14:15:25 -05:00
|
|
|
NodeResolution::into_specifier_and_media_type(
|
2023-09-28 16:43:45 -04:00
|
|
|
npm
|
|
|
|
.npm_resolver
|
2023-10-03 19:05:06 -04:00
|
|
|
.resolve_pkg_folder_from_deno_module_req(npm_req_ref.req(), referrer)
|
2023-02-22 14:15:25 -05:00
|
|
|
.ok()
|
2023-09-28 16:43:45 -04:00
|
|
|
.and_then(|package_folder| {
|
|
|
|
npm
|
|
|
|
.node_resolver
|
2023-10-05 16:18:29 -04:00
|
|
|
.resolve_package_subpath_from_deno_module(
|
2023-09-28 16:43:45 -04:00
|
|
|
&package_folder,
|
|
|
|
npm_req_ref.sub_path(),
|
2023-10-05 16:18:29 -04:00
|
|
|
referrer,
|
2023-09-28 16:43:45 -04:00
|
|
|
NodeResolutionMode::Types,
|
|
|
|
&PermissionsContainer::allow_all(),
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.flatten()
|
|
|
|
}),
|
2023-02-22 14:15:25 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:23:21 -04:00
|
|
|
/// Loader that will look at the open documents.
|
2022-10-21 11:20:18 -04:00
|
|
|
pub struct OpenDocumentsGraphLoader<'a> {
|
2022-10-20 13:23:21 -04:00
|
|
|
pub inner_loader: &'a mut dyn deno_graph::source::Loader,
|
|
|
|
pub open_docs: &'a HashMap<ModuleSpecifier, Document>,
|
2023-12-06 19:03:18 -05:00
|
|
|
pub unstable_sloppy_imports: bool,
|
2022-10-20 13:23:21 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 09:09:16 -04:00
|
|
|
impl<'a> OpenDocumentsGraphLoader<'a> {
|
|
|
|
fn load_from_docs(
|
|
|
|
&self,
|
2022-10-20 13:23:21 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-09-07 09:09:16 -04:00
|
|
|
) -> Option<deno_graph::source::LoadFuture> {
|
2022-10-20 13:23:21 -04:00
|
|
|
if specifier.scheme() == "file" {
|
|
|
|
if let Some(doc) = self.open_docs.get(specifier) {
|
2023-09-07 09:09:16 -04:00
|
|
|
return Some(
|
|
|
|
future::ready(Ok(Some(deno_graph::source::LoadResponse::Module {
|
2022-10-20 13:23:21 -04:00
|
|
|
content: doc.content(),
|
|
|
|
specifier: doc.specifier().clone(),
|
|
|
|
maybe_headers: None,
|
2023-09-07 09:09:16 -04:00
|
|
|
})))
|
|
|
|
.boxed_local(),
|
|
|
|
);
|
2022-10-20 13:23:21 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-07 09:09:16 -04:00
|
|
|
None
|
|
|
|
}
|
2023-12-06 19:03:18 -05:00
|
|
|
|
|
|
|
fn resolve_unstable_sloppy_import<'b>(
|
|
|
|
&self,
|
|
|
|
specifier: &'b ModuleSpecifier,
|
2023-12-07 15:59:13 -05:00
|
|
|
) -> SloppyImportsResolution<'b> {
|
|
|
|
SloppyImportsResolver::resolve_with_stat_sync(specifier, |path| {
|
2023-12-06 19:03:18 -05:00
|
|
|
if let Ok(specifier) = ModuleSpecifier::from_file_path(path) {
|
|
|
|
if self.open_docs.contains_key(&specifier) {
|
2023-12-07 15:59:13 -05:00
|
|
|
return Some(SloppyImportsFsEntry::File);
|
2023-12-06 19:03:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
path.metadata().ok().and_then(|m| {
|
|
|
|
if m.is_file() {
|
2023-12-07 15:59:13 -05:00
|
|
|
Some(SloppyImportsFsEntry::File)
|
2023-12-06 19:03:18 -05:00
|
|
|
} else if m.is_dir() {
|
2023-12-07 15:59:13 -05:00
|
|
|
Some(SloppyImportsFsEntry::Dir)
|
2023-12-06 19:03:18 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2023-09-07 09:09:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> deno_graph::source::Loader for OpenDocumentsGraphLoader<'a> {
|
|
|
|
fn registry_url(&self) -> &Url {
|
|
|
|
self.inner_loader.registry_url()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
is_dynamic: bool,
|
|
|
|
cache_setting: deno_graph::source::CacheSetting,
|
|
|
|
) -> deno_graph::source::LoadFuture {
|
2023-12-06 19:03:18 -05:00
|
|
|
let specifier = if self.unstable_sloppy_imports {
|
|
|
|
self
|
|
|
|
.resolve_unstable_sloppy_import(specifier)
|
|
|
|
.into_specifier()
|
|
|
|
} else {
|
|
|
|
Cow::Borrowed(specifier)
|
|
|
|
};
|
|
|
|
|
|
|
|
match self.load_from_docs(&specifier) {
|
2023-09-07 09:09:16 -04:00
|
|
|
Some(fut) => fut,
|
2023-12-06 19:03:18 -05:00
|
|
|
None => self
|
|
|
|
.inner_loader
|
|
|
|
.load(&specifier, is_dynamic, cache_setting),
|
2023-09-07 09:09:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cache_module_info(
|
|
|
|
&mut self,
|
|
|
|
specifier: &deno_ast::ModuleSpecifier,
|
|
|
|
source: &str,
|
|
|
|
module_info: &deno_graph::ModuleInfo,
|
|
|
|
) {
|
|
|
|
self
|
|
|
|
.inner_loader
|
|
|
|
.cache_module_info(specifier, source, module_info)
|
2022-10-20 13:23:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 10:18:32 -05:00
|
|
|
fn parse_and_analyze_module(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
text_info: SourceTextInfo,
|
|
|
|
maybe_headers: Option<&HashMap<String, String>>,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver: &dyn deno_graph::source::NpmResolver,
|
2023-01-28 10:18:32 -05:00
|
|
|
) -> (Option<ParsedSourceResult>, Option<ModuleResult>) {
|
|
|
|
let parsed_source_result = parse_source(specifier, text_info, maybe_headers);
|
2023-10-20 00:02:08 -04:00
|
|
|
let module_result = analyze_module(
|
|
|
|
specifier,
|
|
|
|
&parsed_source_result,
|
|
|
|
maybe_headers,
|
|
|
|
resolver,
|
|
|
|
npm_resolver,
|
|
|
|
);
|
2023-01-28 10:18:32 -05:00
|
|
|
(Some(parsed_source_result), Some(module_result))
|
|
|
|
}
|
2022-08-22 12:14:59 -04:00
|
|
|
|
2023-01-28 10:18:32 -05:00
|
|
|
fn parse_source(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
text_info: SourceTextInfo,
|
|
|
|
maybe_headers: Option<&HashMap<String, String>>,
|
|
|
|
) -> ParsedSourceResult {
|
|
|
|
deno_ast::parse_module(deno_ast::ParseParams {
|
|
|
|
specifier: specifier.to_string(),
|
|
|
|
text_info,
|
|
|
|
media_type: MediaType::from_specifier_and_headers(specifier, maybe_headers),
|
|
|
|
capture_tokens: true,
|
|
|
|
scope_analysis: true,
|
|
|
|
maybe_syntax: None,
|
|
|
|
})
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
|
|
|
|
2023-01-28 10:18:32 -05:00
|
|
|
fn analyze_module(
|
2022-08-22 12:14:59 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-01-28 10:18:32 -05:00
|
|
|
parsed_source_result: &ParsedSourceResult,
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_headers: Option<&HashMap<String, String>>,
|
2023-02-15 11:30:54 -05:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2023-10-20 00:02:08 -04:00
|
|
|
npm_resolver: &dyn deno_graph::source::NpmResolver,
|
2023-01-28 10:18:32 -05:00
|
|
|
) -> ModuleResult {
|
|
|
|
match parsed_source_result {
|
2023-02-03 14:10:51 -05:00
|
|
|
Ok(parsed_source) => Ok(deno_graph::parse_module_from_ast(
|
2023-12-01 15:12:10 -05:00
|
|
|
deno_graph::ParseModuleFromAstOptions {
|
|
|
|
graph_kind: deno_graph::GraphKind::All,
|
|
|
|
specifier,
|
|
|
|
maybe_headers,
|
|
|
|
parsed_source,
|
|
|
|
// use a null file system because there's no need to bother resolving
|
|
|
|
// dynamic imports like import(`./dir/${something}`) in the LSP
|
|
|
|
file_system: &deno_graph::source::NullFileSystem,
|
|
|
|
maybe_resolver: Some(resolver),
|
|
|
|
maybe_npm_resolver: Some(npm_resolver),
|
|
|
|
},
|
2023-02-03 14:10:51 -05:00
|
|
|
)),
|
2023-03-21 11:46:40 -04:00
|
|
|
Err(err) => Err(deno_graph::ModuleGraphError::ModuleError(
|
|
|
|
deno_graph::ModuleError::ParseErr(specifier.clone(), err.clone()),
|
2022-08-22 12:14:59 -04:00
|
|
|
)),
|
2023-01-28 10:18:32 -05:00
|
|
|
}
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
#[derive(Debug)]
|
2023-04-01 15:10:30 -04:00
|
|
|
enum PendingEntry {
|
|
|
|
/// File specified as a root url.
|
|
|
|
SpecifiedRootFile(PathBuf),
|
|
|
|
/// Directory that is queued to read.
|
2024-01-08 12:18:42 -05:00
|
|
|
Dir(PathBuf, Rc<FilePatterns>),
|
2023-04-01 15:10:30 -04:00
|
|
|
/// The current directory being read.
|
2024-01-08 12:18:42 -05:00
|
|
|
ReadDir(Box<ReadDir>, Rc<FilePatterns>),
|
2023-04-01 15:10:30 -04:00
|
|
|
}
|
|
|
|
|
2023-06-13 15:48:53 -04:00
|
|
|
struct PreloadDocumentFinderOptions {
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: PathOrPatternSet,
|
|
|
|
disabled_paths: PathOrPatternSet,
|
2023-06-13 15:48:53 -04:00
|
|
|
limit: usize,
|
|
|
|
}
|
|
|
|
|
2023-03-30 17:47:53 -04:00
|
|
|
/// Iterator that finds documents that can be preloaded into
|
|
|
|
/// the LSP on startup.
|
|
|
|
struct PreloadDocumentFinder {
|
2023-05-11 17:17:14 -04:00
|
|
|
limit: usize,
|
|
|
|
entry_count: usize,
|
2023-04-01 15:10:30 -04:00
|
|
|
pending_entries: VecDeque<PendingEntry>,
|
2024-01-08 12:18:42 -05:00
|
|
|
root_dir_entries: Vec<PendingEntry>,
|
|
|
|
visited_paths: HashSet<PathBuf>,
|
2023-03-30 17:47:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PreloadDocumentFinder {
|
2023-06-13 15:48:53 -04:00
|
|
|
pub fn new(options: PreloadDocumentFinderOptions) -> Self {
|
2023-04-01 15:10:30 -04:00
|
|
|
fn is_allowed_root_dir(dir_path: &Path) -> bool {
|
|
|
|
if dir_path.parent().is_none() {
|
|
|
|
// never search the root directory of a drive
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-03-30 17:47:53 -04:00
|
|
|
let mut finder = PreloadDocumentFinder {
|
2023-06-13 15:48:53 -04:00
|
|
|
limit: options.limit,
|
2023-04-01 15:10:30 -04:00
|
|
|
entry_count: 0,
|
|
|
|
pending_entries: Default::default(),
|
2024-01-08 12:18:42 -05:00
|
|
|
root_dir_entries: Default::default(),
|
|
|
|
visited_paths: Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let file_patterns = FilePatterns {
|
2024-01-15 19:15:39 -05:00
|
|
|
include: Some(options.enabled_paths),
|
|
|
|
exclude: options.disabled_paths,
|
2023-03-30 17:47:53 -04:00
|
|
|
};
|
2024-01-08 12:18:42 -05:00
|
|
|
let file_patterns_by_base = file_patterns.split_by_base();
|
2023-06-13 15:48:53 -04:00
|
|
|
|
|
|
|
// initialize the finder with the initial paths
|
2024-01-08 12:18:42 -05:00
|
|
|
for (path, file_patterns) in file_patterns_by_base {
|
|
|
|
if path.is_dir() {
|
|
|
|
if is_allowed_root_dir(&path) {
|
2023-09-18 15:58:16 -04:00
|
|
|
finder
|
2024-01-08 12:18:42 -05:00
|
|
|
.root_dir_entries
|
|
|
|
.push(PendingEntry::Dir(path, Rc::new(file_patterns)));
|
2023-03-30 17:47:53 -04:00
|
|
|
}
|
2024-01-08 12:18:42 -05:00
|
|
|
} else {
|
|
|
|
finder
|
|
|
|
.pending_entries
|
|
|
|
.push_back(PendingEntry::SpecifiedRootFile(path));
|
2023-03-30 17:47:53 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-01 15:10:30 -04:00
|
|
|
finder
|
2023-03-30 17:47:53 -04:00
|
|
|
}
|
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
pub fn hit_limit(&self) -> bool {
|
|
|
|
self.entry_count >= self.limit
|
|
|
|
}
|
|
|
|
|
2023-03-30 17:47:53 -04:00
|
|
|
fn get_valid_specifier(path: &Path) -> Option<ModuleSpecifier> {
|
2023-04-01 15:10:30 -04:00
|
|
|
fn is_allowed_media_type(media_type: MediaType) -> bool {
|
2023-03-30 17:47:53 -04:00
|
|
|
match media_type {
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Dts
|
|
|
|
| MediaType::Dmts
|
|
|
|
| MediaType::Dcts
|
|
|
|
| MediaType::Tsx => true,
|
|
|
|
MediaType::Json // ignore because json never depends on other files
|
|
|
|
| MediaType::Wasm
|
|
|
|
| MediaType::SourceMap
|
|
|
|
| MediaType::TsBuildInfo
|
|
|
|
| MediaType::Unknown => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let media_type = MediaType::from_path(path);
|
2023-04-01 15:10:30 -04:00
|
|
|
if is_allowed_media_type(media_type) {
|
2023-03-30 17:47:53 -04:00
|
|
|
if let Ok(specifier) = ModuleSpecifier::from_file_path(path) {
|
|
|
|
return Some(specifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for PreloadDocumentFinder {
|
|
|
|
type Item = ModuleSpecifier;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2023-04-01 15:10:30 -04:00
|
|
|
fn is_discoverable_dir(dir_path: &Path) -> bool {
|
|
|
|
if let Some(dir_name) = dir_path.file_name() {
|
|
|
|
let dir_name = dir_name.to_string_lossy().to_lowercase();
|
|
|
|
// We ignore these directories by default because there is a
|
|
|
|
// high likelihood they aren't relevant. Someone can opt-into
|
|
|
|
// them by specifying one of them as an enabled path.
|
|
|
|
if matches!(dir_name.as_str(), "node_modules" | ".git") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore cargo target directories for anyone using Deno with Rust
|
|
|
|
if dir_name == "target"
|
|
|
|
&& dir_path
|
|
|
|
.parent()
|
|
|
|
.map(|p| p.join("Cargo.toml").exists())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2023-03-30 17:47:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 15:10:30 -04:00
|
|
|
fn is_discoverable_file(file_path: &Path) -> bool {
|
|
|
|
// Don't auto-discover minified files as they are likely to be very large
|
|
|
|
// and likely not to have dependencies on code outside them that would
|
|
|
|
// be useful in the LSP
|
|
|
|
if let Some(file_name) = file_path.file_name() {
|
|
|
|
let file_name = file_name.to_string_lossy().to_lowercase();
|
|
|
|
!file_name.as_str().contains(".min.")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
// This first drains all the pending entries then adds the root dir entries
|
|
|
|
// one at a time to the pending entries before draining them. This is because
|
|
|
|
// we're traversing based on directory depth, so we want to search deeper
|
|
|
|
// directories first
|
|
|
|
while !self.pending_entries.is_empty() || !self.root_dir_entries.is_empty()
|
|
|
|
{
|
|
|
|
while let Some(entry) = self.pending_entries.pop_front() {
|
|
|
|
match entry {
|
|
|
|
PendingEntry::SpecifiedRootFile(file) => {
|
|
|
|
// since it was a file that was specified as a root url, only
|
|
|
|
// verify that it's valid
|
|
|
|
if let Some(specifier) = Self::get_valid_specifier(&file) {
|
|
|
|
return Some(specifier);
|
|
|
|
}
|
2023-04-01 15:10:30 -04:00
|
|
|
}
|
2024-01-08 12:18:42 -05:00
|
|
|
PendingEntry::Dir(dir_path, file_patterns) => {
|
|
|
|
if self.visited_paths.insert(dir_path.clone()) {
|
|
|
|
if let Ok(read_dir) = fs::read_dir(&dir_path) {
|
|
|
|
self.pending_entries.push_back(PendingEntry::ReadDir(
|
|
|
|
Box::new(read_dir),
|
|
|
|
file_patterns,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 15:10:30 -04:00
|
|
|
}
|
2024-01-08 12:18:42 -05:00
|
|
|
PendingEntry::ReadDir(mut entries, file_patterns) => {
|
|
|
|
while let Some(entry) = entries.next() {
|
|
|
|
self.entry_count += 1;
|
2023-04-01 15:10:30 -04:00
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
if self.hit_limit() {
|
|
|
|
self.pending_entries.clear(); // stop searching
|
|
|
|
return None;
|
|
|
|
}
|
2023-04-01 15:10:30 -04:00
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
if let Ok(entry) = entry {
|
|
|
|
let path = entry.path();
|
|
|
|
if let Ok(file_type) = entry.file_type() {
|
|
|
|
if file_patterns.matches_path(&path) {
|
|
|
|
if file_type.is_dir() && is_discoverable_dir(&path) {
|
|
|
|
self.pending_entries.push_back(PendingEntry::Dir(
|
|
|
|
path.to_path_buf(),
|
|
|
|
file_patterns.clone(),
|
|
|
|
));
|
|
|
|
} else if file_type.is_file() && is_discoverable_file(&path)
|
|
|
|
{
|
|
|
|
if let Some(specifier) = Self::get_valid_specifier(&path)
|
|
|
|
{
|
|
|
|
// restore the next entries for next time
|
|
|
|
self.pending_entries.push_front(PendingEntry::ReadDir(
|
|
|
|
entries,
|
|
|
|
file_patterns.clone(),
|
|
|
|
));
|
|
|
|
return Some(specifier);
|
|
|
|
}
|
2023-06-13 15:48:53 -04:00
|
|
|
}
|
2023-04-01 15:10:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 17:47:53 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-01 15:10:30 -04:00
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
if let Some(entry) = self.root_dir_entries.pop() {
|
|
|
|
self.pending_entries.push_back(entry);
|
2023-04-01 15:10:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
None
|
|
|
|
}
|
2023-04-01 15:10:30 -04:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-08-01 20:49:09 -04:00
|
|
|
use crate::cache::GlobalHttpCache;
|
2023-08-08 10:23:02 -04:00
|
|
|
use crate::cache::RealDenoCacheEnv;
|
2023-02-22 14:15:25 -05:00
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
use super::*;
|
2023-01-28 10:18:32 -05:00
|
|
|
use import_map::ImportMap;
|
2023-03-30 17:47:53 -04:00
|
|
|
use pretty_assertions::assert_eq;
|
2023-06-10 11:09:45 -04:00
|
|
|
use test_util::PathRef;
|
2022-04-01 11:15:37 -04:00
|
|
|
use test_util::TempDir;
|
2021-01-22 05:03:16 -05:00
|
|
|
|
2023-06-10 11:09:45 -04:00
|
|
|
fn setup(temp_dir: &TempDir) -> (Documents, PathRef) {
|
2021-10-28 19:56:01 -04:00
|
|
|
let location = temp_dir.path().join("deps");
|
2023-08-08 10:23:02 -04:00
|
|
|
let cache = Arc::new(GlobalHttpCache::new(
|
|
|
|
location.to_path_buf(),
|
|
|
|
RealDenoCacheEnv,
|
|
|
|
));
|
2023-07-08 16:06:45 -04:00
|
|
|
let documents = Documents::new(cache);
|
2021-10-28 19:56:01 -04:00
|
|
|
(documents, location)
|
2021-08-18 23:19:12 -04:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
#[test]
|
2021-10-28 19:56:01 -04:00
|
|
|
fn test_documents_open() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let (mut documents, _) = setup(&temp_dir);
|
2021-10-28 19:56:01 -04:00
|
|
|
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let content = r#"import * as b from "./b.ts";
|
2021-10-28 19:56:01 -04:00
|
|
|
console.log(b);
|
2022-05-20 16:40:55 -04:00
|
|
|
"#;
|
|
|
|
let document = documents.open(
|
|
|
|
specifier,
|
|
|
|
1,
|
|
|
|
"javascript".parse().unwrap(),
|
|
|
|
content.into(),
|
2021-06-02 06:29:58 -04:00
|
|
|
);
|
2021-11-12 11:42:04 -05:00
|
|
|
assert!(document.is_open());
|
|
|
|
assert!(document.is_diagnosable());
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-28 19:56:01 -04:00
|
|
|
fn test_documents_change() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let (mut documents, _) = setup(&temp_dir);
|
2021-10-28 19:56:01 -04:00
|
|
|
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let content = r#"import * as b from "./b.ts";
|
2021-10-28 19:56:01 -04:00
|
|
|
console.log(b);
|
2022-05-20 16:40:55 -04:00
|
|
|
"#;
|
2021-10-28 19:56:01 -04:00
|
|
|
documents.open(
|
2021-06-02 06:29:58 -04:00
|
|
|
specifier.clone(),
|
|
|
|
1,
|
2021-10-28 19:56:01 -04:00
|
|
|
"javascript".parse().unwrap(),
|
2022-05-20 16:40:55 -04:00
|
|
|
content.into(),
|
2021-06-02 06:29:58 -04:00
|
|
|
);
|
2021-10-28 19:56:01 -04:00
|
|
|
documents
|
2021-01-22 05:03:16 -05:00
|
|
|
.change(
|
|
|
|
&specifier,
|
|
|
|
2,
|
2021-01-29 14:34:33 -05:00
|
|
|
vec![lsp::TextDocumentContentChangeEvent {
|
|
|
|
range: Some(lsp::Range {
|
|
|
|
start: lsp::Position {
|
2021-10-28 19:56:01 -04:00
|
|
|
line: 1,
|
|
|
|
character: 13,
|
2021-01-22 05:03:16 -05:00
|
|
|
},
|
2021-01-29 14:34:33 -05:00
|
|
|
end: lsp::Position {
|
2021-10-28 19:56:01 -04:00
|
|
|
line: 1,
|
|
|
|
character: 13,
|
2021-01-22 05:03:16 -05:00
|
|
|
},
|
|
|
|
}),
|
2021-10-28 19:56:01 -04:00
|
|
|
range_length: None,
|
|
|
|
text: r#", "hello deno""#.to_string(),
|
2021-01-22 05:03:16 -05:00
|
|
|
}],
|
|
|
|
)
|
2021-10-28 19:56:01 -04:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
2022-05-20 16:40:55 -04:00
|
|
|
&*documents.get(&specifier).unwrap().content(),
|
2021-10-28 19:56:01 -04:00
|
|
|
r#"import * as b from "./b.ts";
|
|
|
|
console.log(b, "hello deno");
|
|
|
|
"#
|
2021-08-18 23:19:12 -04:00
|
|
|
);
|
2021-06-02 06:29:58 -04:00
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_documents_ensure_no_duplicates() {
|
|
|
|
// it should never happen that a user of this API causes this to happen,
|
|
|
|
// but we'll guard against it anyway
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let (mut documents, documents_path) = setup(&temp_dir);
|
2021-11-18 13:50:24 -05:00
|
|
|
let file_path = documents_path.join("file.ts");
|
|
|
|
let file_specifier = ModuleSpecifier::from_file_path(&file_path).unwrap();
|
2023-06-10 11:09:45 -04:00
|
|
|
documents_path.create_dir_all();
|
|
|
|
file_path.write("");
|
2021-11-18 13:50:24 -05:00
|
|
|
|
|
|
|
// open the document
|
|
|
|
documents.open(
|
|
|
|
file_specifier.clone(),
|
|
|
|
1,
|
|
|
|
LanguageId::TypeScript,
|
2022-05-20 16:40:55 -04:00
|
|
|
"".into(),
|
2021-11-18 13:50:24 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// make a clone of the document store and close the document in that one
|
|
|
|
let mut documents2 = documents.clone();
|
|
|
|
documents2.close(&file_specifier).unwrap();
|
|
|
|
|
|
|
|
// At this point the document will be in both documents and the shared file system documents.
|
|
|
|
// Now make sure that the original documents doesn't return both copies
|
2023-03-29 16:25:48 -04:00
|
|
|
assert_eq!(documents.documents(DocumentsFilter::All).len(), 1);
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2023-01-28 10:18:32 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_documents_refresh_dependencies_config_change() {
|
|
|
|
// it should never happen that a user of this API causes this to happen,
|
|
|
|
// but we'll guard against it anyway
|
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let (mut documents, documents_path) = setup(&temp_dir);
|
|
|
|
fs::create_dir_all(&documents_path).unwrap();
|
|
|
|
|
|
|
|
let file1_path = documents_path.join("file1.ts");
|
|
|
|
let file1_specifier = ModuleSpecifier::from_file_path(&file1_path).unwrap();
|
|
|
|
fs::write(&file1_path, "").unwrap();
|
|
|
|
|
|
|
|
let file2_path = documents_path.join("file2.ts");
|
|
|
|
let file2_specifier = ModuleSpecifier::from_file_path(&file2_path).unwrap();
|
|
|
|
fs::write(&file2_path, "").unwrap();
|
|
|
|
|
|
|
|
let file3_path = documents_path.join("file3.ts");
|
|
|
|
let file3_specifier = ModuleSpecifier::from_file_path(&file3_path).unwrap();
|
|
|
|
fs::write(&file3_path, "").unwrap();
|
|
|
|
|
|
|
|
// set the initial import map and point to file 2
|
|
|
|
{
|
|
|
|
let mut import_map = ImportMap::new(
|
|
|
|
ModuleSpecifier::from_file_path(documents_path.join("import_map.json"))
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
import_map
|
|
|
|
.imports_mut()
|
|
|
|
.append("test".to_string(), "./file2.ts".to_string())
|
|
|
|
.unwrap();
|
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
documents.update_config(UpdateDocumentConfigOptions {
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: Default::default(),
|
|
|
|
disabled_paths: Default::default(),
|
2023-05-11 17:17:14 -04:00
|
|
|
document_preload_limit: 1_000,
|
|
|
|
maybe_import_map: Some(Arc::new(import_map)),
|
|
|
|
maybe_config_file: None,
|
|
|
|
maybe_package_json: None,
|
2023-10-25 14:39:00 -04:00
|
|
|
node_resolver: None,
|
2023-09-30 12:06:38 -04:00
|
|
|
npm_resolver: None,
|
2023-05-11 17:17:14 -04:00
|
|
|
});
|
2023-01-28 10:18:32 -05:00
|
|
|
|
|
|
|
// open the document
|
|
|
|
let document = documents.open(
|
|
|
|
file1_specifier.clone(),
|
|
|
|
1,
|
|
|
|
LanguageId::TypeScript,
|
|
|
|
"import {} from 'test';".into(),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
document
|
|
|
|
.dependencies()
|
|
|
|
.get("test")
|
|
|
|
.unwrap()
|
|
|
|
.maybe_code
|
|
|
|
.maybe_specifier()
|
|
|
|
.map(ToOwned::to_owned),
|
|
|
|
Some(file2_specifier),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now point at file 3
|
|
|
|
{
|
|
|
|
let mut import_map = ImportMap::new(
|
|
|
|
ModuleSpecifier::from_file_path(documents_path.join("import_map.json"))
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
import_map
|
|
|
|
.imports_mut()
|
|
|
|
.append("test".to_string(), "./file3.ts".to_string())
|
|
|
|
.unwrap();
|
|
|
|
|
2023-05-11 17:17:14 -04:00
|
|
|
documents.update_config(UpdateDocumentConfigOptions {
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: Default::default(),
|
|
|
|
disabled_paths: Default::default(),
|
2023-05-11 17:17:14 -04:00
|
|
|
document_preload_limit: 1_000,
|
|
|
|
maybe_import_map: Some(Arc::new(import_map)),
|
|
|
|
maybe_config_file: None,
|
|
|
|
maybe_package_json: None,
|
2023-10-25 14:39:00 -04:00
|
|
|
node_resolver: None,
|
2023-09-30 12:06:38 -04:00
|
|
|
npm_resolver: None,
|
2023-05-11 17:17:14 -04:00
|
|
|
});
|
2023-01-28 10:18:32 -05:00
|
|
|
|
|
|
|
// check the document's dependencies
|
|
|
|
let document = documents.get(&file1_specifier).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
document
|
|
|
|
.dependencies()
|
|
|
|
.get("test")
|
|
|
|
.unwrap()
|
|
|
|
.maybe_code
|
|
|
|
.maybe_specifier()
|
|
|
|
.map(ToOwned::to_owned),
|
|
|
|
Some(file3_specifier),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 17:47:53 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_pre_load_document_finder() {
|
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
temp_dir.create_dir_all("root1/node_modules/");
|
|
|
|
temp_dir.write("root1/node_modules/mod.ts", ""); // no, node_modules
|
|
|
|
|
|
|
|
temp_dir.create_dir_all("root1/sub_dir");
|
2023-04-01 15:10:30 -04:00
|
|
|
temp_dir.create_dir_all("root1/target");
|
|
|
|
temp_dir.create_dir_all("root1/node_modules");
|
2023-03-30 17:47:53 -04:00
|
|
|
temp_dir.create_dir_all("root1/.git");
|
|
|
|
temp_dir.create_dir_all("root1/file.ts"); // no, directory
|
|
|
|
temp_dir.write("root1/mod1.ts", ""); // yes
|
|
|
|
temp_dir.write("root1/mod2.js", ""); // yes
|
|
|
|
temp_dir.write("root1/mod3.tsx", ""); // yes
|
|
|
|
temp_dir.write("root1/mod4.d.ts", ""); // yes
|
|
|
|
temp_dir.write("root1/mod5.jsx", ""); // yes
|
|
|
|
temp_dir.write("root1/mod6.mjs", ""); // yes
|
|
|
|
temp_dir.write("root1/mod7.mts", ""); // yes
|
|
|
|
temp_dir.write("root1/mod8.d.mts", ""); // yes
|
|
|
|
temp_dir.write("root1/other.json", ""); // no, json
|
|
|
|
temp_dir.write("root1/other.txt", ""); // no, text file
|
|
|
|
temp_dir.write("root1/other.wasm", ""); // no, don't load wasm
|
2023-04-01 15:10:30 -04:00
|
|
|
temp_dir.write("root1/Cargo.toml", ""); // no
|
2023-03-30 17:47:53 -04:00
|
|
|
temp_dir.write("root1/sub_dir/mod.ts", ""); // yes
|
|
|
|
temp_dir.write("root1/sub_dir/data.min.ts", ""); // no, minified file
|
|
|
|
temp_dir.write("root1/.git/main.ts", ""); // no, .git folder
|
2023-04-01 15:10:30 -04:00
|
|
|
temp_dir.write("root1/node_modules/main.ts", ""); // no, because it's in a node_modules folder
|
|
|
|
temp_dir.write("root1/target/main.ts", ""); // no, because there is a Cargo.toml in the root directory
|
2023-03-30 17:47:53 -04:00
|
|
|
|
|
|
|
temp_dir.create_dir_all("root2/folder");
|
2023-06-13 15:48:53 -04:00
|
|
|
temp_dir.create_dir_all("root2/sub_folder");
|
2023-03-30 17:47:53 -04:00
|
|
|
temp_dir.write("root2/file1.ts", ""); // yes, provided
|
|
|
|
temp_dir.write("root2/file2.ts", ""); // no, not provided
|
2023-04-01 15:10:30 -04:00
|
|
|
temp_dir.write("root2/main.min.ts", ""); // yes, provided
|
2023-03-30 17:47:53 -04:00
|
|
|
temp_dir.write("root2/folder/main.ts", ""); // yes, provided
|
2023-06-13 15:48:53 -04:00
|
|
|
temp_dir.write("root2/sub_folder/a.js", ""); // no, not provided
|
|
|
|
temp_dir.write("root2/sub_folder/b.ts", ""); // no, not provided
|
|
|
|
temp_dir.write("root2/sub_folder/c.js", ""); // no, not provided
|
2023-03-30 17:47:53 -04:00
|
|
|
|
|
|
|
temp_dir.create_dir_all("root3/");
|
|
|
|
temp_dir.write("root3/mod.ts", ""); // no, not provided
|
|
|
|
|
2023-06-13 15:48:53 -04:00
|
|
|
let mut urls = PreloadDocumentFinder::new(PreloadDocumentFinderOptions {
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: PathOrPatternSet::from_relative_path_or_patterns(
|
|
|
|
temp_dir.path().as_path(),
|
|
|
|
&[
|
|
|
|
"root1".to_string(),
|
|
|
|
"root2/file1.ts".to_string(),
|
|
|
|
"root2/main.min.ts".to_string(),
|
|
|
|
"root2/folder".to_string(),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
disabled_paths: Default::default(),
|
2023-06-13 15:48:53 -04:00
|
|
|
limit: 1_000,
|
|
|
|
})
|
2023-03-30 17:47:53 -04:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2023-04-01 15:10:30 -04:00
|
|
|
// Ideally we would test for order here, which should be BFS, but
|
|
|
|
// different file systems have different directory iteration
|
|
|
|
// so we sort the results
|
2023-03-30 17:47:53 -04:00
|
|
|
urls.sort();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
urls,
|
|
|
|
vec![
|
|
|
|
temp_dir.uri().join("root1/mod1.ts").unwrap(),
|
|
|
|
temp_dir.uri().join("root1/mod2.js").unwrap(),
|
|
|
|
temp_dir.uri().join("root1/mod3.tsx").unwrap(),
|
|
|
|
temp_dir.uri().join("root1/mod4.d.ts").unwrap(),
|
|
|
|
temp_dir.uri().join("root1/mod5.jsx").unwrap(),
|
|
|
|
temp_dir.uri().join("root1/mod6.mjs").unwrap(),
|
|
|
|
temp_dir.uri().join("root1/mod7.mts").unwrap(),
|
|
|
|
temp_dir.uri().join("root1/mod8.d.mts").unwrap(),
|
|
|
|
temp_dir.uri().join("root1/sub_dir/mod.ts").unwrap(),
|
|
|
|
temp_dir.uri().join("root2/file1.ts").unwrap(),
|
|
|
|
temp_dir.uri().join("root2/folder/main.ts").unwrap(),
|
2023-04-01 15:10:30 -04:00
|
|
|
temp_dir.uri().join("root2/main.min.ts").unwrap(),
|
2023-03-30 17:47:53 -04:00
|
|
|
]
|
|
|
|
);
|
2023-04-01 15:10:30 -04:00
|
|
|
|
|
|
|
// now try iterating with a low limit
|
2023-06-13 15:48:53 -04:00
|
|
|
let urls = PreloadDocumentFinder::new(PreloadDocumentFinderOptions {
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: PathOrPatternSet::new(vec![PathOrPattern::Path(
|
|
|
|
temp_dir.path().to_path_buf(),
|
|
|
|
)]),
|
|
|
|
disabled_paths: Default::default(),
|
2023-06-13 15:48:53 -04:00
|
|
|
limit: 10, // entries and not results
|
|
|
|
})
|
2023-04-01 15:10:30 -04:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// since different file system have different iteration
|
|
|
|
// order, the number here may vary, so just assert it's below
|
|
|
|
// a certain amount
|
|
|
|
assert!(urls.len() < 5, "Actual length: {}", urls.len());
|
2023-06-13 15:48:53 -04:00
|
|
|
|
|
|
|
// now try with certain directories and files disabled
|
|
|
|
let mut urls = PreloadDocumentFinder::new(PreloadDocumentFinderOptions {
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: PathOrPatternSet::new(vec![PathOrPattern::Path(
|
|
|
|
temp_dir.path().to_path_buf(),
|
|
|
|
)]),
|
|
|
|
disabled_paths: PathOrPatternSet::from_relative_path_or_patterns(
|
|
|
|
temp_dir.path().as_path(),
|
|
|
|
&[
|
|
|
|
"root1".to_string(),
|
|
|
|
"root2/file1.ts".to_string(),
|
|
|
|
"**/*.js".to_string(), // ignore js files
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2023-06-13 15:48:53 -04:00
|
|
|
limit: 1_000,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
urls.sort();
|
|
|
|
assert_eq!(
|
|
|
|
urls,
|
|
|
|
vec![
|
|
|
|
temp_dir.uri().join("root2/file2.ts").unwrap(),
|
|
|
|
temp_dir.uri().join("root2/folder/main.ts").unwrap(),
|
|
|
|
temp_dir.uri().join("root2/sub_folder/b.ts").unwrap(), // won't have the javascript files
|
|
|
|
temp_dir.uri().join("root3/mod.ts").unwrap(),
|
|
|
|
]
|
|
|
|
);
|
2023-03-30 17:47:53 -04:00
|
|
|
}
|
2023-04-01 12:02:44 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_pre_load_document_finder_disallowed_dirs() {
|
|
|
|
if cfg!(windows) {
|
2023-06-13 15:48:53 -04:00
|
|
|
let paths = PreloadDocumentFinder::new(PreloadDocumentFinderOptions {
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: PathOrPatternSet::new(vec![PathOrPattern::Path(
|
|
|
|
PathBuf::from("C:\\"),
|
|
|
|
)]),
|
|
|
|
disabled_paths: Default::default(),
|
2023-06-13 15:48:53 -04:00
|
|
|
limit: 1_000,
|
|
|
|
})
|
2023-04-01 15:10:30 -04:00
|
|
|
.collect::<Vec<_>>();
|
2023-04-01 12:02:44 -04:00
|
|
|
assert_eq!(paths, vec![]);
|
|
|
|
} else {
|
2023-06-13 15:48:53 -04:00
|
|
|
let paths = PreloadDocumentFinder::new(PreloadDocumentFinderOptions {
|
2024-01-15 19:15:39 -05:00
|
|
|
enabled_paths: PathOrPatternSet::new(vec![PathOrPattern::Path(
|
|
|
|
PathBuf::from("/"),
|
|
|
|
)]),
|
|
|
|
disabled_paths: Default::default(),
|
2023-06-13 15:48:53 -04:00
|
|
|
limit: 1_000,
|
|
|
|
})
|
2023-05-11 17:17:14 -04:00
|
|
|
.collect::<Vec<_>>();
|
2023-04-01 12:02:44 -04:00
|
|
|
assert_eq!(paths, vec![]);
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|