1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-19 12:16:17 -05:00

refactor(npm): move SloppyImportsCachedFs to deno_resolver (#27610)

This commit is contained in:
David Sherret 2025-01-09 18:30:48 -05:00 committed by GitHub
parent 8bafb182ef
commit 34beeb7703
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 57 additions and 69 deletions

View file

@ -15,6 +15,7 @@ use deno_resolver::cjs::IsCjsResolutionMode;
use deno_resolver::npm::managed::ManagedInNpmPkgCheckerCreateOptions; use deno_resolver::npm::managed::ManagedInNpmPkgCheckerCreateOptions;
use deno_resolver::npm::CreateInNpmPkgCheckerOptions; use deno_resolver::npm::CreateInNpmPkgCheckerOptions;
use deno_resolver::npm::NpmReqResolverOptions; use deno_resolver::npm::NpmReqResolverOptions;
use deno_resolver::sloppy_imports::SloppyImportsCachedFs;
use deno_resolver::DenoResolverOptions; use deno_resolver::DenoResolverOptions;
use deno_resolver::NodeAndNpmReqResolver; use deno_resolver::NodeAndNpmReqResolver;
use deno_runtime::deno_fs; use deno_runtime::deno_fs;
@ -80,7 +81,6 @@ use crate::resolver::CliResolver;
use crate::resolver::CliResolverOptions; use crate::resolver::CliResolverOptions;
use crate::resolver::CliSloppyImportsResolver; use crate::resolver::CliSloppyImportsResolver;
use crate::resolver::NpmModuleLoader; use crate::resolver::NpmModuleLoader;
use crate::resolver::SloppyImportsCachedFs;
use crate::standalone::binary::DenoCompileBinaryWriter; use crate::standalone::binary::DenoCompileBinaryWriter;
use crate::sys::CliSys; use crate::sys::CliSys;
use crate::tools::check::TypeChecker; use crate::tools::check::TypeChecker;

View file

@ -29,6 +29,7 @@ use deno_graph::ResolutionError;
use deno_graph::SpecifierError; use deno_graph::SpecifierError;
use deno_graph::WorkspaceFastCheckOption; use deno_graph::WorkspaceFastCheckOption;
use deno_path_util::url_to_file_path; use deno_path_util::url_to_file_path;
use deno_resolver::sloppy_imports::SloppyImportsCachedFs;
use deno_resolver::sloppy_imports::SloppyImportsResolutionKind; use deno_resolver::sloppy_imports::SloppyImportsResolutionKind;
use deno_runtime::deno_node; use deno_runtime::deno_node;
use deno_runtime::deno_permissions::PermissionsContainer; use deno_runtime::deno_permissions::PermissionsContainer;
@ -55,7 +56,6 @@ use crate::npm::CliNpmResolver;
use crate::resolver::CjsTracker; use crate::resolver::CjsTracker;
use crate::resolver::CliResolver; use crate::resolver::CliResolver;
use crate::resolver::CliSloppyImportsResolver; use crate::resolver::CliSloppyImportsResolver;
use crate::resolver::SloppyImportsCachedFs;
use crate::sys::CliSys; use crate::sys::CliSys;
use crate::tools::check; use crate::tools::check;
use crate::tools::check::CheckError; use crate::tools::check::CheckError;

View file

@ -46,6 +46,7 @@ use deno_lint::linter::LintConfig as DenoLintConfig;
use deno_npm::npm_rc::ResolvedNpmRc; use deno_npm::npm_rc::ResolvedNpmRc;
use deno_package_json::PackageJsonCache; use deno_package_json::PackageJsonCache;
use deno_path_util::url_to_file_path; use deno_path_util::url_to_file_path;
use deno_resolver::sloppy_imports::SloppyImportsCachedFs;
use deno_runtime::deno_node::PackageJson; use deno_runtime::deno_node::PackageJson;
use indexmap::IndexSet; use indexmap::IndexSet;
use lsp_types::ClientCapabilities; use lsp_types::ClientCapabilities;
@ -65,7 +66,6 @@ use crate::cache::FastInsecureHasher;
use crate::file_fetcher::CliFileFetcher; use crate::file_fetcher::CliFileFetcher;
use crate::lsp::logging::lsp_warn; use crate::lsp::logging::lsp_warn;
use crate::resolver::CliSloppyImportsResolver; use crate::resolver::CliSloppyImportsResolver;
use crate::resolver::SloppyImportsCachedFs;
use crate::sys::CliSys; use crate::sys::CliSys;
use crate::tools::lint::CliLinter; use crate::tools::lint::CliLinter;
use crate::tools::lint::CliLinterOptions; use crate::tools::lint::CliLinterOptions;

View file

@ -26,6 +26,7 @@ use deno_graph::Resolution;
use deno_graph::ResolutionError; use deno_graph::ResolutionError;
use deno_graph::SpecifierError; use deno_graph::SpecifierError;
use deno_lint::linter::LintConfig as DenoLintConfig; use deno_lint::linter::LintConfig as DenoLintConfig;
use deno_resolver::sloppy_imports::SloppyImportsCachedFs;
use deno_resolver::sloppy_imports::SloppyImportsResolution; use deno_resolver::sloppy_imports::SloppyImportsResolution;
use deno_resolver::sloppy_imports::SloppyImportsResolutionKind; use deno_resolver::sloppy_imports::SloppyImportsResolutionKind;
use deno_runtime::deno_node; use deno_runtime::deno_node;
@ -61,7 +62,6 @@ use crate::graph_util;
use crate::graph_util::enhanced_resolution_error_message; use crate::graph_util::enhanced_resolution_error_message;
use crate::lsp::lsp_custom::DiagnosticBatchNotificationParams; use crate::lsp::lsp_custom::DiagnosticBatchNotificationParams;
use crate::resolver::CliSloppyImportsResolver; use crate::resolver::CliSloppyImportsResolver;
use crate::resolver::SloppyImportsCachedFs;
use crate::sys::CliSys; use crate::sys::CliSys;
use crate::tools::lint::CliLinter; use crate::tools::lint::CliLinter;
use crate::tools::lint::CliLinterOptions; use crate::tools::lint::CliLinterOptions;

View file

@ -1,12 +1,9 @@
// Copyright 2018-2025 the Deno authors. MIT license. // Copyright 2018-2025 the Deno authors. MIT license.
use std::borrow::Cow; use std::borrow::Cow;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use dashmap::DashMap;
use dashmap::DashSet; use dashmap::DashSet;
use deno_ast::MediaType; use deno_ast::MediaType;
use deno_config::workspace::MappedResolutionDiagnostic; use deno_config::workspace::MappedResolutionDiagnostic;
@ -22,6 +19,7 @@ use deno_graph::source::UnknownBuiltInNodeModuleError;
use deno_graph::NpmLoadError; use deno_graph::NpmLoadError;
use deno_graph::NpmResolvePkgReqsResult; use deno_graph::NpmResolvePkgReqsResult;
use deno_npm::resolution::NpmResolutionError; use deno_npm::resolution::NpmResolutionError;
use deno_resolver::sloppy_imports::SloppyImportsCachedFs;
use deno_resolver::sloppy_imports::SloppyImportsResolver; use deno_resolver::sloppy_imports::SloppyImportsResolver;
use deno_runtime::colors; use deno_runtime::colors;
use deno_runtime::deno_fs; use deno_runtime::deno_fs;
@ -30,8 +28,6 @@ use deno_runtime::deno_node::RealIsBuiltInNodeModuleChecker;
use deno_semver::package::PackageReq; use deno_semver::package::PackageReq;
use node_resolver::NodeResolutionKind; use node_resolver::NodeResolutionKind;
use node_resolver::ResolutionMode; use node_resolver::ResolutionMode;
use sys_traits::FsMetadata;
use sys_traits::FsMetadataValue;
use thiserror::Error; use thiserror::Error;
use crate::args::NpmCachingStrategy; use crate::args::NpmCachingStrategy;
@ -45,11 +41,12 @@ use crate::util::text_encoding::from_utf8_lossy_cow;
pub type CjsTracker = deno_resolver::cjs::CjsTracker<CliSys>; pub type CjsTracker = deno_resolver::cjs::CjsTracker<CliSys>;
pub type IsCjsResolver = deno_resolver::cjs::IsCjsResolver<CliSys>; pub type IsCjsResolver = deno_resolver::cjs::IsCjsResolver<CliSys>;
pub type CliSloppyImportsCachedFs = SloppyImportsCachedFs<CliSys>;
pub type CliSloppyImportsResolver = pub type CliSloppyImportsResolver =
SloppyImportsResolver<SloppyImportsCachedFs>; SloppyImportsResolver<CliSloppyImportsCachedFs>;
pub type CliDenoResolver = deno_resolver::DenoResolver< pub type CliDenoResolver = deno_resolver::DenoResolver<
RealIsBuiltInNodeModuleChecker, RealIsBuiltInNodeModuleChecker,
SloppyImportsCachedFs, CliSloppyImportsCachedFs,
CliSys, CliSys,
>; >;
pub type CliNpmReqResolver = pub type CliNpmReqResolver =
@ -397,60 +394,3 @@ impl<'a> deno_graph::source::NpmResolver for WorkerCliNpmGraphResolver<'a> {
self.bare_node_builtins_enabled self.bare_node_builtins_enabled
} }
} }
#[derive(Debug)]
pub struct SloppyImportsCachedFs {
sys: CliSys,
cache: Option<
DashMap<
PathBuf,
Option<deno_resolver::sloppy_imports::SloppyImportsFsEntry>,
>,
>,
}
impl SloppyImportsCachedFs {
pub fn new(sys: CliSys) -> Self {
Self {
sys,
cache: Some(Default::default()),
}
}
pub fn new_without_stat_cache(fs: CliSys) -> Self {
Self {
sys: fs,
cache: None,
}
}
}
impl deno_resolver::sloppy_imports::SloppyImportResolverFs
for SloppyImportsCachedFs
{
fn stat_sync(
&self,
path: &Path,
) -> Option<deno_resolver::sloppy_imports::SloppyImportsFsEntry> {
if let Some(cache) = &self.cache {
if let Some(entry) = cache.get(path) {
return *entry;
}
}
let entry = self.sys.fs_metadata(path).ok().and_then(|stat| {
if stat.file_type().is_file() {
Some(deno_resolver::sloppy_imports::SloppyImportsFsEntry::File)
} else if stat.file_type().is_dir() {
Some(deno_resolver::sloppy_imports::SloppyImportsFsEntry::Dir)
} else {
None
}
});
if let Some(cache) = &self.cache {
cache.insert(path.to_owned(), entry);
}
entry
}
}

