diff --git a/packages/core/schematics/migrations/undecorated-classes-with-di/create_ngc_program.ts b/packages/core/schematics/migrations/undecorated-classes-with-di/create_ngc_program.ts index 4c0f09cb51..a25fc52c0b 100644 --- a/packages/core/schematics/migrations/undecorated-classes-with-di/create_ngc_program.ts +++ b/packages/core/schematics/migrations/undecorated-classes-with-di/create_ngc_program.ts @@ -20,6 +20,19 @@ export function createNgcProgram( // NGC program. In order to ensure that the migration runs properly, we set "enableIvy" to false. options.enableIvy = false; + // Libraries which have been generated with CLI versions past v6.2.0, explicitly set the + // flat-module options in their tsconfig files. This is problematic because by default, + // those tsconfig files do not specify explicit source files which can be considered as + // entry point for the flat-module bundle. Therefore the `@angular/compiler-cli` is unable + // to determine the flat module entry point and throws a compile error. This is not an issue + // for the libraries built with `ng-packagr`, because the tsconfig files are modified in-memory + // to specify an explicit flat module entry-point. Our migrations don't distinguish between + // libraries and applications, and also don't run `ng-packagr`. To ensure that such libraries + // can be successfully migrated, we remove the flat-module options to eliminate the flat module + // entry-point requirement. More context: https://github.com/angular/angular/issues/34985. + options.flatModuleId = undefined; + options.flatModuleOutFile = undefined; + const host = createHost(options); // For this migration, we never need to read resources and can just return diff --git a/packages/core/schematics/test/undecorated_classes_with_di_migration_spec.ts b/packages/core/schematics/test/undecorated_classes_with_di_migration_spec.ts index b39a8ae74a..997dca5b52 100644 --- a/packages/core/schematics/test/undecorated_classes_with_di_migration_spec.ts +++ b/packages/core/schematics/test/undecorated_classes_with_di_migration_spec.ts @@ -1473,6 +1473,40 @@ describe('Undecorated classes with DI migration', () => { 'TypeScript program failures'); }); + // Regression test for: https://github.com/angular/angular/issues/34985. + it('should be able to migrate libraries with multiple source files and flat-module ' + + 'options set', + async() => { + writeFile('/tsconfig.json', JSON.stringify({ + compilerOptions: { + lib: ['es2015'], + }, + angularCompilerOptions: + {flatModuleId: 'AUTOGENERATED', flatModuleOutFile: 'AUTOGENERATED'} + })); + + writeFile('/second.ts', ``); + writeFile('/test.ts', ` + import {Injectable, NgModule, NgZone} from '@angular/core'; + + export class BaseClass { + constructor(zone: NgZone) {} + } + + @Injectable({template: ''}) + export class MyService extends BaseClass {} + + @NgModule({providers: [MyService]}) + export class AppModule {} + `); + + await runMigration(); + + expect(errorOutput.length).toBe(0); + expect(warnOutput.length).toBe(0); + expect(tree.readContent('/test.ts')).toMatch(/@Injectable\(\)\nexport class BaseClass {/); + }); + it('should not throw if resources could not be read', async() => { writeFile('/index.ts', ` import {Component, NgModule} from '@angular/core';