Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add WorkspaceFactory and ResolverFactory #27766

Merged
merged 23 commits into from
Jan 23, 2025

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Jan 22, 2025

Allows easily constructing a DenoResolver using the exact same logic that we use in the CLI (useful for dnt and for external bundlers). This code is then used in the CLI to ensure the logic is always up-to-date.

use std::rc::Rc;

use deno_resolver::factory::ResolverFactory;
use deno_resolver::factory::WorkspaceFactory;
use sys_traits::impls::RealSys;

let sys = RealSys;
let cwd = sys.env_current_dir()?;
let workspace_factory = Rc::new(WorkspaceFactory::new(sys, cwd, Default::default()));
let resolver_factory = ResolverFactory::new(workspace_factory.clone(), Default::default());
let deno_resolver = resolver_factory.deno_resolver().await?;

pub struct ImportMapSpecifierResolveError {
#[source]
source: deno_path_util::ResolveUrlOrPathError,
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shifted this code out of the CLI, but then I moved it back. I kept the changes.

@dsherret dsherret marked this pull request as ready for review January 22, 2025 16:58
self.options.no_npm
}

pub fn node_modules_dir_mode(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pay attention here. I rewrote the logic for determining the NodeModulesDirMode because previously it was convoluted. This should be much more straightforward.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted from CLI crate.

#[inline(always)]
fn is_builtin_node_module(&self, module_name: &str) -> bool {
DENO_SUPPORTED_BUILTIN_NODE_MODULES
.binary_search(&module_name)
Copy link
Member Author

@dsherret dsherret Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to binary search instead of iterating because DENO_SUPPORTED_BUILTIN_NODE_MODULES is guaranteed to be sorted.

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine for me, but probably good to get another pair of eyes on it before landing.

Comment on lines +59 to +64
/// Discover `.npmrc` file - currently we only support it next to `package.json`,
/// next to `deno.json`, or in the user's home directory.
///
/// In the future we will need to support it in the global directory
/// as per https://docs.npmjs.com/cli/v10/configuring-npm/npmrc#files.
pub fn discover_npmrc_from_workspace<TSys: EnvVar + EnvHomeDir + FsRead>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause conflicts with #27119 :(

Comment on lines +24 to +25
pub static DENO_SUPPORTED_BUILTIN_NODE_MODULES: &[&str] = &[
// NOTE(bartlomieju): keep this list in sync with `ext/node/polyfills/01_require.js`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure this is reachable when a crate is imported - Deploy relies on this list

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checked and it is.

Copy link
Member

@nathanwhit nathanwhit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dsherret dsherret merged commit 273ec9f into denoland:main Jan 23, 2025
17 checks passed
@dsherret dsherret deleted the refactor_resolver_factory branch January 23, 2025 23:52
bartlomieju pushed a commit that referenced this pull request Jan 30, 2025
Allows easily constructing a `DenoResolver` using the exact same logic
that we use in the CLI (useful for dnt and for external bundlers). This
code is then used in the CLI to ensure the logic is always up-to-date.

```rs
use std::rc::Rc;

use deno_resolver::factory::ResolverFactory;
use deno_resolver::factory::WorkspaceFactory;
use sys_traits::impls::RealSys;

let sys = RealSys;
let cwd = sys.env_current_dir()?;
let workspace_factory = Rc::new(WorkspaceFactory::new(sys, cwd, Default::default()));
let resolver_factory = ResolverFactory::new(workspace_factory.clone(), Default::default());
let deno_resolver = resolver_factory.deno_resolver().await?;
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants