-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
require does not cache exceptions #13386
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
Comments
No strong opinion, but I am slightly against this change for some reasons: First, and most importantly, the vast majority of What is your use case? If you want to handle the case that a module does not exist, you can use
I don't believe this is true for most users. When attempting to The documentation is a little vague about this point:
If we decide not to do the suggested change, we might want to clarify this behavior in the documentation. |
This is very true, and what I suggest would not change that behavior; if anything, it would make that behavior more consistent for all possible cases.
Whether modules should throw an Error like that in the first place - recoverable or not - is of course debatable. However, the issue is that if a module does throw in such a way, then it may lead to very cryptic issues because of the fact that the code is executed more than once - when, as you pointed out, the documentation indicates that this should not be happening.
This is not what I am trying to do, as per the code sample I have attached to this issue. I personally do not have a use case for that; what happened is that I am using a proprietary framework which includes a code path similar to what is presented here, and I ended up seeing the module code being executed twice. In other words, I submit that providing a behavior similar to what I have suggested would make debugging much easier for cases where some dependency might be doing funky stuff like that.
I find this rather unlikely (nor safe), but supposing there would be such use-case; in the case of trying to re-require code which is already in the |
This reminds me of "RFC 2308 - Negative Caching of DNS Queries (DNS NCACHE)" especially when considering ESM and |
I wont be recommending any change of this magnitude to CJS; the ESM cache does properly save exceptions. The CJS ESM bridge was intentionally vague here, but recent change to ECMAScript spec mandates rethrowing and we can rephrase to be more concrete. |
@refack Negative DNS caches can be a PITA though... |
I know that's why I think it's a good comparison. Theoretically a failed |
@refack As for the file system going offline/online, I guess that's a valid case. But I also think it is likely to be less common. In my opinion, it would make more sense to force cache invalidation if you want to reimport. |
AFAIK But anyway a neg-cache is a trade off, and it would be introducing a change to a very delicate mechanism. |
What I meant './module' will be converted to absolute path before being looked up in
@bmeck does this mean require will rely on the import cache in the future? Or that it is already the case? |
No it is not, and will not. CJS and ESM are two different systems. I think Bradley was just pointing out the fact that ESM will have a different behavior. |
It's been a while and there seems to be no consensus so I'll go ahead and close this out. |
Uh oh!
There was an error while loading. Please reload this page.
Ref: https://gist.github.com/stelcheck/0d981e2b951b8c95a3487a733fde1925
If upon require, a module file throws, it will be re-required again upon subsequent attempts. This means that the content of the file will be re-executed as well, which I believe is not likely to be a desirable behavior (otherwise one would manually clean up the require cache).
What I would suggest is instead to cache the thrown Error and re-throw it upon subsequent require attempts.
The text was updated successfully, but these errors were encountered: