From b70746a11324609ce236ac399f00e976ec8f4eeb Mon Sep 17 00:00:00 2001 From: JoostK Date: Sat, 27 Jul 2019 21:02:04 +0200 Subject: [PATCH] fix(ivy): ngcc - prevent crash when analyzed target is ignored (#31872) ngcc analyzes the dependency structure of the entrypoints it needs to process, as the compilation of entrypoints is ordering sensitive: any dependent upon entrypoint must be compiled before its dependees. As part of the analysis of the dependency graph, it is detected when a dependency of entrypoint is not installed, in which case that entrypoint will be marked as ignored. When a target entrypoint to compile is provided, it could occur that given target is considered ignored because one of its dependencies might be missing. This situation was not dealt with currently, instead resulting in a crash of ngcc. This commit prevents the crash by taking the above scenario into account. PR Close #31872 --- .../ngcc/src/dependencies/dependency_resolver.ts | 2 +- .../test/dependencies/dependency_resolver_spec.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/ngcc/src/dependencies/dependency_resolver.ts b/packages/compiler-cli/ngcc/src/dependencies/dependency_resolver.ts index 02d4a26013..316dc2c474 100644 --- a/packages/compiler-cli/ngcc/src/dependencies/dependency_resolver.ts +++ b/packages/compiler-cli/ngcc/src/dependencies/dependency_resolver.ts @@ -81,7 +81,7 @@ export class DependencyResolver { let sortedEntryPointNodes: string[]; if (target) { - if (target.compiledByAngular) { + if (target.compiledByAngular && graph.hasNode(target.path)) { sortedEntryPointNodes = graph.dependenciesOf(target.path); sortedEntryPointNodes.push(target.path); } else { diff --git a/packages/compiler-cli/ngcc/test/dependencies/dependency_resolver_spec.ts b/packages/compiler-cli/ngcc/test/dependencies/dependency_resolver_spec.ts index abb47d0fef..6d395632d7 100644 --- a/packages/compiler-cli/ngcc/test/dependencies/dependency_resolver_spec.ts +++ b/packages/compiler-cli/ngcc/test/dependencies/dependency_resolver_spec.ts @@ -179,6 +179,19 @@ runInEachFileSystem(() => { expect(sorted.entryPoints).toEqual([fifth]); }); + it('should not process the provided target if it has missing dependencies', () => { + spyOn(host, 'findDependencies').and.callFake(createFakeComputeDependencies({ + [_('/first/index.js')]: {resolved: [], missing: ['/missing']}, + })); + const entryPoints = [first]; + let sorted: SortedEntryPointsInfo; + + sorted = resolver.sortEntryPointsByDependency(entryPoints, first); + expect(sorted.entryPoints).toEqual([]); + expect(sorted.invalidEntryPoints[0].entryPoint).toEqual(first); + expect(sorted.invalidEntryPoints[0].missingDependencies).toEqual(['/missing']); + }); + it('should use the appropriate DependencyHost for each entry-point', () => { const esm5Host = new EsmDependencyHost(fs, moduleResolver); const esm2015Host = new EsmDependencyHost(fs, moduleResolver);