fix(ngcc): handle bad path mappings when finding entry-points (#36331)
Previously, a bad baseUrl or path mapping passed to an `EntryPointFinder` could cause the original `sourceDirectory` to be superceded by a higher directory. This could result in none of the sourceDirectory entry-points being processed. Now missing basePaths computed from path-mappings are discarded with a warning. Further, if the `baseUrl` is the root directory then a warning is given as this is most likely an error in the tsconfig.json. Resolves #36313 Resolves #36283 PR Close #36331
This commit is contained in:

committed by
Alex Rickabaugh

parent
03a3c753bb
commit
7bb35889fa
@ -340,7 +340,7 @@ runInEachFileSystem(() => {
|
||||
const pathMappings: PathMappings = {
|
||||
baseUrl: '/path_mapped/dist',
|
||||
paths: {
|
||||
'@test': ['pkg2/fesm2015/pkg2.js'],
|
||||
'@test': ['pkg2/pkg2.d.ts'],
|
||||
'@missing': ['pkg3'],
|
||||
}
|
||||
};
|
||||
@ -371,6 +371,10 @@ runInEachFileSystem(() => {
|
||||
fesm2015: `./fesm2015/${packageName}.js`,
|
||||
})
|
||||
},
|
||||
{
|
||||
name: _Abs(`${basePath}/${packageName}/${packageName}.d.ts`),
|
||||
contents: deps.map((dep, i) => `import * as i${i} from '${dep}';`).join('\n'),
|
||||
},
|
||||
{
|
||||
name: _Abs(`${basePath}/${packageName}/fesm2015/${packageName}.js`),
|
||||
contents: deps.map((dep, i) => `import * as i${i} from '${dep}';`).join('\n'),
|
||||
|
142
packages/compiler-cli/ngcc/test/entry_point_finder/utils_spec.ts
Normal file
142
packages/compiler-cli/ngcc/test/entry_point_finder/utils_spec.ts
Normal file
@ -0,0 +1,142 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* 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 {absoluteFrom, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
|
||||
|
||||
import {runInEachFileSystem} from '../../../src/ngtsc/file_system/testing';
|
||||
import {getBasePaths} from '../../src/entry_point_finder/utils';
|
||||
import {MockLogger} from '../helpers/mock_logger';
|
||||
|
||||
runInEachFileSystem(() => {
|
||||
let _: typeof absoluteFrom;
|
||||
let logger: MockLogger;
|
||||
|
||||
beforeEach(() => {
|
||||
_ = absoluteFrom;
|
||||
logger = new MockLogger();
|
||||
});
|
||||
|
||||
describe('getBasePaths', () => {
|
||||
it('should just return the `sourceDirectory if there are no `pathMappings', () => {
|
||||
const sourceDirectory = _('/path/to/project/node_modules');
|
||||
const basePaths = getBasePaths(logger, sourceDirectory, undefined);
|
||||
expect(basePaths).toEqual([sourceDirectory]);
|
||||
});
|
||||
|
||||
it('should use each path mapping prefix and sort in descending order', () => {
|
||||
const projectDirectory = _('/path/to/project');
|
||||
const fs = getFileSystem();
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'dist-1'));
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'sub-folder/dist-2'));
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'libs'));
|
||||
|
||||
const sourceDirectory = _('/path/to/project/node_modules');
|
||||
const pathMappings = {
|
||||
baseUrl: projectDirectory,
|
||||
paths: {'@dist': ['dist-1', 'sub-folder/dist-2'], '@lib/*': ['libs/*']}
|
||||
};
|
||||
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
|
||||
expect(basePaths).toEqual([
|
||||
fs.resolve(projectDirectory, 'sub-folder/dist-2'),
|
||||
sourceDirectory,
|
||||
fs.resolve(projectDirectory, 'libs'),
|
||||
fs.resolve(projectDirectory, 'dist-1'),
|
||||
]);
|
||||
});
|
||||
|
||||
it('should discard paths that are already contained by another path', () => {
|
||||
const projectDirectory = _('/path/to/project');
|
||||
const fs = getFileSystem();
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'dist-1'));
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'dist-1/sub-folder'));
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'node_modules/libs'));
|
||||
|
||||
const sourceDirectory = _('/path/to/project/node_modules');
|
||||
const pathMappings = {
|
||||
baseUrl: projectDirectory,
|
||||
paths: {'@dist': ['dist-1', 'dist-1/sub-folder'], '@lib/*': ['node_modules/libs/*']}
|
||||
};
|
||||
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
|
||||
expect(basePaths).toEqual([
|
||||
sourceDirectory,
|
||||
fs.resolve(projectDirectory, 'dist-1'),
|
||||
]);
|
||||
});
|
||||
|
||||
it('should use the containing directory of path mapped files', () => {
|
||||
const projectDirectory = _('/path/to/project');
|
||||
const fs = getFileSystem();
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'dist-1'));
|
||||
fs.writeFile(fs.resolve(projectDirectory, 'dist-1/file.js'), 'dummy content');
|
||||
|
||||
const sourceDirectory = _('/path/to/project/node_modules');
|
||||
const pathMappings = {baseUrl: projectDirectory, paths: {'@dist': ['dist-1/file.js']}};
|
||||
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
|
||||
expect(basePaths).toEqual([
|
||||
sourceDirectory,
|
||||
fs.resolve(projectDirectory, 'dist-1'),
|
||||
]);
|
||||
});
|
||||
|
||||
it('should always include the `sourceDirectory` if it is a node_modules directory in the returned basePaths, even if it is contained by another basePath',
|
||||
() => {
|
||||
const projectDirectory = _('/path/to/project');
|
||||
const sourceDirectory = _('/path/to/project/node_modules');
|
||||
const fs = getFileSystem();
|
||||
fs.ensureDir(fs.resolve(sourceDirectory));
|
||||
|
||||
const pathMappings = {baseUrl: projectDirectory, paths: {'*': ['./*']}};
|
||||
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
|
||||
expect(basePaths).toEqual([
|
||||
sourceDirectory,
|
||||
projectDirectory,
|
||||
]);
|
||||
});
|
||||
|
||||
it('should log a warning if baseUrl is the root path', () => {
|
||||
const fs = getFileSystem();
|
||||
fs.ensureDir(fs.resolve('/dist'));
|
||||
|
||||
const sourceDirectory = _('/path/to/project/node_modules');
|
||||
const pathMappings = {baseUrl: _('/'), paths: {'@dist': ['dist']}};
|
||||
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
|
||||
expect(basePaths).toEqual([
|
||||
sourceDirectory,
|
||||
fs.resolve('/dist'),
|
||||
]);
|
||||
expect(logger.logs.warn).toEqual([
|
||||
[`The provided pathMappings baseUrl is the root path ${_('/')}.\n` +
|
||||
`This is likely to mess up how ngcc finds entry-points and is probably not correct.\n` +
|
||||
`Please check your path mappings configuration such as in the tsconfig.json file.`]
|
||||
]);
|
||||
});
|
||||
|
||||
it('should discard basePaths that do not exists and log a warning', () => {
|
||||
const projectDirectory = _('/path/to/project');
|
||||
const fs = getFileSystem();
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'dist-1'));
|
||||
fs.ensureDir(fs.resolve(projectDirectory, 'sub-folder'));
|
||||
|
||||
const sourceDirectory = _('/path/to/project/node_modules');
|
||||
const pathMappings = {
|
||||
baseUrl: projectDirectory,
|
||||
paths: {'@dist': ['dist-1', 'sub-folder/dist-2'], '@lib/*': ['libs/*']}
|
||||
};
|
||||
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
|
||||
expect(basePaths).toEqual([
|
||||
sourceDirectory,
|
||||
fs.resolve(projectDirectory, 'dist-1'),
|
||||
]);
|
||||
expect(logger.logs.warn).toEqual([
|
||||
[`The basePath "${fs.resolve(projectDirectory, 'sub-folder/dist-2')}" computed from baseUrl "${projectDirectory}" and path mapping "sub-folder/dist-2" does not exist in the file-system.\n` +
|
||||
`It will not be scanned for entry-points.`],
|
||||
[`The basePath "${fs.resolve(projectDirectory, 'libs')}" computed from baseUrl "${projectDirectory}" and path mapping "libs/*" does not exist in the file-system.\n` +
|
||||
`It will not be scanned for entry-points.`],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user