node/test/es-module/test-require-module-cycle-esm-esm-cjs-esm.js
Joyee Cheung db1746182b
module: disallow CJS <-> ESM edges in a cycle from require(esm)
This patch disallows CJS <-> ESM edges when they come from
require(esm) requested in ESM evalaution.

Drive-by: don't reuse the cache for imported CJS modules to stash
source code of required ESM because the former is also used for
cycle detection.

PR-URL: https://github.com/nodejs/node/pull/52264
Fixes: https://github.com/nodejs/node/issues/52145
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-04-08 14:45:55 +00:00

83 lines
1.8 KiB
JavaScript

'use strict';
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const fixtures = require('../common/fixtures');
const assert = require('assert');
// a.mjs -> b.mjs -> c.mjs -> d.mjs -> c.mjs
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
fixtures.path('es-modules/esm-esm-cjs-esm-cycle/a.mjs'),
],
{
signal: null,
status: 0,
trim: true,
stdout(output) {
assert.match(output, /Start c/);
assert.match(output, /dynamic import b\.mjs failed.*ERR_REQUIRE_CYCLE_MODULE/);
assert.match(output, /dynamic import d\.mjs failed.*ERR_REQUIRE_CYCLE_MODULE/);
}
}
);
}
// b.mjs -> c.mjs -> d.mjs -> c.mjs
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
fixtures.path('es-modules/esm-esm-cjs-esm-cycle/b.mjs'),
],
{
signal: null,
status: 1,
trim: true,
stdout: /Start c/,
stderr: /Cannot import Module \.\/c\.mjs in a cycle\. \(from .*d\.mjs\)/,
}
);
}
// c.mjs -> d.mjs -> c.mjs
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
fixtures.path('es-modules/esm-esm-cjs-esm-cycle/c.mjs'),
],
{
signal: null,
status: 1,
trim: true,
stdout: /Start c/,
stderr: /Cannot import Module \.\/c\.mjs in a cycle\. \(from .*d\.mjs\)/,
}
);
}
// d.mjs -> c.mjs -> d.mjs
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
fixtures.path('es-modules/esm-esm-cjs-esm-cycle/d.mjs'),
],
{
signal: null,
status: 1,
trim: true,
stdout: /Start c/,
stderr: /Cannot require\(\) ES Module .*d\.mjs in a cycle\. \(from .*c\.mjs\)/,
}
);
}