Skip to content

Commit f5a0ec0

Browse files
cexbrayatAndrewKushnir
authored andcommitted
fix(ivy): ngcc should not fail on invalid package.json (angular#26539)
Some package.json files may have invalid JSON, for example package.json blueprints from `@schematics/angular` (see https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/workspace/files/package.json). This makes ngcc more resilient, by simpling logging a warning if an error is encountered, instead of failing as it does right now. PR Close angular#26539
1 parent 5247594 commit f5a0ec0

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

packages/compiler-cli/src/ngcc/src/packages/entry_point.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ interface EntryPointPackageJson {
5050
typings?: string; // TypeScript .d.ts files
5151
}
5252

53+
/**
54+
* Parses the JSON from a package.json file.
55+
* @param packageJsonPath the absolute path to the package.json file.
56+
* @returns JSON from the package.json file if it is valid, `null` otherwise.
57+
*/
58+
function loadEntryPointPackage(packageJsonPath: string): {[key: string]: any}|null {
59+
try {
60+
return JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
61+
} catch (e) {
62+
// We may have run into a package.json with unexpected symbols
63+
console.warn(`Failed to read entry point info from ${packageJsonPath} with error ${e}.`);
64+
return null;
65+
}
66+
}
67+
5368
/**
5469
* Try to get entry point info from the given path.
5570
* @param pkgPath the absolute path to the containing npm package
@@ -62,6 +77,11 @@ export function getEntryPointInfo(pkgPath: string, entryPoint: string): EntryPoi
6277
return null;
6378
}
6479

80+
const entryPointPackageJson = loadEntryPointPackage(packageJsonPath);
81+
if (!entryPointPackageJson) {
82+
return null;
83+
}
84+
6585
// If there is `esm2015` then `es2015` will be FESM2015, otherwise ESM2015.
6686
// If there is `esm5` then `module` will be FESM5, otherwise it will be ESM5.
6787
const {
@@ -75,8 +95,7 @@ export function getEntryPointInfo(pkgPath: string, entryPoint: string): EntryPoi
7595
esm2015,
7696
esm5,
7797
main
78-
}: EntryPointPackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
79-
98+
} = entryPointPackageJson;
8099
// Minimum requirement is that we have typings and one of esm2015 or fesm2015 formats.
81100
if (!typings || !(fesm2015 || esm2015)) {
82101
return null;

packages/compiler-cli/src/ngcc/test/packages/entry_point_spec.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ describe('getEntryPointInfo()', () => {
7878
umd: `/some_package/material_style/bundles/material_style.umd.js`,
7979
});
8080
});
81+
82+
it('should return null if the package.json is not valid JSON', () => {
83+
const entryPoint = getEntryPointInfo('/some_package', '/some_package/unexpected_symbols');
84+
expect(entryPoint).toBe(null);
85+
});
8186
});
8287

8388
function createMockFileSystem() {
@@ -115,8 +120,15 @@ function createMockFileSystem() {
115120
"module": "./esm5/material_style.es5.js",
116121
"es2015": "./esm2015/material_style.js"
117122
}`,
118-
'material_style.metadata.json': 'some meta data'
119-
}
123+
'material_style.metadata.json': 'some meta data',
124+
},
125+
'unexpected_symbols': {
126+
// package.json might not be a valid JSON
127+
// for example, @schematics/angular contains a package.json blueprint
128+
// with unexpected symbols
129+
'package.json':
130+
'{"devDependencies": {<% if (!minimal) { %>"@types/jasmine": "~2.8.8" <% } %>}}',
131+
},
120132
}
121133
});
122134
}

0 commit comments

Comments
 (0)