Skip to content

Commit c8f1cb1

Browse files
committed
Merge pull request #459 from mousetraps/i208
fix #208 Error reading package.json. The file may be parseable JSON but may
2 parents 3c61442 + 4d6da86 commit c8f1cb1

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

Nodejs/Product/Npm/ReaderPackageJsonSource.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,21 @@
1717
using System;
1818
using System.IO;
1919
using Newtonsoft.Json;
20+
using Newtonsoft.Json.Linq;
2021

2122
namespace Microsoft.NodejsTools.Npm {
2223
public class ReaderPackageJsonSource : IPackageJsonSource {
2324
public ReaderPackageJsonSource(TextReader reader) {
2425
try {
25-
Package = JsonConvert.DeserializeObject(reader.ReadToEnd());
26+
var text = reader.ReadToEnd();
27+
try {
28+
// JsonConvert and JObject.Parse exhibit slightly different behavior,
29+
// so fall back to JObject.Parse if JsonConvert does not properly deserialize
30+
// the object.
31+
Package = JsonConvert.DeserializeObject(text);
32+
} catch (ArgumentException) {
33+
Package = JObject.Parse(text);
34+
}
2635
} catch (JsonReaderException jre) {
2736
WrapExceptionAndRethrow(jre);
2837
} catch (JsonSerializationException jse) {

Nodejs/Product/Npm/SPI/NodeModules.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ public NodeModules(IRootPackage parent, bool showMissingDevOptionalSubPackages,
4646
// Go through every directory in node_modules, and see if it's required as a top-level dependency
4747
foreach (var moduleDir in topLevelDirectories) {
4848
if (moduleDir.Length < NativeMethods.MAX_FOLDER_PATH && !_ignoredDirectories.Any(toIgnore => moduleDir.EndsWith(toIgnore))) {
49-
var packageJson = PackageJsonFactory.Create(new DirectoryPackageJsonSource(moduleDir));
49+
IPackageJson packageJson;
50+
try {
51+
packageJson = PackageJsonFactory.Create(new DirectoryPackageJsonSource(moduleDir));
52+
} catch (PackageJsonException) {
53+
// Fail gracefully if there was an error parsing the package.json
54+
Debug.Fail("Failed to parse package.json in {0}", moduleDir);
55+
continue;
56+
}
5057

5158
if (packageJson != null) {
5259
if (packageJson.RequiredBy.Count() > 0) {

0 commit comments

Comments
 (0)