-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Clearing require.cache of native addon makes it throw when required later. #6160
Comments
Modifying module cache isn't exactly supported. Would be possible for the module in question to be a bit more intelligent about clearing the cache? |
Modifying the cache is certainly documented, though perhaps incompletely at the moment. Lots of modules remove items from I'm not sure what "more intelligent" would mean here. The use case is being able to require the same native addon a second time, after clearing its cache entry in-between. Seems pretty normal to me, as that's how several frameworks for mocking/stubbing work. |
Reloading native add-ons currently isn't supported: the shared object isn't unloaded when you delete it from the cache because it would be unsafe to do so in many cases (unsafe as in segfault unsafe) and most add-ons aren't suitable for reloading anyway. They expect to be loaded once and only once; they don't expect to get unloaded. |
I think we should just document the fact it's not supported. This is very hard to get right for native modules anyway. |
oh hmm, I looked through the document and didn't see it, sorry! D:
This is me presuming there is a way to tell something is an addon, I'm not sure beyond that. Alternative silly idea: we make Honestly though, maybe that's unnecessary safety. We should at least document it. Maybe native addons could fall back to a second internal cache that can't be cleared? |
Emitting a warning when trying to unload a native module could do wonders. |
What's wrong with just documenting it? |
@bnoordhuis Nothing is wrong with just documenting it, it would be an improvement already - I think that emitting a warning as well would be far more useful though since it would give users very fast feedback about what went wrong in their code. Since Node now has warning infrastructure it makes sense to utilize it for this purpose. |
Only if it can be done without gross hacks and performance regressions - which I'm somewhat skeptical about. |
Decided to try cache approach, PTAL |
To be fair, |
Yes, but it could modify |
Clarify in docs for require.cache that reloading native modules isn't supported. Related: nodejs#6160
Clarify in docs for require.cache that reloading native modules isn't supported. Related: nodejs#6160 PR-URL: nodejs#6168 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: James M Snell <jasnell@gmail.com>
Oh, how it is inconvenient for me. :( It is like dlclose() removing from POSIX, "just for safety reasons". :( I have created a native module which handles licenses for my soft and which can change its internal state irreversible when something is going wrong. Now it seems I need to introduce some additional function 'clearState()' to my addon and export it in test configuration. Too inconvenient, too unflexible. :( |
It's been documented in commit 5c14d69 so I'll go ahead and close the issue. |
Apologies for reviving a dead issue, I just wanted to see if there was a better way to do what I'm attempting, because I ran into this issue after I had deleted a cached file from
The reason I am manually removing a file from the cache is because I am building a custom Node-Sass importer to allow JavaScript files to be imported into Sass. If a file is imported into the file that is imported into Sass, and a change is made to that file, Sass does not re-compile with the update. If a file is imported directly into Sass and a change is made to that file, then it will recompile with the update. This is because Is there anyway around this? |
@esr360 The javascript files importing into Sass are all non-native correct? So you should be able to safely delete them without getting the error. Only native ones (such as the importer itself) will fail on re-requirement. Are you clearing the entire cache? A solution I've been using to circumvent the error is to save the required module to a global as such:
This way it will not try and re-require if it has already been set. Using globals is never ideal but easiest solution I've found for now. |
This is how I WAR-ed my problem. When deleting just skip over file with .node extension. |
require
-ing a native addon after it has already been required and then removed from the require cache results in an error, rather than simply providing the module again.For example, I inserted the following code into a test called
require-cache-clear
, modelled aftertest/addons/hello-world
, using a dummy native module:The resulting error is:
A quick Google search informs me that such errors are usually the result of version mismatches, and a
node-gyp rebuild
will solve them. This isn't the case here.We came across this due to
mockery
's clearing of the cache. I suspect that folks using this or other modules that manipulaterequire.cache
/Module._cache
are going to (or have already) run into this at some point or another.My best guess is that this might be related to this comment here by @bnoordhuis.
The text was updated successfully, but these errors were encountered: