Skip to content

Commit 2c6f79c

Browse files
committed
src: don't error at startup when cwd doesn't exist
The current working directory may not exist when iojs starts up. Don't treat that as an error because it's still possible to do many useful things, like evaluating a command line script or starting a REPL. This commit also fixes an age-old Windows bug where process.argv[0] was not properly expanded, that's why the parallel/test-process-argv-0 test gets an update as well. Fixes: nodejs#1184 PR-URL: nodejs#1194 Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Rod Vagg <rod@vagg.org>
1 parent c15e81a commit 2c6f79c

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

src/node.js

+10-19
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
startup.processRawDebug();
4545

46-
startup.resolveArgv0();
46+
process.argv[0] = process.execPath;
4747

4848
// There are various modes that Node can run in. The most common two
4949
// are running from a script and running the REPL - but there are a few
@@ -459,7 +459,15 @@
459459
function evalScript(name) {
460460
var Module = NativeModule.require('module');
461461
var path = NativeModule.require('path');
462-
var cwd = process.cwd();
462+
463+
try {
464+
var cwd = process.cwd();
465+
} catch (e) {
466+
// getcwd(3) can fail if the current working directory has been deleted.
467+
// Fall back to the directory name of the (absolute) executable path.
468+
// It's not really correct but what are the alternatives?
469+
var cwd = path.dirname(process.execPath);
470+
}
463471

464472
var module = new Module(name);
465473
module.filename = path.join(cwd, name);
@@ -764,23 +772,6 @@
764772
};
765773
};
766774

767-
768-
startup.resolveArgv0 = function() {
769-
var cwd = process.cwd();
770-
var isWindows = process.platform === 'win32';
771-
772-
// Make process.argv[0] into a full path, but only touch argv[0] if it's
773-
// not a system $PATH lookup.
774-
// TODO: Make this work on Windows as well. Note that "node" might
775-
// execute cwd\node.exe, or some %PATH%\node.exe on Windows,
776-
// and that every directory has its own cwd, so d:node.exe is valid.
777-
var argv0 = process.argv[0];
778-
if (!isWindows && argv0.indexOf('/') !== -1 && argv0.charAt(0) !== '/') {
779-
var path = NativeModule.require('path');
780-
process.argv[0] = path.join(cwd, process.argv[0]);
781-
}
782-
};
783-
784775
// Below you find a minimal module system, which is used to load the node
785776
// core modules found in lib/*.js. All core modules are compiled into the
786777
// node binary, so they can be loaded faster.

test/parallel/test-cwd-enoent.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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, ['-e', '0']);
18+
proc.stdout.pipe(process.stdout);
19+
proc.stderr.pipe(process.stderr);
20+
21+
proc.once('exit', common.mustCall(function(exitCode, signalCode) {
22+
assert.equal(exitCode, 0);
23+
assert.equal(signalCode, null);
24+
}));

test/parallel/test-process-argv-0.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ if (process.argv[2] !== "child") {
2222
});
2323
child.on('exit', function () {
2424
console.error('CHILD: %s', childErr.trim().split('\n').join('\nCHILD: '));
25-
if (process.platform === 'win32') {
26-
// On Windows argv[0] is not expanded into full path
27-
assert.equal(childArgv0, './iojs');
28-
} else {
29-
assert.equal(childArgv0, process.execPath);
30-
}
25+
assert.equal(childArgv0, process.execPath);
3126
});
3227
}
3328
else {

0 commit comments

Comments
 (0)