Skip to content

Commit e9e4449

Browse files
crisbetopkozlowski-opensource
authored andcommitted
fix(migrations): preserve tsconfig in standalone migration (#48987)
For the standalone migration we need to pass a couple of compiler flags which accidentally also overwrote the project's compiler options. These changes extend the options instead. PR Close #48987
1 parent 1afa6ed commit e9e4449

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

packages/core/schematics/ng-generate/standalone-migration/index.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,19 @@ export default function(options: Options): Rule {
6161

6262
function standaloneMigration(
6363
tree: Tree, tsconfigPath: string, basePath: string, pathToMigrate: string,
64-
options: Options): number {
65-
if (options.path.startsWith('..')) {
64+
schematicOptions: Options): number {
65+
if (schematicOptions.path.startsWith('..')) {
6666
throw new SchematicsException(
6767
'Cannot run standalone migration outside of the current project.');
6868
}
6969

70-
const {host, rootNames} = createProgramOptions(tree, tsconfigPath, basePath);
71-
const program = createProgram({
72-
rootNames,
73-
host,
74-
options: {_enableTemplateTypeChecker: true, compileNonExportedClasses: true}
75-
}) as NgtscProgram;
70+
const {host, options, rootNames} = createProgramOptions(
71+
tree, tsconfigPath, basePath, undefined, undefined,
72+
{
73+
_enableTemplateTypeChecker: true, // Required for the template type checker to work.
74+
compileNonExportedClasses: true, // We want to migrate non-exported classes too.
75+
});
76+
const program = createProgram({rootNames, host, options}) as NgtscProgram;
7677
const printer = ts.createPrinter();
7778

7879
if (existsSync(pathToMigrate) && !statSync(pathToMigrate).isDirectory()) {
@@ -92,11 +93,11 @@ function standaloneMigration(
9293
let pendingChanges: ChangesByFile;
9394
let filesToRemove: Set<ts.SourceFile>|null = null;
9495

95-
if (options.mode === MigrationMode.pruneModules) {
96+
if (schematicOptions.mode === MigrationMode.pruneModules) {
9697
const result = pruneNgModules(program, host, basePath, rootNames, sourceFiles, printer);
9798
pendingChanges = result.pendingChanges;
9899
filesToRemove = result.filesToRemove;
99-
} else if (options.mode === MigrationMode.standaloneBootstrap) {
100+
} else if (schematicOptions.mode === MigrationMode.standaloneBootstrap) {
100101
pendingChanges =
101102
toStandaloneBootstrap(program, host, basePath, rootNames, sourceFiles, printer);
102103
} else {

packages/core/schematics/utils/typescript/compiler_host.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ export function createMigrationProgram(
3939
* @param fakeFileRead Optional file reader function. Can be used to overwrite files in
4040
* the TypeScript program, or to add in-memory files (e.g. to add global types).
4141
* @param additionalFiles Additional file paths that should be added to the program.
42+
* @param optionOverrides Overrides of the parsed compiler options.
4243
*/
4344
export function createProgramOptions(
4445
tree: Tree, tsconfigPath: string, basePath: string, fakeFileRead?: FakeReadFileFn,
45-
additionalFiles?: string[]) {
46+
additionalFiles?: string[], optionOverrides?: ts.CompilerOptions) {
4647
// Resolve the tsconfig path to an absolute path. This is needed as TypeScript otherwise
4748
// is not able to resolve root directories in the given tsconfig. More details can be found
4849
// in the following issue: https://github.com/microsoft/TypeScript/issues/37731.
4950
tsconfigPath = resolve(basePath, tsconfigPath);
5051
const parsed = parseTsconfigFile(tsconfigPath, dirname(tsconfigPath));
51-
const host = createMigrationCompilerHost(tree, parsed.options, basePath, fakeFileRead);
52-
return {rootNames: parsed.fileNames.concat(additionalFiles || []), options: parsed.options, host};
52+
const options = optionOverrides ? {...parsed.options, ...optionOverrides} : parsed.options;
53+
const host = createMigrationCompilerHost(tree, options, basePath, fakeFileRead);
54+
return {rootNames: parsed.fileNames.concat(additionalFiles || []), options, host};
5355
}
5456

5557
function createMigrationCompilerHost(

0 commit comments

Comments
 (0)