Skip to content

Commit d13387d

Browse files
alan-agius4vikerman
authored andcommitted
fix(@schematics/angular): tsconfig creates invalid file references
Fixes: #16035
1 parent afae476 commit d13387d

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

packages/schematics/angular/migrations/update-9/update-app-tsconfigs.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import { JsonAstObject } from '@angular-devkit/core';
99
import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics';
10+
import { posix } from 'path';
1011
import {
1112
findPropertyInAstObject,
1213
insertPropertyInAstObjectInOrder,
@@ -74,38 +75,47 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
7475
}
7576

7677
// Add stricter file inclusions to avoid unused file warning during compilation
77-
const rootInSrc = tsConfigPath.includes('src/');
78-
const rootSrc = rootInSrc ? '' : 'src/';
7978
if (builderName !== Builders.Karma) {
8079
// Note: we need to re-read the tsconfig after very commit because
8180
// otherwise the updates will be out of sync since we are ammending the same node.
8281
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
83-
const files = findPropertyInAstObject(tsConfigAst, 'files');
8482
const include = findPropertyInAstObject(tsConfigAst, 'include');
8583

86-
recorder = tree.beginUpdate(tsConfigPath);
8784
if (include && include.kind === 'array') {
8885
const tsInclude = include.elements.find(({ value }) => typeof value === 'string' && value.endsWith('**/*.ts'));
8986
if (tsInclude) {
9087
const { start, end } = tsInclude;
88+
recorder = tree.beginUpdate(tsConfigPath);
9189
recorder.remove(start.offset, end.offset - start.offset);
9290
// Replace ts includes with d.ts
9391
recorder.insertLeft(start.offset, tsInclude.text.replace('.ts', '.d.ts'));
92+
tree.commitUpdate(recorder);
9493
}
95-
} else {
96-
insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'include', [`${rootSrc}**/*.d.ts`], 2);
9794
}
9895

99-
tree.commitUpdate(recorder);
100-
96+
const files = findPropertyInAstObject(tsConfigAst, 'files');
10197
if (!files) {
102-
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
103-
recorder = tree.beginUpdate(tsConfigPath);
98+
const newFiles: string[] = [];
99+
100+
const mainOption = findPropertyInAstObject(option, 'main');
101+
if (mainOption && mainOption.kind === 'string') {
102+
newFiles.push(posix.relative(posix.dirname(tsConfigPath), mainOption.value));
103+
}
104104

105-
const files = builderName === Builders.Server
106-
? [`${rootSrc}main.server.ts`]
107-
: [`${rootSrc}main.ts`, `${rootSrc}polyfills.ts`];
108-
insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'files', files, 2);
105+
const polyfillsOption = findPropertyInAstObject(option, 'polyfills');
106+
if (polyfillsOption && polyfillsOption.kind === 'string') {
107+
newFiles.push(posix.relative(posix.dirname(tsConfigPath), polyfillsOption.value));
108+
}
109+
110+
if (newFiles.length) {
111+
recorder = tree.beginUpdate(tsConfigPath);
112+
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
113+
insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'files', newFiles, 2);
114+
tree.commitUpdate(recorder);
115+
}
116+
117+
recorder = tree.beginUpdate(tsConfigPath);
118+
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
109119
removePropertyInAstObject(recorder, tsConfigAst, 'exclude');
110120
tree.commitUpdate(recorder);
111121
}

packages/schematics/angular/migrations/update-9/update-app-tsconfigs_spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ describe('Migration to version 9', () => {
5858
it('should update apps tsConfig with stricter files inclusions', async () => {
5959
overrideJsonFile(tree, 'tsconfig.app.json', defaultTsConfigOptions);
6060
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
61-
const { exclude, files, include } = JSON.parse(tree2.readContent('tsconfig.app.json'));
61+
const { exclude, files } = JSON.parse(tree2.readContent('tsconfig.app.json'));
6262
expect(exclude).toBeUndefined();
6363
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
64-
expect(include).toEqual(['src/**/*.d.ts']);
6564
});
6665

6766
it('should update apps tsConfig when tsconfig has include', async () => {

0 commit comments

Comments
 (0)