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

Clarify behavior of require('http') and require('node:http') with existing require.cache entries #52992

Closed
wants to merge 1 commit into from

Conversation

gitspeaks
Copy link

No description provided.

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. module Issues and PRs related to the module subsystem. labels May 14, 2024
@RedYetiDev
Copy link
Member

@nodejs/documentation
@nodejs/loaders

Comment on lines +408 to +414
passed to `require()`. For instance, `require('http')` will
return the built-in HTTP module, even if there is a file by that name. However,
note that core modules specified without the `node:` prefix are served from the
cache if an entry for the module exists. Because cache entries can be manually
altered by modifying the `require.cache `object, this allows the module's
implementation to be replaced. To ensure `require() ` returns the original
implementation, use the `node:` prefix. The list
Copy link
Contributor

@aduh95 aduh95 May 15, 2024

Choose a reason for hiding this comment

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

I would be more direct, at the risk of being vague: mutation of the module cache is a thing in CJS, but that shouldn't matter for most users – and for those for which it does matter, it seems fair to assume they won't need the extra details.

Suggested change
passed to `require()`. For instance, `require('http')` will
return the built-in HTTP module, even if there is a file by that name. However,
note that core modules specified without the `node:` prefix are served from the
cache if an entry for the module exists. Because cache entries can be manually
altered by modifying the `require.cache `object, this allows the module's
implementation to be replaced. To ensure `require() ` returns the original
implementation, use the `node:` prefix. The list
passed to `require()`. For instance, assuming no mutation of the module cache, `require('http')`
will return the built-in HTTP module, even if there is a file by that name. The list

Copy link
Author

Choose a reason for hiding this comment

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

As a newcomer to this, I find the wording "assuming no mutation of the module cache" somewhat confusing.
My goal is to clearly understand the specification, and this phrase doesn't help clarify the situation for me.
I believe my initial wording is preferable because it explicitly states the technical condition under which the behavior differs.

Copy link
Contributor

Choose a reason for hiding this comment

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

How is it confusing, and how can we clear up that confusion?

Copy link
Author

Choose a reason for hiding this comment

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

I find the wording "assuming no mutation of the module cache" somewhat confusing because it implies that mutations exist, which begs the question: "under what circumstances?" My wording describes the specification as it is. As for how to move forward, feel free to decide how to write this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's fine, the rest of the documentation should contain the answer to that question. Maybe it's clearer if we say require.cache instead of "the module cache"

Suggested change
passed to `require()`. For instance, `require('http')` will
return the built-in HTTP module, even if there is a file by that name. However,
note that core modules specified without the `node:` prefix are served from the
cache if an entry for the module exists. Because cache entries can be manually
altered by modifying the `require.cache `object, this allows the module's
implementation to be replaced. To ensure `require() ` returns the original
implementation, use the `node:` prefix. The list
passed to `require()`. For instance, assuming `require.cache` is not modified, `require('http')`
will return the built-in HTTP module, even if there is a file by that name. The list

If you look at the section just above this one, it already deals with how caching works in CJS

Copy link
Member

Choose a reason for hiding this comment

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

IMO it should be past tense, 'assuming require.cache has not been modified'

Copy link
Author

Choose a reason for hiding this comment

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

FYI, I initially raised this question in the IRC, and the reply I got indicated that there is an impression that core modules are not served from the cache at all, which suggests that this is not so clear. Just my 2c.

@gitspeaks
Copy link
Author

@aduh95 prefers different wording, so this PR is being closed.

@gitspeaks gitspeaks closed this May 15, 2024
@aduh95
Copy link
Contributor

aduh95 commented May 15, 2024

@aduh95 prefers different wording, so this PR is being closed.

To clarify, my comments are non-blocking. If you re-open, that might give opportunity for more people to contribute to the discussion.

@gitspeaks
Copy link
Author

@aduh95 I apologize, but this is taking up more of my attention than I can afford. I believe the amendment is trivial, and I see no issue with my wording. It's clear that we have different writing styles, which is okay. I don't have a particular interest in advocating for my style. I think we all agree on the overall message, and the GitHub discussion is still open.
In any case, I don't see the point in keeping my PR open if it means revising its entire content, as it becomes not my PR :) Thank you for clarifying the initial issue.

@RedYetiDev
Copy link
Member

RedYetiDev commented May 15, 2024

but this is taking up more of my attention than I can afford

I'm sorry to hear that, if you'd like, someone could make some changes, and credit you as the Co-Author, as it was your inspiration?

(I'm speaking for myself, and not on behalf of the 'someone')

@gitspeaks
Copy link
Author

@RedYetiDev No worries! No credit needed. I'm happy to be the behind-the-scenes inspiration. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. module Issues and PRs related to the module subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants