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

fix absolute path preloads when cwd is ENOENT #2353

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-cwd-enoent-preload.js
Original file line number Diff line number Diff line change
@@ -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);
}));