fix(ngcc): do not write entry-point manifest outside node_modules (#36299)

Fixes #36296

PR Close #36299
This commit is contained in:
Pete Bacon Darwin 2020-03-28 17:32:15 +00:00 committed by Alex Rickabaugh
parent 5ac308060d
commit c6dd900f60
3 changed files with 37 additions and 6 deletions

View File

@ -99,6 +99,10 @@ export class EntryPointManifest {
* @param entryPoints A collection of entry-points to record in the manifest.
*/
writeEntryPointManifest(basePath: AbsoluteFsPath, entryPoints: EntryPoint[]): void {
if (this.fs.basename(basePath) !== 'node_modules') {
return;
}
const lockFileHash = this.computeLockFileHash(basePath);
if (lockFileHash === null) {
return;

View File

@ -103,15 +103,37 @@ runInEachFileSystem(() => {
expect(entryPoints).toEqual([]);
});
it('should write an entry-point manifest file if none was found', () => {
const basePath = _Abs('/sub_entry_points/node_modules');
it('should write an entry-point manifest file if none was found and basePath is `node_modules`',
() => {
const basePath = _Abs('/sub_entry_points/node_modules');
loadTestFiles([
...createPackage(basePath, 'common'),
...createPackage(fs.resolve(basePath, 'common'), 'http', ['common']),
...createPackage(
fs.resolve(basePath, 'common/http'), 'testing', ['common/http', 'common/testing']),
...createPackage(fs.resolve(basePath, 'common'), 'testing', ['common']),
{name: _Abs('/sub_entry_points/yarn.lock'), contents: 'MOCK LOCK FILE'},
]);
spyOn(manifest, 'readEntryPointsUsingManifest').and.callThrough();
spyOn(manifest, 'writeEntryPointManifest').and.callThrough();
const finder = new DirectoryWalkerEntryPointFinder(
fs, config, logger, resolver, manifest, basePath, undefined);
finder.findEntryPoints();
expect(manifest.readEntryPointsUsingManifest).toHaveBeenCalled();
expect(manifest.writeEntryPointManifest).toHaveBeenCalled();
expect(fs.exists(_Abs('/sub_entry_points/node_modules/__ngcc_entry_points__.json')))
.toBe(true);
});
it('should not write an entry-point manifest file if basePath is not `node_modules`', () => {
const basePath = _Abs('/sub_entry_points/dist');
loadTestFiles([
...createPackage(basePath, 'common'),
...createPackage(fs.resolve(basePath, 'common'), 'http', ['common']),
...createPackage(
fs.resolve(basePath, 'common/http'), 'testing', ['common/http', 'common/testing']),
...createPackage(fs.resolve(basePath, 'common'), 'testing', ['common']),
{name: _Abs('/sub_entry_points/yarn.lock'), contents: 'MOCM LOCK FILE'},
{name: _Abs('/sub_entry_points/yarn.lock'), contents: 'MOCK LOCK FILE'},
]);
spyOn(manifest, 'readEntryPointsUsingManifest').and.callThrough();
spyOn(manifest, 'writeEntryPointManifest').and.callThrough();
@ -120,8 +142,7 @@ runInEachFileSystem(() => {
finder.findEntryPoints();
expect(manifest.readEntryPointsUsingManifest).toHaveBeenCalled();
expect(manifest.writeEntryPointManifest).toHaveBeenCalled();
expect(fs.exists(_Abs('/sub_entry_points/node_modules/__ngcc_entry_points__.json')))
.toBe(true);
expect(fs.exists(_Abs('/sub_entry_points/dist/__ngcc_entry_points__.json'))).toBe(false);
});
it('should read from the entry-point manifest file if found', () => {
@ -132,7 +153,7 @@ runInEachFileSystem(() => {
...createPackage(
fs.resolve(basePath, 'common/http'), 'testing', ['common/http', 'common/testing']),
...createPackage(fs.resolve(basePath, 'common'), 'testing', ['common']),
{name: _Abs('/sub_entry_points/yarn.lock'), contents: 'MOCM LOCK FILE'},
{name: _Abs('/sub_entry_points/yarn.lock'), contents: 'MOCK LOCK FILE'},
]);
const finder = new DirectoryWalkerEntryPointFinder(
fs, config, logger, resolver, manifest, basePath, undefined);

View File

@ -165,6 +165,12 @@ runInEachFileSystem(() => {
expect(fs.exists(_Abs('/project/node_modules/__ngcc_entry_points__.json'))).toBe(false);
});
it('should do nothing if the basePath is not node_modules', () => {
fs.writeFile(_Abs('/project/yarn.lock'), 'LOCK FILE CONTENTS');
manifest.writeEntryPointManifest(_Abs('/project/dist'), []);
expect(fs.exists(_Abs('/project/dist/__ngcc_entry_points__.json'))).toBe(false);
});
it('should write an __ngcc_entry_points__.json file below the base path if there is a yarn.lock file',
() => {
fs.writeFile(_Abs('/project/yarn.lock'), 'LOCK FILE CONTENTS');