Skip to content

Commit 0fe2711

Browse files
devversionjosephperrott
authored andcommitted
fix(ng-update): parse cli workspace config as json5 (#16218)
Currently we try to parse CLI workspace configurations gracefully by using the native `JSON.parse()` method. This means that the CLI workspace configuration needs to follow the strict JSON specification because otherwise the migrations would not be able to find TypeScript configurations in the CLI project where JSON5 workspace configurations are supported. In order to handle such workspace configurations, we leverage the JSON parsing logicfrom the `@angular-devkit/core` which is also used by the CLI.
1 parent 7386ea1 commit 0fe2711

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,24 @@ describe('ng-update project-tsconfig-paths', () => {
4545

4646
expect(getProjectTsConfigPaths(testTree)).toEqual(['tsconfig.json']);
4747
});
48+
49+
it('should be able to read workspace configuration which is using JSON5 features', () => {
50+
testTree.create('/my-build-config.json', '');
51+
testTree.create('/angular.json', `{
52+
// Comments, unquoted properties or trailing commas are only supported in JSON5.
53+
projects: {
54+
with_tests: {
55+
targets: {
56+
build: {
57+
options: {
58+
tsConfig: './my-build-config.json',
59+
}
60+
}
61+
}
62+
}
63+
},
64+
}`);
65+
66+
expect(getProjectTsConfigPaths(testTree)).toEqual(['my-build-config.json']);
67+
});
4868
});

src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {normalize} from '@angular-devkit/core';
9+
import {JsonParseMode, normalize, parseJson} from '@angular-devkit/core';
1010
import {Tree} from '@angular-devkit/schematics';
1111

1212
/** Name of the default Angular CLI workspace configuration files. */
@@ -63,7 +63,9 @@ function getWorkspaceConfigGracefully(tree: Tree): any {
6363
}
6464

6565
try {
66-
return JSON.parse(configBuffer.toString());
66+
// Parse the workspace file as JSON5 which is also supported for CLI
67+
// workspace configurations.
68+
return parseJson(configBuffer.toString(), JsonParseMode.Json5);
6769
} catch {
6870
return null;
6971
}

0 commit comments

Comments
 (0)