From f758d32523d02101e879a094f885c2edda96abeb Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Thu, 20 Oct 2016 15:38:54 -0400 Subject: [PATCH 1/2] tests: add test for loading from global folders Test executes with a copy of the node executable since $PREFIX/lib/node is relative to the executable location. --- .../home-pkg-in-both/.node_libraries/foo.js | 1 + .../home-pkg-in-both/.node_modules/foo.js | 1 + .../.node_libraries/foo.js | 1 + .../.node_modules/foo.js | 1 + .../local-pkg/node_modules/foo.js | 1 + .../local-pkg/test.js | 2 + .../node_path/foo.js | 1 + .../test-module-loading-globalpaths.js | 101 ++++++++++++++++++ 8 files changed, 109 insertions(+) create mode 100644 test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_libraries/foo.js create mode 100644 test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_modules/foo.js create mode 100644 test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_libraries/.node_libraries/foo.js create mode 100644 test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_modules/.node_modules/foo.js create mode 100644 test/fixtures/test-module-loading-globalpaths/local-pkg/node_modules/foo.js create mode 100644 test/fixtures/test-module-loading-globalpaths/local-pkg/test.js create mode 100644 test/fixtures/test-module-loading-globalpaths/node_path/foo.js create mode 100644 test/parallel/test-module-loading-globalpaths.js diff --git a/test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_libraries/foo.js b/test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_libraries/foo.js new file mode 100644 index 00000000000000..eb278f95762215 --- /dev/null +++ b/test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_libraries/foo.js @@ -0,0 +1 @@ +exports.string = '$HOME/.node_libraries'; diff --git a/test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_modules/foo.js b/test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_modules/foo.js new file mode 100644 index 00000000000000..8a665b3e98cfe2 --- /dev/null +++ b/test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_modules/foo.js @@ -0,0 +1 @@ +exports.string = '$HOME/.node_modules'; diff --git a/test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_libraries/.node_libraries/foo.js b/test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_libraries/.node_libraries/foo.js new file mode 100644 index 00000000000000..eb278f95762215 --- /dev/null +++ b/test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_libraries/.node_libraries/foo.js @@ -0,0 +1 @@ +exports.string = '$HOME/.node_libraries'; diff --git a/test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_modules/.node_modules/foo.js b/test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_modules/.node_modules/foo.js new file mode 100644 index 00000000000000..8a665b3e98cfe2 --- /dev/null +++ b/test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_modules/.node_modules/foo.js @@ -0,0 +1 @@ +exports.string = '$HOME/.node_modules'; diff --git a/test/fixtures/test-module-loading-globalpaths/local-pkg/node_modules/foo.js b/test/fixtures/test-module-loading-globalpaths/local-pkg/node_modules/foo.js new file mode 100644 index 00000000000000..63e844e4d4060b --- /dev/null +++ b/test/fixtures/test-module-loading-globalpaths/local-pkg/node_modules/foo.js @@ -0,0 +1 @@ +exports.string = 'local'; diff --git a/test/fixtures/test-module-loading-globalpaths/local-pkg/test.js b/test/fixtures/test-module-loading-globalpaths/local-pkg/test.js new file mode 100644 index 00000000000000..8054983e992ce8 --- /dev/null +++ b/test/fixtures/test-module-loading-globalpaths/local-pkg/test.js @@ -0,0 +1,2 @@ +'use strict'; +console.log(require('foo').string); diff --git a/test/fixtures/test-module-loading-globalpaths/node_path/foo.js b/test/fixtures/test-module-loading-globalpaths/node_path/foo.js new file mode 100644 index 00000000000000..3ce43c49206f73 --- /dev/null +++ b/test/fixtures/test-module-loading-globalpaths/node_path/foo.js @@ -0,0 +1 @@ +exports.string = '$NODE_PATH'; diff --git a/test/parallel/test-module-loading-globalpaths.js b/test/parallel/test-module-loading-globalpaths.js new file mode 100644 index 00000000000000..d789f5409901ba --- /dev/null +++ b/test/parallel/test-module-loading-globalpaths.js @@ -0,0 +1,101 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); +const fs = require('fs'); +const child_process = require('child_process'); +const pkgName = 'foo'; + +if (process.argv[2] === 'child') { + console.log(require(pkgName).string); +} else { + common.refreshTmpDir(); + + // Copy node binary into a test $PREFIX directory. + const prefixPath = path.join(common.tmpDir, 'install'); + fs.mkdirSync(prefixPath); + let testExecPath; + if (common.isWindows) { + testExecPath = path.join(prefixPath, path.basename(process.execPath)); + } else { + const prefixBinPath = path.join(prefixPath, 'bin'); + fs.mkdirSync(prefixBinPath); + testExecPath = path.join(prefixBinPath, path.basename(process.execPath)); + } + const mode = fs.statSync(process.execPath).mode; + fs.writeFileSync(testExecPath, fs.readFileSync(process.execPath)); + fs.chmodSync(testExecPath, mode); + + const runTest = (expectedString, env) => { + const child = child_process.execFileSync(testExecPath, + [ __filename, 'child' ], + { encoding: 'utf8', env: env }); + assert.strictEqual(child.trim(), expectedString); + }; + + const testFixturesDir = path.join(common.fixturesDir, + path.basename(__filename, '.js')); + + const env = Object.assign({}, process.env); + // Turn on module debug to aid diagnosing failures. + env['NODE_DEBUG'] = 'module'; + // Unset NODE_PATH. + delete env['NODE_PATH']; + + // Test empty global path. + const noPkgHomeDir = path.join(common.tmpDir, 'home-no-pkg'); + fs.mkdirSync(noPkgHomeDir); + env['HOME'] = env['USERPROFILE'] = noPkgHomeDir; + assert.throws( + () => { + child_process.execFileSync(testExecPath, [ __filename, 'child' ], + { encoding: 'utf8', env: env }); + }, + new RegExp('Cannot find module \'' + pkgName + '\'')); + + // Test module in $HOME/.node_modules. + const modHomeDir = path.join(testFixturesDir, 'home-pkg-in-node_modules'); + env['HOME'] = env['USERPROFILE'] = modHomeDir; + runTest('$HOME/.node_modules', env); + + // Test module in $HOME/.node_libraries. + const libHomeDir = path.join(testFixturesDir, 'home-pkg-in-node_libraries'); + env['HOME'] = env['USERPROFILE'] = libHomeDir; + runTest('$HOME/.node_libraries', env); + + // Test module both $HOME/.node_modules and $HOME/.node_libraries. + const bothHomeDir = path.join(testFixturesDir, 'home-pkg-in-both'); + env['HOME'] = env['USERPROFILE'] = bothHomeDir; + runTest('$HOME/.node_modules', env); + + // Test module in $PREFIX/lib/node. + // Write module into $PREFIX/lib/node. + const expectedString = '$PREFIX/lib/node'; + const prefixLibPath = path.join(prefixPath, 'lib'); + fs.mkdirSync(prefixLibPath); + const prefixLibNodePath = path.join(prefixLibPath, 'node'); + fs.mkdirSync(prefixLibNodePath); + const pkgPath = path.join(prefixLibNodePath, pkgName + '.js'); + fs.writeFileSync(pkgPath, 'exports.string = \'' + expectedString + '\';'); + + env['HOME'] = env['USERPROFILE'] = noPkgHomeDir; + runTest(expectedString, env); + + // Test module in all global folders. + env['HOME'] = env['USERPROFILE'] = bothHomeDir; + runTest('$HOME/.node_modules', env); + + // Test module in NODE_PATH is loaded ahead of global folders. + env['HOME'] = env['USERPROFILE'] = bothHomeDir; + env['NODE_PATH'] = path.join(testFixturesDir, 'node_path'); + runTest('$NODE_PATH', env); + + // Test module in local folder is loaded ahead of global folders. + const localDir = path.join(testFixturesDir, 'local-pkg'); + env['HOME'] = env['USERPROFILE'] = bothHomeDir; + env['NODE_PATH'] = path.join(testFixturesDir, 'node_path'); + const child = child_process.execFileSync(testExecPath, + [ path.join(localDir, 'test.js') ], + { encoding: 'utf8', env: env }); + assert.strictEqual(child.trim(), 'local'); +} From 9451ae6a03b66d0a6592b09da291c9e463303b55 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Tue, 25 Oct 2016 23:58:06 +0100 Subject: [PATCH 2/2] module: fix loading from global folders on Windows Code was calculating $PREFIX/lib/node relative to process.execPath, but on Windows process.execPath is $PREFIX\node.exe whereas everywhere else process.execPath is $PREFIX/bin/node (where $PREFIX is the root of the installed Node.js). --- lib/module.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/module.js b/lib/module.js index 9a6b641a753588..b965bbd8dae73f 100644 --- a/lib/module.js +++ b/lib/module.js @@ -617,7 +617,16 @@ Module._initPaths = function() { homeDir = process.env.HOME; } - var paths = [path.resolve(process.execPath, '..', '..', 'lib', 'node')]; + // $PREFIX/lib/node, where $PREFIX is the root of the Node.js installation. + var prefixDir; + // process.execPath is $PREFIX/bin/node except on Windows where it is + // $PREFIX\node.exe. + if (isWindows) { + prefixDir = path.resolve(process.execPath, '..'); + } else { + prefixDir = path.resolve(process.execPath, '..', '..'); + } + var paths = [path.resolve(prefixDir, 'lib', 'node')]; if (homeDir) { paths.unshift(path.resolve(homeDir, '.node_libraries'));