-
Notifications
You must be signed in to change notification settings - Fork 29.1k
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
module: make require.cache reassignable #8888
Conversation
Previously, when wanting to clear require.cache completely, the only options were to manually delete each property or to reassign the underlying cache object at require('module')._cache. By making require.cache a passthrough to require('module')._cache, the require cache can now be cleared by just setting require.cache to a new empty object.
test/parallel/test-require-cache.js
Outdated
|
||
assert.strictEqual(require(relativePath).extraProperty, mod.extraProperty); | ||
require.cache = {}; | ||
assert.equal(typeof require(relativePath).extraProperty, 'undefined'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for not using assert.strictEqual(require(relativePath).extraProperty, undefined);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, no. No reason. Fixed that.
337cae4
to
4ea7980
Compare
Is this semver minor? Also I'm not sure what is the policy for |
@lpinca My guess is semver-minor at the very least. It turns out |
If |
I would like to see a slightly better description as reassigning |
Do we actually want to support this? Also note that the example in the testcase here doesn't delete all the references to the original module (even the internal ones), and re-iterating that would eventually exhaust all the available memory and crash. See #8443. If we do want to support actually clearing (or partially clearing) If we don't want to support that — I see no need in this change. /cc @nodejs/ctc, perhaps. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't support this change. Locking into such internals-exposing API will potentially cause us troubles in the future, and the incentive for this change doesn't seem to overweight the cons for me.
@ChALkeR Some testing modules clear the require cache via @indutny Do you mean in terms of changing |
@bengl I wonder if there are any implications with the ongoing modules effort? cc @bmeck |
What happens to |
@indutny Right, hence the attempt here to make an officially supported method of clearing the whole cache (apart from manually deleting all the properties), since it's being done anyway. On the other hand... clearing the whole cache can lead to things like #6160. But as long as that remains documented (and it is), then I think that's not a problem. Here's a potential alternative: The docs could be updated to make it clearer that the object cannot simply be replaced, and what to do instead. @lpinca At the moment, nothing. This PR would solve that too, via the getter. |
@bengl yeah my comment was more a way to emphasize that |
I'm with @indutny on this, we'll need a cache api for es modules though. That being said I'm not entirely against having a proper (but underscored) way to do this imo |
The underscore is just the messenger, the fact is that it's a part of unsupported and undocumented internal API. Removing the underscore or even a simplier change like in this PR don't change that, and in fact make things worse — it encourages using unsupported internal API and brings in some more lines of code to support such behaviour, without proper documentation. If we need that for internal use, then this change would be fine. If the intent here is to change an internal API so that it could be better used by external libraries, but still keeping it internal — please, no. If we can come up with situations when this API is really needed and if we want to export such API — we need to make it public and documented.
This PR doesn't change anything about that being not supported officially — that would require docs change. |
The idempotency requirement of ESM means only the global cache for finding resolved specifiers can be cleared. Local cache of unresolved specifiers cannot be cleared. |
I'm +1 on exposing a public API for this but I agree that this may not be the right way. Rather than using a getter/setter, I'd rather see a function on |
c133999
to
83c7a88
Compare
@jasnell @Fishrock123 @indutny @ChALkeR Is this something that you guys want to discuss in a CTC meeting ? |
it's likely worth discussing |
This would need to be revisited in light of #10789 |
ping @bengl |
Closing since I don't think this is the correct approach anyway. |
Checklist
make -j8 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
module
Description of change
Previously, when wanting to clear require.cache completely, the only
options were to manually delete each property or to reassign the
underlying cache object at
require('module')._cache
.By making require.cache a passthrough to
require('module')._cache
, therequire cache can now be cleared by just setting
require.cache
to a newempty object.