module: self referential modules in repl or `-r` · nodejs/node@00aa935 · GitHub
Skip to content

Commit 00aa935

Browse files
dnlupcodebytere
authored andcommitted
module: self referential modules in repl or -r
Load self referential modules from the repl and using the preload flag `-r`. In both cases the base path used for resolution is the current `process.cwd()`. Also fixes an internal cycle bug in the REPL exports resolution. PR-URL: #32261 Backport-PR-URL: #35385 Fixes: #31595 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com>
1 parent d065334 commit 00aa935

7 files changed

Lines changed: 94 additions & 8 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 27 additions & 7 deletions

lib/internal/modules/esm/loader.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
// This is needed to avoid cycles in esm/resolve <-> cjs/loader
4+
require('internal/modules/cjs/loader');
5+
36
const {
47
FunctionPrototypeBind,
58
ObjectSetPrototypeOf,

lib/internal/modules/esm/resolve.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const {
3232
} = require('fs');
3333
const { getOptionValue } = require('internal/options');
3434
const { sep, relative } = require('path');
35-
const { Module: CJSModule } = require('internal/modules/cjs/loader');
3635
const preserveSymlinks = getOptionValue('--preserve-symlinks');
3736
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
3837
const typeFlag = getOptionValue('--input-type');
@@ -49,11 +48,13 @@ const {
4948
ERR_UNSUPPORTED_DIR_IMPORT,
5049
ERR_UNSUPPORTED_ESM_URL_SCHEME,
5150
} = require('internal/errors').codes;
51+
const { Module: CJSModule } = require('internal/modules/cjs/loader');
5252

5353
const packageJsonReader = require('internal/modules/package_json_reader');
5454
const DEFAULT_CONDITIONS = ObjectFreeze(['node', 'import']);
5555
const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS);
5656

57+
5758
function getConditionsSet(conditions) {
5859
if (conditions !== undefined && conditions !== DEFAULT_CONDITIONS) {
5960
if (!ArrayIsArray(conditions)) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict'
2+
3+
module.exports = 'Self resolution working';
4+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "self_ref",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"exports": "./index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC"
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const assert = require('assert');
6+
const { exec } = require('child_process');
7+
8+
const nodeBinary = process.argv[0];
9+
10+
if (!common.isMainThread)
11+
common.skip('process.chdir is not available in Workers');
12+
13+
const selfRefModule = fixtures.path('self_ref_module');
14+
const fixtureA = fixtures.path('printA.js');
15+
16+
exec(`"${nodeBinary}" -r self_ref "${fixtureA}"`, { cwd: selfRefModule },
17+
(err, stdout, stderr) => {
18+
assert.ifError(err);
19+
assert.strictEqual(stdout, 'A\n');
20+
});
Lines changed: 25 additions & 0 deletions

0 commit comments

Comments
 (0)