Skip to content

Commit

Permalink
fix(migrations): preserve tsconfig in standalone migration (#48987)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
crisbeto authored and pkozlowski-opensource committed Feb 8, 2023
1 parent 1afa6ed commit e9e4449
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
21 changes: 11 additions & 10 deletions packages/core/schematics/ng-generate/standalone-migration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ export default function(options: Options): Rule {

function standaloneMigration(
tree: Tree, tsconfigPath: string, basePath: string, pathToMigrate: string,
options: Options): number {
if (options.path.startsWith('..')) {
schematicOptions: Options): number {
if (schematicOptions.path.startsWith('..')) {
throw new SchematicsException(
'Cannot run standalone migration outside of the current project.');
}

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

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

if (options.mode === MigrationMode.pruneModules) {
if (schematicOptions.mode === MigrationMode.pruneModules) {
const result = pruneNgModules(program, host, basePath, rootNames, sourceFiles, printer);
pendingChanges = result.pendingChanges;
filesToRemove = result.filesToRemove;
} else if (options.mode === MigrationMode.standaloneBootstrap) {
} else if (schematicOptions.mode === MigrationMode.standaloneBootstrap) {
pendingChanges =
toStandaloneBootstrap(program, host, basePath, rootNames, sourceFiles, printer);
} else {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/schematics/utils/typescript/compiler_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ export function createMigrationProgram(
* @param fakeFileRead Optional file reader function. Can be used to overwrite files in
* the TypeScript program, or to add in-memory files (e.g. to add global types).
* @param additionalFiles Additional file paths that should be added to the program.
* @param optionOverrides Overrides of the parsed compiler options.
*/
export function createProgramOptions(
tree: Tree, tsconfigPath: string, basePath: string, fakeFileRead?: FakeReadFileFn,
additionalFiles?: string[]) {
additionalFiles?: string[], optionOverrides?: ts.CompilerOptions) {
// Resolve the tsconfig path to an absolute path. This is needed as TypeScript otherwise
// is not able to resolve root directories in the given tsconfig. More details can be found
// in the following issue: https://github.com/microsoft/TypeScript/issues/37731.
tsconfigPath = resolve(basePath, tsconfigPath);
const parsed = parseTsconfigFile(tsconfigPath, dirname(tsconfigPath));
const host = createMigrationCompilerHost(tree, parsed.options, basePath, fakeFileRead);
return {rootNames: parsed.fileNames.concat(additionalFiles || []), options: parsed.options, host};
const options = optionOverrides ? {...parsed.options, ...optionOverrides} : parsed.options;
const host = createMigrationCompilerHost(tree, options, basePath, fakeFileRead);
return {rootNames: parsed.fileNames.concat(additionalFiles || []), options, host};
}

function createMigrationCompilerHost(
Expand Down

0 comments on commit e9e4449

Please sign in to comment.