View file

@ -660,6 +660,7 @@ mod tests {
use deno_config::workspace::ResolverWorkspaceJsrPackage; use deno_config::workspace::ResolverWorkspaceJsrPackage;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::url::Url; use deno_core::url::Url;
use deno_resolver::sloppy_imports::SloppyImportsCachedFs;
use deno_runtime::deno_node::PackageJson; use deno_runtime::deno_node::PackageJson;
use deno_semver::Version; use deno_semver::Version;
use import_map::ImportMapWithDiagnostics; use import_map::ImportMapWithDiagnostics;
@ -668,7 +669,6 @@ mod tests {
use test_util::testdata_path; use test_util::testdata_path;
use super::*; use super::*;
use crate::resolver::SloppyImportsCachedFs;
use crate::sys::CliSys; use crate::sys::CliSys;
fn parse_ast(specifier: &Url, source_code: &str) -> ParsedSource { fn parse_ast(specifier: &Url, source_code: &str) -> ParsedSource {

View file

@ -7,8 +7,12 @@ use std::path::PathBuf;
use deno_media_type::MediaType; use deno_media_type::MediaType;
use deno_path_util::url_from_file_path; use deno_path_util::url_from_file_path;
use deno_path_util::url_to_file_path; use deno_path_util::url_to_file_path;
use sys_traits::FsMetadata;
use sys_traits::FsMetadataValue;
use url::Url; use url::Url;
use crate::sync::MaybeDashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SloppyImportsFsEntry { pub enum SloppyImportsFsEntry {
File, File,
@ -368,6 +372,50 @@ impl<Fs: SloppyImportResolverFs> SloppyImportsResolver<Fs> {
} }
} }
#[derive(Debug)]
pub struct SloppyImportsCachedFs<TSys: FsMetadata> {
sys: TSys,
cache: Option<MaybeDashMap<PathBuf, Option<SloppyImportsFsEntry>>>,
}
impl<TSys: FsMetadata> SloppyImportsCachedFs<TSys> {
pub fn new(sys: TSys) -> Self {
Self {
sys,
cache: Some(Default::default()),
}
}
pub fn new_without_stat_cache(sys: TSys) -> Self {
Self { sys, cache: None }
}
}
impl<TSys: FsMetadata> SloppyImportResolverFs for SloppyImportsCachedFs<TSys> {
fn stat_sync(&self, path: &Path) -> Option<SloppyImportsFsEntry> {
if let Some(cache) = &self.cache {
if let Some(entry) = cache.get(path) {
return *entry;
}
}
let entry = self.sys.fs_metadata(path).ok().and_then(|stat| {
if stat.file_type().is_file() {
Some(SloppyImportsFsEntry::File)
} else if stat.file_type().is_dir() {
Some(SloppyImportsFsEntry::Dir)
} else {
None
}
});
if let Some(cache) = &self.cache {
cache.insert(path.to_owned(), entry);
}
entry
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use test_util::TestContext; use test_util::TestContext;