node icon indicating copy to clipboard operation
node copied to clipboard

Experimental require of circular ESM graph stack overflow

Open laverdet opened this issue 9 months ago • 3 comments

Version

v22.1.0

Platform

macos

Subsystem

modules

What steps will reproduce the bug?

$ cat left.mjs 
import {} from './right.mjs';

$ cat right.mjs 
import {} from './left.mjs';

$ node left.mjs
# (nothing)

$ cat main.cjs 
require('./left.mjs');

$ node --experimental-require-module main.cjs
node:fs:441
function readFileSync(path, options) {
                     ^

RangeError: Maximum call stack size exceeded
    at readFileSync (node:fs:441:22)
    at getSourceSync (node:internal/modules/esm/load:85:14)
    at defaultLoadSync (node:internal/modules/esm/load:201:32)
    at ModuleLoader.getModuleJobForRequire (node:internal/modules/esm/loader:363:24)
    at new ModuleJobSync (node:internal/modules/esm/module_job:307:32)
    at ModuleLoader.getModuleJobForRequire (node:internal/modules/esm/loader:404:11)
    at new ModuleJobSync (node:internal/modules/esm/module_job:307:32)
    at ModuleLoader.getModuleJobForRequire (node:internal/modules/esm/loader:404:11)
    at new ModuleJobSync (node:internal/modules/esm/module_job:307:32)
    at ModuleLoader.getModuleJobForRequire (node:internal/modules/esm/loader:404:11)

Node.js v22.1.0

How often does it reproduce? Is there a required condition?

It reproduces consistently.

What is the expected behavior? Why is that the expected behavior?

Circular imports are well-defined in the specification and should work correctly.

What do you see instead?

Stack overflow

Additional information

No response

laverdet avatar May 06 '24 20:05 laverdet

@joyeecheung #51977 thanks!

laverdet avatar May 06 '24 20:05 laverdet

@nodejs/loaders

GeoffreyBooth avatar May 06 '24 21:05 GeoffreyBooth

Thanks for reporting it, fix in https://github.com/nodejs/node/pull/52868

joyeecheung avatar May 07 '24 00:05 joyeecheung