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

add preserveSymlinks option #131

Merged
merged 1 commit into from
Jul 26, 2017
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
11 changes: 11 additions & 0 deletions lib/node-modules-paths.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var path = require('path');
var fs = require('fs');
var parse = path.parse || require('path-parse');

module.exports = function nodeModulesPaths(start, opts) {
Expand All @@ -10,6 +11,16 @@ module.exports = function nodeModulesPaths(start, opts) {
// resolving against the process' current working directory
var absoluteStart = path.resolve(start);

if (opts && opts.preserveSymlinks === false) {
try {
absoluteStart = fs.realpathSync(absoluteStart);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}

var prefix = '/';
if (/^([A-Za-z]:)/.test(absoluteStart)) {
prefix = '';
Expand Down
16 changes: 14 additions & 2 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ node_modules recursive walk (probably don't use this)

* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`

* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
**Note:** this property is currently `true` by default but it will be changed to
`false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.

default `opts` values:

``` javascript
Expand All @@ -88,7 +93,8 @@ default `opts` values:
else cb(null, stat.isFile())
});
},
moduleDirectory: 'node_modules'
moduleDirectory: 'node_modules',
preserveSymlinks: true
}
```

Expand All @@ -115,6 +121,11 @@ node_modules recursive walk (probably don't use this)

* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`

* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
**Note:** this property is currently `true` by default but it will be changed to
`false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.

default `opts` values:

``` javascript
Expand All @@ -127,7 +138,8 @@ default `opts` values:
try { return fs.statSync(file).isFile() }
catch (e) { return false }
},
moduleDirectory: 'node_modules'
moduleDirectory: 'node_modules',
preserveSymlinks: true
}
````

Expand Down
1 change: 1 addition & 0 deletions test/resolver/symlinked/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
symlink
1 change: 1 addition & 0 deletions test/resolver/symlinked/_/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules
Empty file.
Empty file.
54 changes: 54 additions & 0 deletions test/symlinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var path = require('path');
var fs = require('fs');
var test = require('tape');
var resolve = require('../');

var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink');
try {
fs.unlinkSync(symlinkDir);
} catch (err) {}
try {
fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir');
} catch (err) {
// if fails then it is probably on Windows and lets try to create a junction
fs.symlinkSync(path.join(__dirname, 'resolver', 'symlinked', '_', 'symlink_target') + '\\', symlinkDir, 'junction');
}

test('symlink', function (t) {
t.plan(1);

resolve('foo', { basedir: symlinkDir, preserveSymlinks: false }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
});
});

test('sync symlink when preserveSymlinks = true', function (t) {
t.plan(4);

resolve('foo', { basedir: symlinkDir }, function (err, res, pkg) {
t.ok(err, 'there is an error');
t.notOk(res, 'no result');

t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
t.equal(
err && err.message,
'Cannot find module \'foo\' from \'' + symlinkDir + '\'',
'can not find nonexistent module'
);
});
});

test('sync symlink', function (t) {
var start = new Date();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be able to use Date.now() here, nbd tho

t.equal(resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }), path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
t.ok(new Date() - start < 50, 'resolve.sync timedout');
t.end();
});

test('sync symlink when preserveSymlinks = true', function (t) {
t.throws(function () {
resolve.sync('foo', { basedir: symlinkDir });
}, /Cannot find module 'foo'/);
t.end();
});