Skip to content
/ node Public
forked from nodejs/node

Commit fee4bc4

Browse files
committed
vm: fix link with invalid graph
This fixes an annoying bug where the entry module would link before its dependencies were linked. This is fixed by forcing modules to wait for all dependency linker promises to resolve. Fixes: nodejs#37426 Signed-off-by: snek <[email protected]>
1 parent 3b2863d commit fee4bc4

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

lib/internal/vm/module.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ const kNoError = Symbol('kNoError');
255255
class SourceTextModule extends Module {
256256
#error = kNoError;
257257
#statusOverride;
258+
#waitingLinkPromises;
258259

259260
constructor(sourceText, options = {}) {
260261
validateString(sourceText, 'sourceText');
@@ -308,31 +309,37 @@ class SourceTextModule extends Module {
308309
this[kLink] = async (linker) => {
309310
this.#statusOverride = 'linking';
310311

311-
const promises = this[kWrap].link(async (identifier) => {
312-
const module = await linker(identifier, this);
313-
if (module[kWrap] === undefined) {
314-
throw new ERR_VM_MODULE_NOT_MODULE();
315-
}
316-
if (module.context !== this.context) {
317-
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
318-
}
319-
if (module.status === 'errored') {
320-
throw new ERR_VM_MODULE_LINKING_ERRORED();
321-
}
322-
if (module.status === 'unlinked') {
323-
await module[kLink](linker);
324-
}
325-
return module[kWrap];
326-
});
327-
328312
try {
313+
const promises = this[kWrap].link(async (identifier) => {
314+
const module = await linker(identifier, this);
315+
if (module[kWrap] === undefined) {
316+
throw new ERR_VM_MODULE_NOT_MODULE();
317+
}
318+
if (module.context !== this.context) {
319+
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
320+
}
321+
if (module.status === 'errored') {
322+
throw new ERR_VM_MODULE_LINKING_ERRORED();
323+
}
324+
if (module.status === 'unlinked') {
325+
await module[kLink](linker);
326+
}
327+
if (module.status === 'linking') {
328+
await module.#waitingLinkPromises;
329+
}
330+
return module[kWrap];
331+
});
332+
329333
if (promises !== undefined) {
330-
await PromiseAll(promises);
334+
assert(this.#waitingLinkPromises === undefined);
335+
this.#waitingLinkPromises = PromiseAll(promises);
331336
}
337+
await this.#waitingLinkPromises;
332338
} catch (e) {
333339
this.#error = e;
334340
throw e;
335341
} finally {
342+
this.#waitingLinkPromises = undefined;
336343
this.#statusOverride = undefined;
337344
}
338345
};

test.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const { SourceTextModule, SyntheticModule } = require('vm');
4+
5+
const tm = new SourceTextModule('import "a"');
6+
7+
tm
8+
.link(async () => {
9+
const tm2 = new SourceTextModule('import "b"');
10+
tm2.link(async () => {
11+
const sm = new SyntheticModule([], () => {});
12+
await sm.link(() => {});
13+
await sm.evaluate();
14+
return sm;
15+
}).then(() => tm2.evaluate());
16+
return tm2;
17+
})
18+
.then(() => tm.evaluate())
19+
.then(() => {
20+
console.log('dun');
21+
})
22+
.catch((e) => {
23+
console.error(e);
24+
});
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Flags: --experimental-vm-modules
2+
3+
'use strict';
4+
5+
const common = require('../common');
6+
const { SourceTextModule, SyntheticModule } = require('vm');
7+
8+
const tm = new SourceTextModule('import "a"');
9+
10+
tm
11+
.link(async () => {
12+
const tm2 = new SourceTextModule('import "b"');
13+
tm2.link(async () => {
14+
const sm = new SyntheticModule([], () => {});
15+
await sm.link(() => {});
16+
await sm.evaluate();
17+
return sm;
18+
}).then(() => tm2.evaluate());
19+
return tm2;
20+
})
21+
.then(() => tm.evaluate())
22+
.then(common.mustCall())
23+
.catch(common.mustNotCall());

0 commit comments

Comments
 (0)