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

fix(core): use require.resolve instead of randomly matching from our … #14523

Merged
merged 1 commit into from
Jan 23, 2023

Conversation

FrozenPandaz
Copy link
Collaborator

@FrozenPandaz FrozenPandaz commented Jan 20, 2023

…project graph

Current Behavior

We resolve any imports which match the name of one of the projects in our project graph.

This is kind of arbitrary because this dependency would only exist in Nx and wouldn't stem from any underlying tool.

Expected Behavior

We use require.resolve to resolve those dependencies. This is more valid because the dependency would exist natively in JS.

Related Issue(s)

Fixes #

@vercel
Copy link

vercel bot commented Jan 20, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
nx-dev ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Jan 20, 2023 at 10:21PM (UTC)

@meeroslav
Copy link
Contributor

Require.resolve is notoriously slow. Can we have some benchmarks to check the impact? Running our linter rule on any medium size (50-100 projects) monorepo should tell us.

In any case I would move it after npm package check as that's cheaper operation.

@@ -135,22 +137,17 @@ export class TargetProjectLocator {
return;
}

private findProject(importExpr: string): string | undefined {
if (this.projectResolutionCache.has(importExpr)) {
Copy link
Member

Choose a reason for hiding this comment

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

Do you think its worth keeping the cache? Or I guess require.resolve may have some caching, I know regular require does.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

require has its own cache:
https://nodejs.org/api/modules.html#modules_caching

But it should also be rare for it to get down there.

@FrozenPandaz
Copy link
Collaborator Author

In any case I would move it after npm package check as that's cheaper operation.

It is already below the npm package check. It's below all of our existing checks actually.

The last line isn't part of the actual check. it sets undefined into the cache is to remember that we couldn't resolve the import for later runs.

@FrozenPandaz
Copy link
Collaborator Author

FrozenPandaz commented Jan 23, 2023

I did a benchmark and it took ~4ms for 500 unique imports on my machine.

Rule                               | Time (ms) | Relative
:----------------------------------|----------:|--------:
@nrwl/nx/enforce-module-boundaries |     4.352 |   100.0%
Rule | Time (ms) | Relative
:----|----------:|--------:

Copy link
Contributor

@meeroslav meeroslav left a comment

Choose a reason for hiding this comment

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

Thank you for the benchmark and code reshuffle. Looks good to me!

@FrozenPandaz FrozenPandaz merged commit 21d65cb into nrwl:master Jan 23, 2023
@FrozenPandaz FrozenPandaz deleted the fix-resolve branch January 23, 2023 19:07
@github-actions
Copy link

github-actions bot commented Mar 7, 2023

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants