diff --git a/packages/compiler-cli/ngcc/src/entry_point_finder/utils.ts b/packages/compiler-cli/ngcc/src/entry_point_finder/utils.ts index 601d5c7721..a264166d01 100644 --- a/packages/compiler-cli/ngcc/src/entry_point_finder/utils.ts +++ b/packages/compiler-cli/ngcc/src/entry_point_finder/utils.ts @@ -5,7 +5,7 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -import {AbsoluteFsPath, join, resolve} from '../../../src/ngtsc/file_system'; +import {AbsoluteFsPath, getFileSystem, join, resolve} from '../../../src/ngtsc/file_system'; import {PathMappings} from '../utils'; /** @@ -29,11 +29,17 @@ import {PathMappings} from '../utils'; */ export function getBasePaths( sourceDirectory: AbsoluteFsPath, pathMappings: PathMappings | undefined): AbsoluteFsPath[] { - const basePaths = [sourceDirectory]; + const fs = getFileSystem(); + let basePaths = [sourceDirectory]; if (pathMappings) { const baseUrl = resolve(pathMappings.baseUrl); Object.values(pathMappings.paths).forEach(paths => paths.forEach(path => { - basePaths.push(join(baseUrl, extractPathPrefix(path))); + // We only want base paths that exist and are not files + let basePath = join(baseUrl, extractPathPrefix(path)); + while (basePath !== baseUrl && (!fs.exists(basePath) || fs.stat(basePath).isFile())) { + basePath = fs.dirname(basePath); + } + basePaths.push(basePath); })); } basePaths.sort(); // Get the paths in order with the shorter ones first. diff --git a/packages/compiler-cli/ngcc/test/entry_point_finder/directory_walker_entry_point_finder_spec.ts b/packages/compiler-cli/ngcc/test/entry_point_finder/directory_walker_entry_point_finder_spec.ts index 7e58681923..bcf79801b3 100644 --- a/packages/compiler-cli/ngcc/test/entry_point_finder/directory_walker_entry_point_finder_spec.ts +++ b/packages/compiler-cli/ngcc/test/entry_point_finder/directory_walker_entry_point_finder_spec.ts @@ -166,6 +166,30 @@ runInEachFileSystem(() => { ]); }); + it('should handle pathMappings that map to files or non-existent directories', () => { + const basePath = _Abs('/path_mapped/node_modules'); + const pathMappings: PathMappings = { + baseUrl: '/path_mapped/dist', + paths: { + '@test': ['pkg2/fesm2015/pkg2.js'], + '@missing': ['pkg3'], + } + }; + loadTestFiles([ + ...createPackage(_Abs('/path_mapped/node_modules'), 'test', []), + ...createPackage(_Abs('/path_mapped/dist'), 'pkg2'), + ]); + resolver = new DependencyResolver( + fs, logger, {esm2015: new EsmDependencyHost(fs, new ModuleResolver(fs, pathMappings))}); + const finder = new DirectoryWalkerEntryPointFinder( + fs, config, logger, resolver, basePath, pathMappings); + const {entryPoints} = finder.findEntryPoints(); + expect(dumpEntryPointPaths(basePath, entryPoints)).toEqual([ + ['../dist/pkg2', '../dist/pkg2'], + ['test', 'test'], + ]); + }); + function createPackage( basePath: AbsoluteFsPath, packageName: string, deps: string[] = []): TestFile[] { return [ diff --git a/packages/compiler-cli/ngcc/test/entry_point_finder/targeted_entry_point_finder_spec.ts b/packages/compiler-cli/ngcc/test/entry_point_finder/targeted_entry_point_finder_spec.ts index 496fae8dc2..351ed99bc8 100644 --- a/packages/compiler-cli/ngcc/test/entry_point_finder/targeted_entry_point_finder_spec.ts +++ b/packages/compiler-cli/ngcc/test/entry_point_finder/targeted_entry_point_finder_spec.ts @@ -146,15 +146,18 @@ runInEachFileSystem(() => { paths: { '@x/*': ['*'], '@y/*/test': ['lib/*/test'], + '@z/*': ['../dist/moo/../*'], } }; loadTestFiles([ ...createPackage( - _Abs('/path_mapped/node_modules'), 'test', ['pkg1', '@x/pkg2', '@y/pkg3/test']), + _Abs('/path_mapped/node_modules'), 'test', + ['pkg1', '@x/pkg2', '@y/pkg3/test', '@z/pkg5']), ...createPackage(_Abs('/path_mapped/node_modules'), 'pkg1'), ...createPackage(_Abs('/path_mapped/dist'), 'pkg2', ['pkg4']), ...createPackage(_Abs('/path_mapped/dist/pkg2/node_modules'), 'pkg4'), ...createPackage(_Abs('/path_mapped/dist/lib/pkg3'), 'test'), + ...createPackage(_Abs('/path_mapped/dist'), 'pkg5'), ]); resolver = new DependencyResolver( fs, logger, {esm2015: new EsmDependencyHost(fs, new ModuleResolver(fs, pathMappings))}); @@ -166,6 +169,31 @@ runInEachFileSystem(() => { ['../dist/pkg2/node_modules/pkg4', '../dist/pkg2/node_modules/pkg4'], ['../dist/pkg2', '../dist/pkg2'], ['../dist/lib/pkg3/test', '../dist/lib/pkg3/test'], + ['../dist/pkg5', '../dist/pkg5'], + ['test', 'test'], + ]); + }); + + it('should handle pathMappings that map to files or non-existent directories', () => { + const basePath = _Abs('/path_mapped/node_modules'); + const targetPath = _Abs('/path_mapped/node_modules/test'); + const pathMappings: PathMappings = { + baseUrl: '/path_mapped/dist', + paths: { + '@test': ['pkg2/fesm2015/pkg2.js'], + '@missing': ['pkg3'], + } + }; + loadTestFiles([ + ...createPackage(_Abs('/path_mapped/node_modules'), 'test', []), + ...createPackage(_Abs('/path_mapped/dist'), 'pkg2'), + ]); + resolver = new DependencyResolver( + fs, logger, {esm2015: new EsmDependencyHost(fs, new ModuleResolver(fs, pathMappings))}); + const finder = new TargetedEntryPointFinder( + fs, config, logger, resolver, basePath, targetPath, pathMappings); + const {entryPoints} = finder.findEntryPoints(); + expect(dumpEntryPointPaths(basePath, entryPoints)).toEqual([ ['test', 'test'], ]); });