-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
module: cache synchronous module jobs before linking #52868
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -295,22 +295,33 @@ class ModuleJobSync extends ModuleJobBase { | |||||||
| #loader = null; | ||||||||
| constructor(loader, url, importAttributes, moduleWrap, isMain, inspectBrk) { | ||||||||
| super(url, importAttributes, moduleWrap, isMain, inspectBrk, true); | ||||||||
| assert(this.module instanceof ModuleWrap); | ||||||||
| this.#loader = loader; | ||||||||
| const moduleRequests = this.module.getModuleRequests(); | ||||||||
| // Specifiers should be aligned with the moduleRequests array in order. | ||||||||
| const specifiers = Array(moduleRequests.length); | ||||||||
| const modules = Array(moduleRequests.length); | ||||||||
| const jobs = Array(moduleRequests.length); | ||||||||
| for (let i = 0; i < moduleRequests.length; ++i) { | ||||||||
| const { specifier, attributes } = moduleRequests[i]; | ||||||||
| const job = this.#loader.getModuleJobForRequire(specifier, url, attributes); | ||||||||
| specifiers[i] = specifier; | ||||||||
| modules[i] = job.module; | ||||||||
| jobs[i] = job; | ||||||||
|
|
||||||||
| assert(this.module instanceof ModuleWrap); | ||||||||
| // Store itself into the cache first before linking in case there are circular | ||||||||
| // references in the linking. | ||||||||
| loader.loadCache.set(url, importAttributes.type, this); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting in cache at node/lib/internal/modules/esm/loader.js Lines 405 to 407 in fe4e569
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was what I did initially but it would cache modules that failed to link and if that’s caught, subsequent attempts would not fail as expected - which is why I added comments explaining why it’s reset in the finally block (doing it inside the linking phase prevents a rethrow in the caller which looks less nice) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, maybe we could transform the try-finally into a try-catch-rethrow and only delete in catch? In this way we don't need to set cache again in the caller. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rethrow was exactly what I am trying to prevent - setting the cache again is fine as the overhead is trivial but having to rethrow in all callers of this makes the code harder to read IMO. It also makes all the source lines of the errors less readable, which shows in the stderr when they are not caught. |
||||||||
|
|
||||||||
| try { | ||||||||
| const moduleRequests = this.module.getModuleRequests(); | ||||||||
| // Specifiers should be aligned with the moduleRequests array in order. | ||||||||
| const specifiers = Array(moduleRequests.length); | ||||||||
| const modules = Array(moduleRequests.length); | ||||||||
| const jobs = Array(moduleRequests.length); | ||||||||
| for (let i = 0; i < moduleRequests.length; ++i) { | ||||||||
| const { specifier, attributes } = moduleRequests[i]; | ||||||||
| const job = this.#loader.getModuleJobForRequire(specifier, url, attributes); | ||||||||
| specifiers[i] = specifier; | ||||||||
| modules[i] = job.module; | ||||||||
| jobs[i] = job; | ||||||||
| } | ||||||||
| this.module.link(specifiers, modules); | ||||||||
| this.linked = jobs; | ||||||||
| } finally { | ||||||||
| // Restore it - if it succeeds, we'll reset in the caller; Otherwise it's | ||||||||
| // not cached and if the error is caught, subsequent attempt would still fail. | ||||||||
| loader.loadCache.delete(url, importAttributes.type); | ||||||||
| } | ||||||||
| this.module.link(specifiers, modules); | ||||||||
| this.linked = jobs; | ||||||||
| } | ||||||||
|
|
||||||||
| get modulePromise() { | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Flags: --experimental-require-module | ||
| 'use strict'; | ||
|
|
||
| // This tests that ESM <-> ESM cycle is allowed in a require()-d graph. | ||
| const common = require('../common'); | ||
| const cycle = require('../fixtures/es-modules/cjs-esm-esm-cycle/c.cjs'); | ||
|
|
||
| common.expectRequiredModule(cycle, { b: 5 }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { b } from './b.mjs'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import './a.mjs' | ||
| export const b = 5; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require('./a.mjs'); |
Uh oh!
There was an error while loading. Please reload this page.