Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: don't error at startup when cwd doesn't exist #1194

Merged
merged 2 commits into from
Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 10 additions & 19 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

startup.processRawDebug();

startup.resolveArgv0();
process.argv[0] = process.execPath;

// There are various modes that Node can run in. The most common two
// are running from a script and running the REPL - but there are a few
Expand Down Expand Up @@ -459,7 +459,15 @@
function evalScript(name) {
var Module = NativeModule.require('module');
var path = NativeModule.require('path');
var cwd = process.cwd();

try {
var cwd = process.cwd();
} catch (e) {
// getcwd(3) can fail if 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?
var cwd = path.dirname(process.execPath);
}

var module = new Module(name);
module.filename = path.join(cwd, name);
Expand Down Expand Up @@ -764,23 +772,6 @@
};
};


startup.resolveArgv0 = function() {
var cwd = process.cwd();
var isWindows = process.platform === 'win32';

// Make process.argv[0] into a full path, but only touch argv[0] if it's
// not a system $PATH lookup.
// TODO: Make this work on Windows as well. Note that "node" might
// execute cwd\node.exe, or some %PATH%\node.exe on Windows,
// and that every directory has its own cwd, so d:node.exe is valid.
var argv0 = process.argv[0];
if (!isWindows && argv0.indexOf('/') !== -1 && argv0.charAt(0) !== '/') {
var path = NativeModule.require('path');
process.argv[0] = path.join(cwd, process.argv[0]);
}
};

// Below you find a minimal module system, which is used to load the node
// core modules found in lib/*.js. All core modules are compiled into the
// node binary, so they can be loaded faster.
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-cwd-enoent-repl.js
Original file line number Diff line number Diff line change
@@ -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);
}));
24 changes: 24 additions & 0 deletions test/parallel/test-cwd-enoent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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, ['-e', '0']);
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);

proc.once('exit', common.mustCall(function(exitCode, signalCode) {
assert.equal(exitCode, 0);
assert.equal(signalCode, null);
}));
7 changes: 1 addition & 6 deletions test/parallel/test-process-argv-0.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ if (process.argv[2] !== "child") {
});
child.on('exit', function () {
console.error('CHILD: %s', childErr.trim().split('\n').join('\nCHILD: '));
if (process.platform === 'win32') {
// On Windows argv[0] is not expanded into full path
assert.equal(childArgv0, './iojs');
} else {
assert.equal(childArgv0, process.execPath);
}
assert.equal(childArgv0, process.execPath);
});
}
else {
Expand Down