From 2b2e48a4b91a4b636ae0e603968bd764697d1e57 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 18 Mar 2015 22:31:16 +0100 Subject: [PATCH] lib: don't error in repl when cwd doesn't exist MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The current working directory may not exist when the REPL starts up. Don't treat that as an error because it's still possible to do many useful things. This is like the previous commit but for the REPL. Fixes: https://github.com/iojs/io.js/issues/1184 PR-URL: https://github.com/iojs/io.js/pull/1194 Reviewed-By: Johan Bergström Reviewed-By: Rod Vagg --- lib/repl.js | 12 ++++++++++-- test/parallel/test-cwd-enoent-repl.js | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-cwd-enoent-repl.js diff --git a/lib/repl.js b/lib/repl.js index 0a63f32..036b561 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -40,8 +40,16 @@ function hasOwnProperty(obj, prop) { } -// hack for require.resolve("./relative") to work properly. -module.filename = path.resolve('repl'); +try { + // hack for require.resolve("./relative") to work properly. + module.filename = path.resolve('repl'); +} catch (e) { + // path.resolve('repl') fails when the current working directory has been + // deleted. Fall back to the directory name of the (absolute) executable + // path. It's not really correct but what are the alternatives? + const dirname = path.dirname(process.execPath); + module.filename = path.resolve(dirname, 'repl'); +} // hack for repl require to work properly with node_modules folders module.paths = require('module')._nodeModulePaths(module.filename); diff --git a/test/parallel/test-cwd-enoent-repl.js b/test/parallel/test-cwd-enoent-repl.js new file mode 100644 index 0000000..64538f8 --- /dev/null +++ b/test/parallel/test-cwd-enoent-repl.js @@ -0,0 +1,26 @@ +var common = require('../common'); +var assert = require('assert'); +var fs = require('fs'); +var spawn = require('child_process').spawn; + +// Fails with EINVAL on SmartOS, EBUSY on Windows. +if (process.platform === 'sunos' || process.platform === 'win32') { + console.log('1..0 # Skipped: cannot rmdir current working directory'); + return; +} + +var dirname = common.tmpDir + '/cwd-does-not-exist-' + process.pid; +fs.mkdirSync(dirname); +process.chdir(dirname); +fs.rmdirSync(dirname); + +var proc = spawn(process.execPath, ['--interactive']); +proc.stdout.pipe(process.stdout); +proc.stderr.pipe(process.stderr); +proc.stdin.write('require("path");\n'); +proc.stdin.write('process.exit(42);\n'); + +proc.once('exit', common.mustCall(function(exitCode, signalCode) { + assert.equal(exitCode, 42); + assert.equal(signalCode, null); +})); -- 2.7.4