fix(ngcc): capture path-mapped entry-points that start with same string (#35592)

Previously if there were two path-mapped libraries that are in
different directories but the path of one started with same string
as the path of the other, we would incorrectly return the shorter
path - e.g. `dist/my-lib` and `dist/my-lib-second`. This was because
the list of `basePaths` was searched in ascending alphabetic order and
we were using `startsWith()` to match the path.

Now the `basePaths` are searched in reverse alphabetic order so the
longer path will be matched correctly.

// FW-1873

Fixes #35536

PR Close #35592
This commit is contained in:
Pete Bacon Darwin
2020-02-20 21:03:14 +01:00
committed by Miško Hevery
parent 53f059ee8f
commit 71b5970450
3 changed files with 52 additions and 10 deletions

View File

@ -189,8 +189,8 @@ runInEachFileSystem(() => {
fs, config, logger, resolver, basePath, pathMappings);
const {entryPoints} = finder.findEntryPoints();
expect(dumpEntryPointPaths(basePath, entryPoints)).toEqual([
['../dist/pkg2', '../dist/pkg2'],
['test', 'test'],
['../dist/pkg2', '../dist/pkg2'],
]);
});

View File

@ -228,6 +228,39 @@ runInEachFileSystem(() => {
expect(entryPoint.package).toEqual(_Abs('/path_mapped/dist/primary'));
});
it('should correctly compute an entry-point whose path starts with the same string as another entry-point, via pathMappings',
() => {
const basePath = _Abs('/path_mapped/node_modules');
const targetPath = _Abs('/path_mapped/node_modules/test');
const pathMappings: PathMappings = {
baseUrl: '/path_mapped/dist',
paths: {
'lib1': ['my-lib/my-lib', 'my-lib'],
'lib2': ['my-lib/a', 'my-lib/a'],
'lib3': ['my-lib/b', 'my-lib/b'],
'lib4': ['my-lib-other/my-lib-other', 'my-lib-other']
}
};
loadTestFiles([
...createPackage(_Abs('/path_mapped/node_modules'), 'test', ['lib2', 'lib4']),
...createPackage(_Abs('/path_mapped/dist/my-lib'), 'my-lib'),
...createPackage(_Abs('/path_mapped/dist/my-lib'), 'a'),
...createPackage(_Abs('/path_mapped/dist/my-lib'), 'b'),
...createPackage(_Abs('/path_mapped/dist/my-lib-other'), 'my-lib-other'),
]);
const srcHost = new EsmDependencyHost(fs, new ModuleResolver(fs, pathMappings));
const dtsHost = new DtsDependencyHost(fs, pathMappings);
resolver = new DependencyResolver(fs, logger, {esm2015: srcHost}, dtsHost);
const finder = new TargetedEntryPointFinder(
fs, config, logger, resolver, basePath, targetPath, pathMappings);
const {entryPoints} = finder.findEntryPoints();
expect(dumpEntryPointPaths(basePath, entryPoints)).toEqual([
['../dist/my-lib/a', '../dist/my-lib/a'],
['../dist/my-lib-other/my-lib-other', '../dist/my-lib-other/my-lib-other'],
['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');