-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: preserve symlinks when requiring
Currently, required modules use the real location of the package/file as their __filename and __dirname, instead of the symlinked path if it exists. This behaviour is undocumented (it even goes against documentation in certain scenarios), creating hard-to-debug problems for developers who wish to leverage filesystem abstractions to lay out their application. This patch resolves all required modules to their canonical path while still preserving any symlinks within the path, instead of resolving to their canonical realpath. The one special case observed is when the main module is loaded -- in this case, the realpath does need to be used in order for the main module to load properly. PR-URL: #5950 Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
8 changed files
with
103 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exports.dep1 = require('dep1'); | ||
exports.dep2 = exports.dep1.dep2; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
test/fixtures/module-require-symlink/node_modules/dep1/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
test/fixtures/module-require-symlink/node_modules/dep1/node_modules/bar/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
test/fixtures/module-require-symlink/node_modules/dep2/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
const assert = require('assert'); | ||
const common = require('../../common'); | ||
const path = require('path'); | ||
|
||
const linkScriptTarget = path.join(common.fixturesDir, | ||
'/module-require-symlink/symlinked.js'); | ||
|
||
var foo = require('./foo'); | ||
assert.equal(foo.dep1.bar.version, 'CORRECT_VERSION'); | ||
assert.equal(foo.dep2.bar.version, 'CORRECT_VERSION'); | ||
assert.equal(__filename, linkScriptTarget); | ||
assert(__filename in require.cache); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const exec = require('child_process').exec; | ||
const spawn = require('child_process').spawn; | ||
|
||
const linkTarget = path.join(common.fixturesDir, | ||
'/module-require-symlink/node_modules/dep2/'); | ||
|
||
const linkDir = path.join(common.fixturesDir, | ||
'/module-require-symlink/node_modules/dep1/node_modules/dep2'); | ||
|
||
const linkScriptTarget = path.join(common.fixturesDir, | ||
'/module-require-symlink/symlinked.js'); | ||
|
||
const linkScript = path.join(common.tmpDir, 'module-require-symlink.js'); | ||
|
||
if (common.isWindows) { | ||
// On Windows, creating symlinks requires admin privileges. | ||
// We'll only try to run symlink test if we have enough privileges. | ||
exec('whoami /priv', function(err, o) { | ||
if (err || o.indexOf('SeCreateSymbolicLinkPrivilege') == -1) { | ||
console.log('Skipped: insufficient privileges'); | ||
return; | ||
} else { | ||
test(); | ||
} | ||
}); | ||
} else { | ||
test(); | ||
} | ||
|
||
function test() { | ||
process.on('exit', function() { | ||
fs.unlinkSync(linkDir); | ||
fs.unlinkSync(linkScript); | ||
}); | ||
|
||
fs.symlinkSync(linkTarget, linkDir); | ||
fs.symlinkSync(linkScriptTarget, linkScript); | ||
|
||
// load symlinked-module | ||
var fooModule = | ||
require(path.join(common.fixturesDir, '/module-require-symlink/foo.js')); | ||
assert.equal(fooModule.dep1.bar.version, 'CORRECT_VERSION'); | ||
assert.equal(fooModule.dep2.bar.version, 'CORRECT_VERSION'); | ||
|
||
// load symlinked-script as main | ||
var node = process.execPath; | ||
var child = spawn(node, [linkScript]); | ||
child.on('close', function(code, signal) { | ||
assert(!code); | ||
assert(!signal); | ||
}); | ||
} |