Skip to content

Commit e058c87

Browse files
authored
fix: ignore invalid package.json during read-pkg-up (#546)
1 parent 8d6ad6e commit e058c87

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'foo': bar}

test/yargs.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,15 @@ describe('yargs dsl tests', function () {
945945
argv.foo.should.equal('a')
946946
argv.dotNotation.should.equal(false)
947947
})
948+
949+
// see https://github.com/yargs/yargs/issues/485
950+
it('handles an invalid package.json', function () {
951+
var argv = yargs('--foo a')
952+
.pkgConf('yargs', './test/fixtures/broken-json')
953+
.argv
954+
955+
argv.foo.should.equal('a')
956+
})
948957
})
949958

950959
describe('skipValidation', function () {

yargs.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,12 @@ function Yargs (processArgs, cwd, parentRequire) {
360360
if (pkg && !path) return pkg
361361
const readPkgUp = require('read-pkg-up')
362362

363-
var obj = readPkgUp.sync({
364-
cwd: path || requireMainFilename(parentRequire || require)
365-
})
363+
var obj = {}
364+
try {
365+
obj = readPkgUp.sync({
366+
cwd: path || requireMainFilename(parentRequire || require)
367+
})
368+
} catch (noop) {}
366369

367370
if (obj.pkg) pkg = obj.pkg
368371
return pkg || {}

0 commit comments

Comments
 (0)