Skip to content

Commit d1c27b2

Browse files
bmeckFishrock123
authored andcommitted
module: fix module preloading when cwd is ENOENT
Fixes a regression from 5759722 that prevented modules from being preloaded if the cwd does not exist. Absolute and builtin modules now preload correctly again. Refs: #1803 PR-URL: #2353 Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 0ea5c8d commit d1c27b2

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/module.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,16 @@ Module._preloadModules = function(requests) {
515515
// in the current working directory. This seeds the search path for
516516
// preloaded modules.
517517
var parent = new Module('internal/preload', null);
518-
parent.paths = Module._nodeModulePaths(process.cwd());
518+
try {
519+
parent.paths = Module._nodeModulePaths(process.cwd());
520+
}
521+
catch (e) {
522+
if (e.code !== 'ENOENT') {
523+
throw e;
524+
}
525+
}
519526
requests.forEach(function(request) {
520-
Module._load(request, parent, false);
527+
parent.require(request);
521528
});
522529
};
523530

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const spawn = require('child_process').spawn;
6+
7+
// Fails with EINVAL on SmartOS, EBUSY on Windows.
8+
if (process.platform === 'sunos' || common.isWindows) {
9+
console.log('1..0 # Skipped: cannot rmdir current working directory');
10+
return;
11+
}
12+
13+
const dirname = common.tmpDir + '/cwd-does-not-exist-' + process.pid;
14+
const abspathFile = require('path').join(common.fixturesDir, 'a.js');
15+
common.refreshTmpDir();
16+
fs.mkdirSync(dirname);
17+
process.chdir(dirname);
18+
fs.rmdirSync(dirname);
19+
20+
21+
const proc = spawn(process.execPath, ['-r', abspathFile, '-e', '0']);
22+
proc.stdout.pipe(process.stdout);
23+
proc.stderr.pipe(process.stderr);
24+
25+
proc.once('exit', common.mustCall(function(exitCode, signalCode) {
26+
assert.strictEqual(exitCode, 0);
27+
assert.strictEqual(signalCode, null);
28+
}));

0 commit comments

Comments
 (0)