diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-12-06 19:03:18 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-07 00:03:18 +0000 |
commit | 890780a9e905f506e897f632d9cfbe153e66b931 (patch) | |
tree | 8e926e3bac67f096f3bec7748e14d507b62cfee4 /cli/resolver.rs | |
parent | 9314928990a6cbd0bc0abe3100872ba2682eda9a (diff) |
feat(unstable): ability to resolve specifiers with no extension, specifiers for a directory, and TS files from JS extensions (#21464)
Adds an `--unstable-sloppy-imports` flag which supports the
following for `file:` specifiers:
* Allows writing `./mod` in a specifier to do extension probing.
- ex. `import { Example } from "./example"` instead of `import { Example
} from "./example.ts"`
* Allows writing `./routes` to do directory extension probing for files
like `./routes/index.ts`
* Allows writing `./mod.js` for *mod.ts* files.
This functionality is **NOT RECOMMENDED** for general use with Deno:
1. It's not as optimal for perf:
https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-2/
1. It makes tooling in the ecosystem more complex in order to have to
understand this.
1. The "Deno way" is to be explicit about what you're doing. It's better
in the long run.
1. It doesn't work if published to the Deno registry because doing stuff
like extension probing with remote specifiers would be incredibly slow.
This is instead only recommended to help with migrating existing
projects to Deno. For example, it's very useful for getting CJS projects
written with import/export declaration working in Deno without modifying
module specifiers and for supporting TS ESM projects written with
`./mod.js` specifiers.
This feature will output warnings to guide the user towards correcting
their specifiers. Additionally, quick fixes are provided in the LSP to
update these specifiers:
Diffstat (limited to 'cli/resolver.rs')
-rw-r--r-- | cli/resolver.rs | 359 |
1 files changed, 356 insertions, 3 deletions
diff --git a/cli/resolver.rs b/cli/resolver.rs index 44de3aa3f..12b86632f 100644 --- a/cli/resolver.rs +++ b/cli/resolver.rs @@ -1,10 +1,12 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use deno_ast::MediaType; use deno_core::anyhow::anyhow; use deno_core::error::AnyError; use deno_core::futures::future; use deno_core::futures::future::LocalBoxFuture; use deno_core::futures::FutureExt; +use deno_core::parking_lot::Mutex; use deno_core::ModuleSpecifier; use deno_graph::source::NpmPackageReqResolution; use deno_graph::source::NpmResolver; @@ -24,16 +26,21 @@ use deno_runtime::permissions::PermissionsContainer; use deno_semver::npm::NpmPackageReqReference; use deno_semver::package::PackageReq; use import_map::ImportMap; +use std::borrow::Cow; +use std::collections::HashMap; +use std::path::Path; use std::path::PathBuf; use std::sync::Arc; use crate::args::package_json::PackageJsonDeps; use crate::args::JsxImportSourceConfig; use crate::args::PackageJsonDepsProvider; +use crate::graph_util::format_range_with_colors; use crate::module_loader::CjsResolutionStore; use crate::npm::ByonmCliNpmResolver; use crate::npm::CliNpmResolver; use crate::npm::InnerCliNpmResolverRef; +use crate::util::path::specifier_to_file_path; use crate::util::sync::AtomicFlag; /// Result of checking if a specifier is mapped via @@ -110,6 +117,7 @@ impl MappedSpecifierResolver { #[derive(Debug)] pub struct CliGraphResolver { fs: Arc<dyn FileSystem>, + sloppy_imports_resolver: Option<UnstableSloppyImportsResolver>, mapped_specifier_resolver: MappedSpecifierResolver, maybe_default_jsx_import_source: Option<String>, maybe_jsx_import_source_module: Option<String>, @@ -124,6 +132,7 @@ pub struct CliGraphResolver { pub struct CliGraphResolverOptions<'a> { pub fs: Arc<dyn FileSystem>, pub cjs_resolutions: Option<Arc<CjsResolutionStore>>, + pub sloppy_imports_resolver: Option<UnstableSloppyImportsResolver>, pub node_resolver: Option<Arc<NodeResolver>>, pub npm_resolver: Option<Arc<dyn CliNpmResolver>>, pub package_json_deps_provider: Arc<PackageJsonDepsProvider>, @@ -143,6 +152,7 @@ impl CliGraphResolver { Self { fs: options.fs, cjs_resolutions: options.cjs_resolutions, + sloppy_imports_resolver: options.sloppy_imports_resolver, mapped_specifier_resolver: MappedSpecifierResolver::new( options.maybe_import_map, if is_byonm { @@ -232,7 +242,7 @@ impl Resolver for CliGraphResolver { fn resolve( &self, specifier: &str, - referrer: &ModuleSpecifier, + referrer_range: &deno_graph::Range, mode: ResolutionMode, ) -> Result<ModuleSpecifier, ResolveError> { fn to_node_mode(mode: ResolutionMode) -> NodeResolutionMode { @@ -242,6 +252,7 @@ impl Resolver for CliGraphResolver { } } + let referrer = &referrer_range.specifier; let result = match self .mapped_specifier_resolver .resolve(specifier, referrer)? @@ -253,10 +264,26 @@ impl Resolver for CliGraphResolver { self.found_package_json_dep_flag.raise(); Ok(specifier) } - MappedResolution::None => deno_graph::resolve_import(specifier, referrer) - .map_err(|err| err.into()), + MappedResolution::None => { + deno_graph::resolve_import(specifier, &referrer_range.specifier) + .map_err(|err| err.into()) + } }; + // do sloppy imports resolution if enabled + let result = + if let Some(sloppy_imports_resolver) = &self.sloppy_imports_resolver { + result.map(|specifier| { + sloppy_imports_resolve( + sloppy_imports_resolver, + specifier, + referrer_range, + ) + }) + } else { + result + }; + // When the user is vendoring, don't allow them to import directly from the vendor/ directory // as it might cause them confusion or duplicate dependencies. Additionally, this folder has // special treatment in the language server so it will definitely cause issues/confusion there @@ -371,6 +398,52 @@ impl Resolver for CliGraphResolver { } } +fn sloppy_imports_resolve( + resolver: &UnstableSloppyImportsResolver, + specifier: ModuleSpecifier, + referrer_range: &deno_graph::Range, +) -> ModuleSpecifier { + let resolution = resolver.resolve(&specifier); + let hint_message = match &resolution { + UnstableSloppyImportsResolution::JsToTs(to_specifier) => { + let from_media_type = MediaType::from_specifier(&specifier); + let to_media_type = MediaType::from_specifier(to_specifier); + format!( + "update {} extension to {}", + from_media_type.as_ts_extension(), + to_media_type.as_ts_extension() + ) + } + UnstableSloppyImportsResolution::NoExtension(to_specifier) => { + let to_media_type = MediaType::from_specifier(to_specifier); + format!("add {} extension", to_media_type.as_ts_extension()) + } + UnstableSloppyImportsResolution::Directory(to_specifier) => { + let file_name = to_specifier + .path() + .rsplit_once('/') + .map(|(_, file_name)| file_name) + .unwrap_or(to_specifier.path()); + format!("specify path to {} file in directory instead", file_name) + } + UnstableSloppyImportsResolution::None(_) => return specifier, + }; + // show a warning when this happens in order to drive + // the user towards correcting these specifiers + log::warn!( + "{} Sloppy import resolution {}\n at {}", + crate::colors::yellow("Warning"), + crate::colors::gray(format!("(hint: {})", hint_message)), + if referrer_range.end == deno_graph::Position::zeroed() { + // not worth showing the range in this case + crate::colors::cyan(referrer_range.specifier.as_str()).to_string() + } else { + format_range_with_colors(referrer_range) + }, + ); + resolution.into_owned_specifier() +} + fn resolve_package_json_dep( specifier: &str, deps: &PackageJsonDeps, @@ -467,10 +540,208 @@ impl NpmResolver for CliGraphResolver { } } +#[derive(Debug)] +struct UnstableSloppyImportsStatCache { + fs: Arc<dyn FileSystem>, + cache: Mutex<HashMap<PathBuf, Option<UnstableSloppyImportsFsEntry>>>, +} + +impl UnstableSloppyImportsStatCache { + pub fn new(fs: Arc<dyn FileSystem>) -> Self { + Self { + fs, + cache: Default::default(), + } + } + + pub fn stat_sync(&self, path: &Path) -> Option<UnstableSloppyImportsFsEntry> { + // there will only ever be one thread in here at a + // time, so it's ok to hold the lock for so long + let mut cache = self.cache.lock(); + if let Some(entry) = cache.get(path) { + return *entry; + } + + let entry = self.fs.stat_sync(path).ok().and_then(|stat| { + if stat.is_file { + Some(UnstableSloppyImportsFsEntry::File) + } else if stat.is_directory { + Some(UnstableSloppyImportsFsEntry::Dir) + } else { + None + } + }); + cache.insert(path.to_owned(), entry); + entry + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum UnstableSloppyImportsFsEntry { + File, + Dir, +} + +#[derive(Debug, PartialEq, Eq)] +pub enum UnstableSloppyImportsResolution<'a> { + /// No sloppy resolution was found. + None(&'a ModuleSpecifier), + /// Ex. `./file.js` to `./file.ts` + JsToTs(ModuleSpecifier), + /// Ex. `./file` to `./file.ts` + NoExtension(ModuleSpecifier), + /// Ex. `./dir` to `./dir/index.ts` + Directory(ModuleSpecifier), +} + +impl<'a> UnstableSloppyImportsResolution<'a> { + pub fn into_specifier(self) -> Cow<'a, ModuleSpecifier> { + match self { + Self::None(specifier) => Cow::Borrowed(specifier), + Self::JsToTs(specifier) => Cow::Owned(specifier), + Self::NoExtension(specifier) => Cow::Owned(specifier), + Self::Directory(specifier) => Cow::Owned(specifier), + } + } + + pub fn into_owned_specifier(self) -> ModuleSpecifier { + match self { + Self::None(specifier) => specifier.clone(), + Self::JsToTs(specifier) => specifier, + Self::NoExtension(specifier) => specifier, + Self::Directory(specifier) => specifier, + } + } +} + +#[derive(Debug)] +pub struct UnstableSloppyImportsResolver { + stat_cache: UnstableSloppyImportsStatCache, +} + +impl UnstableSloppyImportsResolver { + pub fn new(fs: Arc<dyn FileSystem>) -> Self { + Self { + stat_cache: UnstableSloppyImportsStatCache::new(fs), + } + } + + pub fn resolve_with_stat_sync( + specifier: &ModuleSpecifier, + stat_sync: impl Fn(&Path) -> Option<UnstableSloppyImportsFsEntry>, + ) -> UnstableSloppyImportsResolution { + if specifier.scheme() != "file" { + return UnstableSloppyImportsResolution::None(specifier); + } + + let Ok(path) = specifier_to_file_path(specifier) else { + return UnstableSloppyImportsResolution::None(specifier); + }; + let mut is_dir_resolution = false; + let mut is_no_ext_resolution = false; + let probe_paths = match (stat_sync)(&path) { + Some(UnstableSloppyImportsFsEntry::File) => { + return UnstableSloppyImportsResolution::None(specifier); + } + Some(UnstableSloppyImportsFsEntry::Dir) => { + is_dir_resolution = true; + // try to resolve at the index file + vec![ + path.join("index.ts"), + path.join("index.js"), + path.join("index.mts"), + path.join("index.mjs"), + path.join("index.tsx"), + path.join("index.jsx"), + ] + } + None => { + let media_type = MediaType::from_specifier(specifier); + let probe_media_type_types = match media_type { + MediaType::JavaScript => vec![MediaType::TypeScript, MediaType::Tsx], + MediaType::Jsx => vec![MediaType::Tsx], + MediaType::Mjs => vec![MediaType::Mts], + MediaType::Cjs => vec![MediaType::Cts], + MediaType::TypeScript + | MediaType::Mts + | MediaType::Cts + | MediaType::Dts + | MediaType::Dmts + | MediaType::Dcts + | MediaType::Tsx + | MediaType::Json + | MediaType::Wasm + | MediaType::TsBuildInfo + | MediaType::SourceMap => { + return UnstableSloppyImportsResolution::None(specifier) + } + MediaType::Unknown => { + is_no_ext_resolution = true; + vec![ + MediaType::TypeScript, + MediaType::JavaScript, + MediaType::Tsx, + MediaType::Jsx, + MediaType::Mts, + MediaType::Mjs, + ] + } + }; + let old_path_str = path.to_string_lossy(); + let old_path_str = match media_type { + MediaType::Unknown => old_path_str, + _ => match old_path_str.strip_suffix(media_type.as_ts_extension()) { + Some(s) => Cow::Borrowed(s), + None => return UnstableSloppyImportsResolution::None(specifier), + }, + }; + probe_media_type_types + .into_iter() + .map(|media_type| { + PathBuf::from(format!( + "{}{}", + old_path_str, + media_type.as_ts_extension() + )) + }) + .collect::<Vec<_>>() + } + }; + + for probe_path in probe_paths { + if (stat_sync)(&probe_path) == Some(UnstableSloppyImportsFsEntry::File) { + if let Ok(specifier) = ModuleSpecifier::from_file_path(probe_path) { + if is_dir_resolution { + return UnstableSloppyImportsResolution::Directory(specifier); + } else if is_no_ext_resolution { + return UnstableSloppyImportsResolution::NoExtension(specifier); + } else { + return UnstableSloppyImportsResolution::JsToTs(specifier); + } + } + } + } + + UnstableSloppyImportsResolution::None(specifier) + } + + pub fn resolve<'a>( + &self, + specifier: &'a ModuleSpecifier, + ) -> UnstableSloppyImportsResolution<'a> { + Self::resolve_with_stat_sync(specifier, |path| { + self.stat_cache.stat_sync(path) + }) + } +} + #[cfg(test)] mod test { use std::collections::BTreeMap; + use deno_runtime::deno_fs::RealFs; + use test_util::TestContext; + use super::*; #[test] @@ -532,4 +803,86 @@ mod test { // non-existent bare specifier assert_eq!(resolve("non-existent", &deps).unwrap(), None); } + + #[test] + fn test_unstable_sloppy_imports() { + fn resolve(specifier: &ModuleSpecifier) -> UnstableSloppyImportsResolution { + UnstableSloppyImportsResolver::resolve_with_stat_sync(specifier, |path| { + RealFs.stat_sync(path).ok().and_then(|stat| { + if stat.is_file { + Some(UnstableSloppyImportsFsEntry::File) + } else if stat.is_directory { + Some(UnstableSloppyImportsFsEntry::Dir) + } else { + None + } + }) + }) + } + + let context = TestContext::default(); + let temp_dir = context.temp_dir().path(); + + // scenarios like resolving ./example.js to ./example.ts + for (ext_from, ext_to) in [("js", "ts"), ("js", "tsx"), ("mjs", "mts")] { + let ts_file = temp_dir.join(format!("file.{}", ext_to)); + ts_file.write(""); + let ts_file_uri = ts_file.uri_file(); + assert_eq!( + resolve(&ts_file.uri_file()), + UnstableSloppyImportsResolution::None(&ts_file_uri), + ); + assert_eq!( + resolve( + &temp_dir + .uri_dir() + .join(&format!("file.{}", ext_from)) + .unwrap() + ), + UnstableSloppyImportsResolution::JsToTs(ts_file.uri_file()), + ); + ts_file.remove_file(); + } + + // no extension scenarios + for ext in ["js", "ts", "js", "tsx", "jsx", "mjs", "mts"] { + let file = temp_dir.join(format!("file.{}", ext)); + file.write(""); + assert_eq!( + resolve( + &temp_dir + .uri_dir() + .join("file") // no ext + .unwrap() + ), + UnstableSloppyImportsResolution::NoExtension(file.uri_file()), + ); + file.remove_file(); + } + + // .ts and .js exists, .js specified (goes to specified) + { + let ts_file = temp_dir.join("file.ts"); + ts_file.write(""); + let js_file = temp_dir.join("file.js"); + js_file.write(""); + let js_file_uri = js_file.uri_file(); + assert_eq!( + resolve(&js_file.uri_file()), + UnstableSloppyImportsResolution::None(&js_file_uri), + ); + } + + // resolving a directory to an index file + { + let routes_dir = temp_dir.join("routes"); + routes_dir.create_dir_all(); + let index_file = routes_dir.join("index.ts"); + index_file.write(""); + assert_eq!( + resolve(&routes_dir.uri_file()), + UnstableSloppyImportsResolution::Directory(index_file.uri_file()), + ); + } + } } |