Skip to content
/ node Public
forked from nodejs/node

Commit

Permalink
vm: fix link with invalid graph
Browse files Browse the repository at this point in the history
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 <me@gus.host>
  • Loading branch information
devsnek committed Mar 4, 2021
1 parent 3b2863d commit a348609
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
43 changes: 25 additions & 18 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ const kNoError = Symbol('kNoError');
class SourceTextModule extends Module {
#error = kNoError;
#statusOverride;
#waitingLinkPromises;

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

const promises = this[kWrap].link(async (identifier) => {
const module = await linker(identifier, this);
if (module[kWrap] === undefined) {
throw new ERR_VM_MODULE_NOT_MODULE();
}
if (module.context !== this.context) {
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
}
if (module.status === 'errored') {
throw new ERR_VM_MODULE_LINKING_ERRORED();
}
if (module.status === 'unlinked') {
await module[kLink](linker);
}
return module[kWrap];
});

try {
const promises = this[kWrap].link(async (identifier) => {
const module = await linker(identifier, this);
if (module[kWrap] === undefined) {
throw new ERR_VM_MODULE_NOT_MODULE();
}
if (module.context !== this.context) {
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
}
if (module.status === 'errored') {
throw new ERR_VM_MODULE_LINKING_ERRORED();
}
if (module.status === 'unlinked') {
await module[kLink](linker);
}
if (module.status === 'linking') {
await module.#waitingLinkPromises;
}
return module[kWrap];
});

if (promises !== undefined) {
await PromiseAll(promises);
assert(this.#waitingLinkPromises === undefined);
this.#waitingLinkPromises = PromiseAll(promises);
}
await this.#waitingLinkPromises;
} catch (e) {
this.#error = e;
throw e;
} finally {
this.#waitingLinkPromises = undefined;
this.#statusOverride = undefined;
}
};
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-vm-module-link-37426.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Flags: --experimental-vm-modules

'use strict';

const common = require('../common');
const { SourceTextModule, SyntheticModule } = require('vm');

const tm = new SourceTextModule('import "a"');

tm
.link(async () => {
const tm2 = new SourceTextModule('import "b"');
tm2.link(async () => {
const sm = new SyntheticModule([], () => {});
await sm.link(() => {});
await sm.evaluate();
return sm;
}).then(() => tm2.evaluate());
return tm2;
})
.then(() => tm.evaluate())
.then(common.mustCall())
.catch(common.mustNotCall());

0 comments on commit a348609

Please sign in to comment.