From 58436fd81adf915bcefef0100938e6bf3d262b78 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 19 Feb 2019 17:50:27 +0100 Subject: [PATCH] fix(ivy): unable to import shim factory files on case-insensitive platforms (#28831) This change is kind of similar to #27466, but instead of ensuring that these shims can be generated, we also need to make sure that developers are able to also use the factory shims like with `ngc`. This issue is now surfacing because we have various old examples which are now also built with `ngtsc` (due to the bazel migration). On case insensitive platforms (e.g. windows) these examples cannot be built because ngtsc fails the app imports a generated shim file (such as the factory shim files). This is because the `GeneratedShimsHostWrapper` TypeScript host uses the `getCanonicalFileName` method in order to check whether a given file/module exists in the generator file maps. e.g. ``` // Generator Map: 'C:/users/paul/_bazel_paul/lm3s4mgv/execroot/angular/packages/core/index.ngfactory.ts' => 'C:/users/paul/_bazel_paul/lm3s4mgv/execroot/angular/packages/core/index.ts', // Path passed into `fileExists` C:/users/paul/_bazel_paul/lm3s4mgv/execroot/angular/packages/core/index.ngfactory.ts // After getCanonicalFileName (notice the **lower-case drive name**) c:/users/paul/_bazel_paul/lm3s4mgv/execroot/angular/packages/core/index.ngfactory.ts ``` As seen above, the generator map does not use the canonical file names, as well as TypeScript internally does not pass around canonical file names. We can fix this by removing the manual call to `getCanonicalFileName` and just following TypeScript internal-semantics. PR Close #28831 --- .../compiler-cli/src/ngtsc/shims/src/host.ts | 3 +-- packages/compiler-cli/test/ngtsc/ngtsc_spec.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/shims/src/host.ts b/packages/compiler-cli/src/ngtsc/shims/src/host.ts index 5dcf0af8dc..d30dac5756 100644 --- a/packages/compiler-cli/src/ngtsc/shims/src/host.ts +++ b/packages/compiler-cli/src/ngtsc/shims/src/host.ts @@ -95,11 +95,10 @@ export class GeneratedShimsHostWrapper implements ts.CompilerHost { getNewLine(): string { return this.delegate.getNewLine(); } fileExists(fileName: string): boolean { - const canonical = this.getCanonicalFileName(fileName); // Consider the file as existing whenever 1) it really does exist in the delegate host, or // 2) at least one of the shim generators recognizes it. return this.delegate.fileExists(fileName) || - this.shimGenerators.some(gen => gen.recognize(canonical)); + this.shimGenerators.some(gen => gen.recognize(fileName)); } readFile(fileName: string): string|undefined { return this.delegate.readFile(fileName); } diff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts index 71c02f37d6..a27662e909 100644 --- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts +++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts @@ -1550,6 +1550,23 @@ describe('ngtsc behavioral tests', () => { expect(emptyFactory).toContain(`export var ɵNonEmptyModule = true;`); }); + it('should be able to compile an app using the factory shim', () => { + env.tsconfig({'allowEmptyCodegenFiles': true}); + + env.write('test.ts', ` + export {MyModuleNgFactory} from './my-module.ngfactory'; + `); + + env.write('my-module.ts', ` + import {NgModule} from '@angular/core'; + + @NgModule({}) + export class MyModule {} + `); + + env.driveMain(); + }); + it('should generate correct imports in factory stubs when compiling @angular/core', () => { env.tsconfig({'allowEmptyCodegenFiles': true});