diff --git a/cli.js b/cli.js index 30941d41d..09b56aa9f 100755 --- a/cli.js +++ b/cli.js @@ -17,6 +17,7 @@ if (debug.enabled) { require('time-require'); } +var path = require('path'); var updateNotifier = require('update-notifier'); var figures = require('figures'); var arrify = require('arrify'); @@ -93,12 +94,14 @@ if (cli.flags.init) { return; } -var additionalPaths = []; +var nodePaths; if (process.env.NODE_PATH) { - var osSplitChar = process.platform === 'win32' ? ';' : ':'; - process.env.NODE_PATH.split(osSplitChar).forEach(function (additionalPath) { - additionalPaths.push(path.resolve(opts.projectRoot, additionalPath)); - }); + var osSplitChar = process.platform === 'win32' ? ';' : ':'; + nodePaths = process.env.NODE_PATH.split(osSplitChar).map(function (p) { + return path.resolve(process.cwd(), p) + }); +} else { + nodePaths = [] } var api = new Api(cli.input.length ? cli.input : arrify(conf.files), { @@ -106,7 +109,7 @@ var api = new Api(cli.input.length ? cli.input : arrify(conf.files), { serial: cli.flags.serial, require: arrify(cli.flags.require), cacheEnabled: cli.flags.cache !== false, - additionalPaths: additionalPaths + nodePaths: nodePaths }); var logger = new Logger(); diff --git a/lib/fork.js b/lib/fork.js index 0a4e830fb..b9bc5cfaf 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -13,8 +13,7 @@ module.exports = function (file, opts) { tty: process.stdout.isTTY ? { columns: process.stdout.columns, rows: process.stdout.rows - } : false, - additionalPaths: opts.additionalPaths + } : false }, opts); var ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], { diff --git a/lib/test-worker.js b/lib/test-worker.js index 45f4199ec..0b10e5596 100644 --- a/lib/test-worker.js +++ b/lib/test-worker.js @@ -86,10 +86,8 @@ var oldNodeModulesPaths = module.constructor._nodeModulePaths; module.constructor._nodeModulePaths = function () { var ret = oldNodeModulesPaths.apply(this, arguments); ret.push(nodeModulesDir); - if (opts.additionalPaths && opts.additionalPaths.length > 0) { - opts.additionalPaths.forEach(function (additionalPath) { - ret.push(additionalPath); - }); + if (opts.nodePaths && opts.nodePaths.length > 0) { + ret = ret.concat(opts.nodePaths); } return ret; }; diff --git a/test/cli.js b/test/cli.js index cadb0ad7b..3d786220a 100644 --- a/test/cli.js +++ b/test/cli.js @@ -15,7 +15,10 @@ function execCli(args, dirname, cb) { dirname = path.join(__dirname, dirname); } - var env = {}; + var env = { + // This probably should be set only for the corresponding test + NODE_PATH: 'node-paths/modules' + }; if (process.env.AVA_APPVEYOR) { env.AVA_APPVEYOR = 1; @@ -110,3 +113,10 @@ test('pkg-conf: cli takes precedence', function (t) { t.end(); }); }); + +test('handles NODE_PATH', function (t) { + execCli('fixture/node-paths.js', function (err) { + t.notOk(err); + t.end(); + }); +}); diff --git a/test/fixture/node-paths.js b/test/fixture/node-paths.js new file mode 100644 index 000000000..743625818 --- /dev/null +++ b/test/fixture/node-paths.js @@ -0,0 +1,7 @@ +import test from '../../'; + +import foo from 'nested/foo'; + +test('relative require', t => { + t.is(foo(), 'bar'); +}); diff --git a/test/fixture/node-paths/modules/nested/foo.js b/test/fixture/node-paths/modules/nested/foo.js new file mode 100644 index 000000000..1723f5abd --- /dev/null +++ b/test/fixture/node-paths/modules/nested/foo.js @@ -0,0 +1,3 @@ +module.exports = function() { + return 'bar'; +}