Skip to content

Commit fca1724

Browse files
devversionkara
authored andcommitted
refactor(core): static-query schematic incorrectly handles multiple tsconfigs (angular#29133)
Currently when the static-query runs for a project with multiple TypeScript configuration files (e.g. a usual CLI project), the migration incorrectly applies the code transformation multiple times. This is because the migration is currently based on the source file contents in the file system, while the actual source file contents could have already changed in the devkit schematic tree. PR Close angular#29133
1 parent 3c53713 commit fca1724

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/core/schematics/migrations/static-queries/migration.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ import {parseTsconfigFile} from './typescript/tsconfig';
2525
export function runStaticQueryMigration(tree: Tree, tsconfigPath: string, basePath: string) {
2626
const parsed = parseTsconfigFile(tsconfigPath, dirname(tsconfigPath));
2727
const host = ts.createCompilerHost(parsed.options, true);
28+
29+
// We need to overwrite the host "readFile" method, as we want the TypeScript
30+
// program to be based on the file contents in the virtual file tree. Otherwise
31+
// if we run the migration for multiple tsconfig files which have intersecting
32+
// source files, it can end up updating query definitions multiple times.
33+
host.readFile = fileName => {
34+
const buffer = tree.read(relative(basePath, fileName));
35+
return buffer ? buffer.toString() : undefined;
36+
};
37+
2838
const program = ts.createProgram(parsed.fileNames, parsed.options, host);
2939
const typeChecker = program.getTypeChecker();
3040
const queryVisitor = new NgQueryResolveVisitor(typeChecker);

packages/core/schematics/test/static_queries_migration_spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,5 +710,30 @@ describe('static-queries migration', () => {
710710
expect(tree.readContent('/index.ts'))
711711
.toContain(`@${queryType}('test', { static: true }) query2: any;`);
712712
});
713+
714+
it('should properly handle multiple tsconfig files', () => {
715+
writeFile('/src/index.ts', `
716+
import {Component, ${queryType}} from '@angular/core';
717+
718+
@Component({template: '<span #test></span>'})
719+
export class MyComp {
720+
private @${queryType}('test') query: any;
721+
}
722+
`);
723+
724+
writeFile('/src/tsconfig.json', JSON.stringify({
725+
compilerOptions: {
726+
lib: ['es2015'],
727+
}
728+
}));
729+
730+
// The migration runs for "/tsconfig.json" and "/src/tsconfig.json" which both
731+
// contain the "src/index.ts" file. This test ensures that we don't incorrectly
732+
// apply the code transformation multiple times with outdated offsets.
733+
runMigration();
734+
735+
expect(tree.readContent('/src/index.ts'))
736+
.toContain(`@${queryType}('test', { static: false }) query: any;`);
737+
});
713738
}
714739
});

0 commit comments

Comments
 (0)