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: check require.cache to find preloaded modules #406

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

jean-michelet
Copy link
Contributor

Fixes #404

I tested on the reproduction repo and it does work well: https://github.com/zetaraku/fastify-app

runtime.js Outdated Show resolved Hide resolved
runtime.js Outdated Show resolved Hide resolved
runtime.js Outdated
@@ -14,11 +16,16 @@ function checkProcessArgv (moduleName) {

let preloadModules
function checkPreloadModules (moduleName) {
const modulePath = path.join(process.cwd(), 'node_modules/', moduleName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about this path join.
I just want to do a startsWith instead of includes.

Copy link

@zetaraku zetaraku Aug 1, 2024

Choose a reason for hiding this comment

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

Can process.cwd() guarantee to be the project root if the program is executed from other path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would you do?


const modulePath = path.join(process.cwd(), 'node_modules', moduleName)
if (Object.keys(require.cache).some(k => k.startsWith(modulePath))) {
preloadModules.push(moduleName)
Copy link

@zetaraku zetaraku Aug 1, 2024

Choose a reason for hiding this comment

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

I still discourage mutating preloadModules, as you may mutate the process._preload_modules that may be used by others.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

runtime.js Outdated Show resolved Hide resolved
runtime.js Outdated Show resolved Hide resolved
runtime.js Show resolved Hide resolved
@climba03003 climba03003 dismissed their stale review August 1, 2024 05:53

Nevermind, double checked it is not affected.

@mcollina
Copy link
Member

Thanks for opening a PR! Can you please add a unit test?

@jean-michelet
Copy link
Contributor Author

@zetaraku seems to prefer this to be fixed in fastify-cli.
Maybe we should investigate, plus I am not sure how to write relevant unit tests for this.

Are you willing to push a PR on cli @zetaraku? As a POC?

@zetaraku
Copy link

Will try to make one later.

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.

Cannot detect @swc-node/register loaded by fastify-cli with the -r option
4 participants