From d1c27b2e29e74a148dfc026015384b776901d2e0 Mon Sep 17 00:00:00 2001 From: Bradley Meck Date: Tue, 11 Aug 2015 20:25:49 -0400 Subject: [PATCH] module: fix module preloading when cwd is ENOENT Fixes a regression from 5759722cfacf17cc79651c81801a5e03521db053 that prevented modules from being preloaded if the cwd does not exist. Absolute and builtin modules now preload correctly again. Refs: https://github.com/nodejs/node/issues/1803 PR-URL: https://github.com/nodejs/node/pull/2353 Reviewed-By: Jeremiah Senkpiel --- lib/module.js | 11 ++++++++-- test/parallel/test-cwd-enoent-preload.js | 28 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-cwd-enoent-preload.js diff --git a/lib/module.js b/lib/module.js index fa7d5ebcda711e..aaa6220e40a25e 100644 --- a/lib/module.js +++ b/lib/module.js @@ -515,9 +515,16 @@ Module._preloadModules = function(requests) { // in the current working directory. This seeds the search path for // preloaded modules. var parent = new Module('internal/preload', null); - parent.paths = Module._nodeModulePaths(process.cwd()); + try { + parent.paths = Module._nodeModulePaths(process.cwd()); + } + catch (e) { + if (e.code !== 'ENOENT') { + throw e; + } + } requests.forEach(function(request) { - Module._load(request, parent, false); + parent.require(request); }); }; diff --git a/test/parallel/test-cwd-enoent-preload.js b/test/parallel/test-cwd-enoent-preload.js new file mode 100644 index 00000000000000..2dde3f0764b0a4 --- /dev/null +++ b/test/parallel/test-cwd-enoent-preload.js @@ -0,0 +1,28 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const spawn = require('child_process').spawn; + +// Fails with EINVAL on SmartOS, EBUSY on Windows. +if (process.platform === 'sunos' || common.isWindows) { + console.log('1..0 # Skipped: cannot rmdir current working directory'); + return; +} + +const dirname = common.tmpDir + '/cwd-does-not-exist-' + process.pid; +const abspathFile = require('path').join(common.fixturesDir, 'a.js'); +common.refreshTmpDir(); +fs.mkdirSync(dirname); +process.chdir(dirname); +fs.rmdirSync(dirname); + + +const proc = spawn(process.execPath, ['-r', abspathFile, '-e', '0']); +proc.stdout.pipe(process.stdout); +proc.stderr.pipe(process.stderr); + +proc.once('exit', common.mustCall(function(exitCode, signalCode) { + assert.strictEqual(exitCode, 0); + assert.strictEqual(signalCode, null); +}));