Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59607dc

Browse files
devversionmatsko
authored andcommitted
fix(core): undecorated-classes-with-di migration should handle libraries generated with CLI versions past v6.2.0 (#35824)
The options for `flatModuleId` and `flatModuleOutFile` had been removed in the CLI from generated libraries with angular/angular-cli@718ee15. This has been done because `ng-packagr` (which is used to build the libraries) automatically set these options in-memory when it compiles the library. No migration has been created for this because there was no actual need to get rid of this. Keeping the options in the library `tsconfig` does not cause any problems unless the `tsconfig` is used outside of `ng-packagr`. This was not anticipated, but is now commonly done in `ng update` migrations. The `ng update` migrations try to create an instance of the `AngularCompilerProgram` by simply parsing the `tsconfig`. The migrations make the valid assumption that `tsconfig` files are not incomplete/invalid. They _definitely_ are in the file system though. It just works for libraries because `ng-packagr` in-memory completes the invalid `tsconfig` files, so that they can be passed to the `@angular/compiler-cli`. We can't have this logic in the `ng update` migrations because it's out-of-scope for individual migrations to distinguish between libraries and applications. Also it would be out-of-scope to parse the `ng-packagr` configuration and handle the tsconfig in-memory completion. As a workaround though, we can remove the flat-module bundle options in-memory when creating the compiler program. This is acceptable since we don't emit the program and the flat module bundles are not needed. Fixes #34985. PR Close #35824
1 parent bef14cf commit 59607dc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

‎packages/core/schematics/migrations/undecorated-classes-with-di/create_ngc_program.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ export function createNgcProgram(
2020
// NGC program. In order to ensure that the migration runs properly, we set "enableIvy" to false.
2121
options.enableIvy = false;
2222

23+
// Libraries which have been generated with CLI versions past v6.2.0, explicitly set the
24+
// flat-module options in their tsconfig files. This is problematic because by default,
25+
// those tsconfig files do not specify explicit source files which can be considered as
26+
// entry point for the flat-module bundle. Therefore the `@angular/compiler-cli` is unable
27+
// to determine the flat module entry point and throws a compile error. This is not an issue
28+
// for the libraries built with `ng-packagr`, because the tsconfig files are modified in-memory
29+
// to specify an explicit flat module entry-point. Our migrations don't distinguish between
30+
// libraries and applications, and also don't run `ng-packagr`. To ensure that such libraries
31+
// can be successfully migrated, we remove the flat-module options to eliminate the flat module
32+
// entry-point requirement. More context: https://github.com/angular/angular/issues/34985.
33+
options.flatModuleId = undefined;
34+
options.flatModuleOutFile = undefined;
35+
2336
const host = createHost(options);
2437

2538
// For this migration, we never need to read resources and can just return

‎packages/core/schematics/test/undecorated_classes_with_di_migration_spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,40 @@ describe('Undecorated classes with DI migration', () => {
14731473
'TypeScript program failures');
14741474
});
14751475

1476+
// Regression test for: https://github.com/angular/angular/issues/34985.
1477+
it('should be able to migrate libraries with multiple source files and flat-module ' +
1478+
'options set',
1479+
async() => {
1480+
writeFile('/tsconfig.json', JSON.stringify({
1481+
compilerOptions: {
1482+
lib: ['es2015'],
1483+
},
1484+
angularCompilerOptions:
1485+
{flatModuleId: 'AUTOGENERATED', flatModuleOutFile: 'AUTOGENERATED'}
1486+
}));
1487+
1488+
writeFile('/second.ts', ``);
1489+
writeFile('/test.ts', `
1490+
import {Injectable, NgModule, NgZone} from '@angular/core';
1491+
1492+
export class BaseClass {
1493+
constructor(zone: NgZone) {}
1494+
}
1495+
1496+
@Injectable({template: ''})
1497+
export class MyService extends BaseClass {}
1498+
1499+
@NgModule({providers: [MyService]})
1500+
export class AppModule {}
1501+
`);
1502+
1503+
await runMigration();
1504+
1505+
expect(errorOutput.length).toBe(0);
1506+
expect(warnOutput.length).toBe(0);
1507+
expect(tree.readContent('/test.ts')).toMatch(/@Injectable\(\)\nexport class BaseClass {/);
1508+
});
1509+
14761510
it('should not throw if resources could not be read', async() => {
14771511
writeFile('/index.ts', `
14781512
import {Component, NgModule} from '@angular/core';

0 commit comments

Comments
 (0)