Skip to content

Commit 599e530

Browse files
committed
[Fix] sync: packageFilter: when preserveSymlinks is false, realpath the pkgfile option
Fixes #201.
1 parent 912a6cb commit 599e530

File tree

5 files changed

+127
-8
lines changed

5 files changed

+127
-8
lines changed

lib/async.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ module.exports = function resolve(x, options, callback) {
207207
var pkg = JSON.parse(body);
208208
} catch (jsonErr) {}
209209

210-
if (opts.packageFilter) {
210+
if (pkg && opts.packageFilter) {
211211
pkg = opts.packageFilter(pkg, pkgfile);
212212
}
213213

214-
if (pkg.main) {
214+
if (pkg && pkg.main) {
215215
if (typeof pkg.main !== 'string') {
216216
var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
217217
mainError.code = 'INVALID_PACKAGE_MAIN';

lib/sync.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module.exports = function (x, options) {
105105
}
106106
if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
107107

108-
var pkgfile = path.join(dir, 'package.json');
108+
var pkgfile = maybeUnwrapSymlink(path.join(dir, 'package.json'), opts);
109109

110110
if (!isFile(pkgfile)) {
111111
return loadpkg(path.dirname(dir));
@@ -118,25 +118,25 @@ module.exports = function (x, options) {
118118
} catch (jsonErr) {}
119119

120120
if (pkg && opts.packageFilter) {
121-
pkg = opts.packageFilter(pkg, dir);
121+
pkg = opts.packageFilter(pkg, pkgfile);
122122
}
123123

124124
return { pkg: pkg, dir: dir };
125125
}
126126

127127
function loadAsDirectorySync(x) {
128-
var pkgfile = path.join(x, '/package.json');
128+
var pkgfile = maybeUnwrapSymlink(path.join(x, '/package.json'), opts);
129129
if (isFile(pkgfile)) {
130130
try {
131131
var body = readFileSync(pkgfile, 'UTF8');
132132
var pkg = JSON.parse(body);
133133
} catch (e) {}
134134

135-
if (opts.packageFilter) {
135+
if (pkg && opts.packageFilter) {
136136
pkg = opts.packageFilter(pkg, x);
137137
}
138138

139-
if (pkg.main) {
139+
if (pkg && pkg.main) {
140140
if (typeof pkg.main !== 'string') {
141141
var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
142142
mainError.code = 'INVALID_PACKAGE_MAIN';

test/symlinks.js

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@ var resolve = require('../');
55

66
var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink');
77
var packageDir = path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'package');
8+
var modADir = path.join(__dirname, 'symlinks', 'source', 'node_modules', 'mod-a');
9+
var symlinkModADir = path.join(__dirname, 'symlinks', 'dest', 'node_modules', 'mod-a');
810
try {
911
fs.unlinkSync(symlinkDir);
1012
} catch (err) {}
1113
try {
1214
fs.unlinkSync(packageDir);
1315
} catch (err) {}
16+
try {
17+
fs.unlinkSync(modADir);
18+
} catch (err) {}
19+
try {
20+
fs.unlinkSync(symlinkModADir);
21+
} catch (err) {}
22+
1423
try {
1524
fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir');
1625
} catch (err) {
@@ -23,6 +32,12 @@ try {
2332
// if fails then it is probably on Windows and lets try to create a junction
2433
fs.symlinkSync(path.join(__dirname, '..', '..', 'package') + '\\', packageDir, 'junction');
2534
}
35+
try {
36+
fs.symlinkSync('../../source/node_modules/mod-a', symlinkModADir, 'dir');
37+
} catch (err) {
38+
// if fails then it is probably on Windows and lets try to create a junction
39+
fs.symlinkSync(path.join(__dirname, '..', '..', 'source', 'node_modules', 'mod-a') + '\\', symlinkModADir, 'junction');
40+
}
2641

2742
test('symlink', function (t) {
2843
t.plan(2);
@@ -79,6 +94,106 @@ test('async symlink from node_modules to other dir when preserveSymlinks = false
7994
resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) {
8095
t.notOk(err, 'no error');
8196
t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
82-
t.end();
8397
});
8498
});
99+
100+
test('packageFilter', function (t) {
101+
t.test('preserveSymlinks: false', function (st) {
102+
st.plan(5);
103+
104+
var basedir = path.join(__dirname, 'symlinks', 'dest');
105+
var packageFilterPath;
106+
var actualPath = resolve.sync('mod-a', {
107+
basedir: basedir,
108+
preserveSymlinks: false,
109+
packageFilter: function (pkg, pkgfile) {
110+
packageFilterPath = pkgfile;
111+
}
112+
});
113+
st.equal(
114+
actualPath,
115+
path.join(__dirname, 'symlinks/source/node_modules/mod-a/index.js'),
116+
'sync: actual path is correct'
117+
);
118+
st.equal(
119+
packageFilterPath,
120+
path.join(__dirname, 'symlinks/source/node_modules/mod-a/package.json'),
121+
'sync: packageFilter pkgfile arg is correct'
122+
);
123+
124+
resolve(
125+
'mod-a',
126+
{
127+
basedir: basedir,
128+
preserveSymlinks: false,
129+
packageFilter: function (pkg, pkgfile) {
130+
packageFilterPath = pkgfile;
131+
}
132+
},
133+
function (err, actualPath) {
134+
st.error(err, 'no error');
135+
st.equal(
136+
actualPath,
137+
path.join(__dirname, 'symlinks/source/node_modules/mod-a/index.js'),
138+
'async: actual path is correct'
139+
);
140+
st.equal(
141+
packageFilterPath,
142+
path.join(__dirname, 'symlinks/dest/node_modules/mod-a/package.json'),
143+
'async: packageFilter pkgfile arg is correct'
144+
);
145+
}
146+
);
147+
});
148+
149+
t.test('preserveSymlinks: true', function (st) {
150+
st.plan(5);
151+
152+
var basedir = path.join(__dirname, 'symlinks', 'dest');
153+
var packageFilterPath;
154+
var actualPath = resolve.sync('mod-a', {
155+
basedir: basedir,
156+
preserveSymlinks: true,
157+
packageFilter: function (pkg, pkgfile) {
158+
packageFilterPath = pkgfile;
159+
}
160+
});
161+
st.equal(
162+
actualPath,
163+
path.join(__dirname, 'symlinks/dest/node_modules/mod-a/index.js'),
164+
'sync: actual path is correct'
165+
);
166+
st.equal(
167+
packageFilterPath,
168+
path.join(__dirname, 'symlinks/dest/node_modules/mod-a/package.json'),
169+
'sync: packageFilter pkgfile arg is correct'
170+
);
171+
172+
var packageFilterPath;
173+
resolve(
174+
'mod-a',
175+
{
176+
basedir: basedir,
177+
preserveSymlinks: true,
178+
packageFilter: function (pkg, pkgfile) {
179+
packageFilterPath = pkgfile;
180+
}
181+
},
182+
function (err, actualPath) {
183+
st.error(err, 'no error');
184+
st.equal(
185+
actualPath,
186+
path.join(__dirname, 'symlinks/dest/node_modules/mod-a/index.js'),
187+
'async: actual path is correct'
188+
);
189+
st.equal(
190+
packageFilterPath,
191+
path.join(__dirname, 'symlinks/dest/node_modules/mod-a/package.json'),
192+
'async: packageFilter pkgfile arg is correct'
193+
);
194+
}
195+
);
196+
});
197+
198+
t.end();
199+
});

test/symlinks/dest/node_modules/mod-a

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/symlinks/source/node_modules/mod-a/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)