diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-10-25 14:39:00 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-25 14:39:00 -0400 |
commit | be97170a193e8cecc5ce03ecd3c1d0add4a06bf7 (patch) | |
tree | fab7d266e208db93dcf0870dda70f7da56ade735 /cli/cache/mod.rs | |
parent | 093b3eee58181ec45839d0fe10b8157326a102b2 (diff) |
feat(unstable): ability to `npm install` then `deno run main.ts` (#20967)
This PR adds a new unstable "bring your own node_modules" (BYONM)
functionality currently behind a `--unstable-byonm` flag (`"unstable":
["byonm"]` in a deno.json).
This enables users to run a separate install command (ex. `npm install`,
`pnpm install`) then run `deno run main.ts` and Deno will respect the
layout of the node_modules directory as setup by the separate install
command. It also works with npm/yarn/pnpm workspaces.
For this PR, the behaviour is opted into by specifying
`--unstable-byonm`/`"unstable": ["byonm"]`, but in the future we may
make this the default behaviour as outlined in
https://github.com/denoland/deno/issues/18967#issuecomment-1761248941
This is an extremely rough initial implementation. Errors are
terrible in this and the LSP requires frequent restarts. Improvements
will be done in follow up PRs.
Diffstat (limited to 'cli/cache/mod.rs')
-rw-r--r-- | cli/cache/mod.rs | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/cli/cache/mod.rs b/cli/cache/mod.rs index 1d6a79963..5cc91f50f 100644 --- a/cli/cache/mod.rs +++ b/cli/cache/mod.rs @@ -4,6 +4,7 @@ use crate::args::CacheSetting; use crate::errors::get_error_class_name; use crate::file_fetcher::FetchOptions; use crate::file_fetcher::FileFetcher; +use crate::npm::CliNpmResolver; use crate::util::fs::atomic_write_file; use deno_ast::MediaType; @@ -101,10 +102,10 @@ pub struct FetchCacher { file_fetcher: Arc<FileFetcher>, file_header_overrides: HashMap<ModuleSpecifier, HashMap<String, String>>, global_http_cache: Arc<GlobalHttpCache>, + npm_resolver: Arc<dyn CliNpmResolver>, parsed_source_cache: Arc<ParsedSourceCache>, permissions: PermissionsContainer, cache_info_enabled: bool, - maybe_local_node_modules_url: Option<ModuleSpecifier>, } impl FetchCacher { @@ -113,19 +114,19 @@ impl FetchCacher { file_fetcher: Arc<FileFetcher>, file_header_overrides: HashMap<ModuleSpecifier, HashMap<String, String>>, global_http_cache: Arc<GlobalHttpCache>, + npm_resolver: Arc<dyn CliNpmResolver>, parsed_source_cache: Arc<ParsedSourceCache>, permissions: PermissionsContainer, - maybe_local_node_modules_url: Option<ModuleSpecifier>, ) -> Self { Self { emit_cache, file_fetcher, file_header_overrides, global_http_cache, + npm_resolver, parsed_source_cache, permissions, cache_info_enabled: false, - maybe_local_node_modules_url, } } @@ -214,20 +215,18 @@ impl Loader for FetchCacher { ) -> LoadFuture { use deno_graph::source::CacheSetting as LoaderCacheSetting; - if let Some(node_modules_url) = self.maybe_local_node_modules_url.as_ref() { + if specifier.path().contains("/node_modules/") { // The specifier might be in a completely different symlinked tree than - // what the resolved node_modules_url is in (ex. `/my-project-1/node_modules` - // symlinked to `/my-project-2/node_modules`), so first check if the path - // is in a node_modules dir to avoid needlessly canonicalizing, then compare + // what the node_modules url is in (ex. `/my-project-1/node_modules` + // symlinked to `/my-project-2/node_modules`), so first we checked if the path + // is in a node_modules dir to avoid needlessly canonicalizing, then now compare // against the canonicalized specifier. - if specifier.path().contains("/node_modules/") { - let specifier = - crate::node::resolve_specifier_into_node_modules(specifier); - if specifier.as_str().starts_with(node_modules_url.as_str()) { - return Box::pin(futures::future::ready(Ok(Some( - LoadResponse::External { specifier }, - )))); - } + let specifier = + crate::node::resolve_specifier_into_node_modules(specifier); + if self.npm_resolver.in_npm_package(&specifier) { + return Box::pin(futures::future::ready(Ok(Some( + LoadResponse::External { specifier }, + )))); } } |