Skip to content

Commit 2b2e48a

Browse files
committed
lib: don't error in repl when cwd doesn't exist
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: #1184 PR-URL: #1194 Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Rod Vagg <rod@vagg.org>
1 parent 2c6f79c commit 2b2e48a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/repl.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ function hasOwnProperty(obj, prop) {
4040
}
4141

4242

43-
// hack for require.resolve("./relative") to work properly.
44-
module.filename = path.resolve('repl');
43+
try {
44+
// hack for require.resolve("./relative") to work properly.
45+
module.filename = path.resolve('repl');
46+
} catch (e) {
47+
// path.resolve('repl') fails when the current working directory has been
48+
// deleted. Fall back to the directory name of the (absolute) executable
49+
// path. It's not really correct but what are the alternatives?
50+
const dirname = path.dirname(process.execPath);
51+
module.filename = path.resolve(dirname, 'repl');
52+
}
4553

4654
// hack for repl require to work properly with node_modules folders
4755
module.paths = require('module')._nodeModulePaths(module.filename);

test/parallel/test-cwd-enoent-repl.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
var fs = require('fs');
4+
var spawn = require('child_process').spawn;
5+
6+
// Fails with EINVAL on SmartOS, EBUSY on Windows.
7+
if (process.platform === 'sunos' || process.platform === 'win32') {
8+
console.log('1..0 # Skipped: cannot rmdir current working directory');
9+
return;
10+
}
11+
12+
var dirname = common.tmpDir + '/cwd-does-not-exist-' + process.pid;
13+
fs.mkdirSync(dirname);
14+
process.chdir(dirname);
15+
fs.rmdirSync(dirname);
16+
17+
var proc = spawn(process.execPath, ['--interactive']);
18+
proc.stdout.pipe(process.stdout);
19+
proc.stderr.pipe(process.stderr);
20+
proc.stdin.write('require("path");\n');
21+
proc.stdin.write('process.exit(42);\n');
22+
23+
proc.once('exit', common.mustCall(function(exitCode, signalCode) {
24+
assert.equal(exitCode, 42);
25+
assert.equal(signalCode, null);
26+
}));

0 commit comments

Comments
 (0)