fix(ngcc): do not crash on overlapping entry-points (#36083)

When two entry-points overlap, ngcc may attempt to process some
files twice. Previously, when this occured ngcc would just exit with an
error preventing any other entry-points from being processed.

This commit changes ngcc so that if `errorOnFailedEntryPoint` is false, it will
simply log an error and continue to process entry-points. This is useful when
ngcc is processing the entire node_modules folder and there are some invalid
entry-points that the project doesn't actually use.

PR Close #36083
This commit is contained in:
Pete Bacon Darwin
2020-03-17 12:32:34 +00:00
committed by Andrew Kushnir
parent ff665b9e6a
commit c9f554cda7
5 changed files with 79 additions and 34 deletions

View File

@ -277,7 +277,8 @@ export function mainNgcc({basePath, targetEntryPointPath,
// The function for creating the `compile()` function.
const createCompileFn: CreateCompileFn = onTaskCompleted => {
const fileWriter = getFileWriter(fileSystem, pkgJsonUpdater, createNewEntryPointFormats);
const fileWriter = getFileWriter(
fileSystem, logger, pkgJsonUpdater, createNewEntryPointFormats, errorOnFailedEntryPoint);
const transformer = new Transformer(fileSystem, logger);
return (task: Task) => {
@ -362,10 +363,11 @@ function getPackageJsonUpdater(inParallel: boolean, fs: FileSystem): PackageJson
}
function getFileWriter(
fs: FileSystem, pkgJsonUpdater: PackageJsonUpdater,
createNewEntryPointFormats: boolean): FileWriter {
return createNewEntryPointFormats ? new NewEntryPointFileWriter(fs, pkgJsonUpdater) :
new InPlaceFileWriter(fs);
fs: FileSystem, logger: Logger, pkgJsonUpdater: PackageJsonUpdater,
createNewEntryPointFormats: boolean, errorOnFailedEntryPoint: boolean): FileWriter {
return createNewEntryPointFormats ?
new NewEntryPointFileWriter(fs, logger, errorOnFailedEntryPoint, pkgJsonUpdater) :
new InPlaceFileWriter(fs, logger, errorOnFailedEntryPoint);
}
function getTaskQueue(

View File

@ -1,4 +1,3 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
@ -7,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {FileSystem, absoluteFrom, dirname} from '../../../src/ngtsc/file_system';
import {Logger} from '../logging/logger';
import {EntryPointJsonProperty} from '../packages/entry_point';
import {EntryPointBundle} from '../packages/entry_point_bundle';
import {FileToWrite} from '../rendering/utils';
@ -18,7 +18,9 @@ export const NGCC_BACKUP_EXTENSION = '.__ivy_ngcc_bak';
* a back-up of the original file with an extra `.__ivy_ngcc_bak` extension.
*/
export class InPlaceFileWriter implements FileWriter {
constructor(protected fs: FileSystem) {}
constructor(
protected fs: FileSystem, protected logger: Logger,
protected errorOnFailedEntryPoint: boolean) {}
writeBundle(
_bundle: EntryPointBundle, transformedFiles: FileToWrite[],
@ -30,12 +32,20 @@ export class InPlaceFileWriter implements FileWriter {
this.fs.ensureDir(dirname(file.path));
const backPath = absoluteFrom(`${file.path}${NGCC_BACKUP_EXTENSION}`);
if (this.fs.exists(backPath)) {
throw new Error(
`Tried to overwrite ${backPath} with an ngcc back up file, which is disallowed.`);
if (this.errorOnFailedEntryPoint) {
throw new Error(
`Tried to overwrite ${backPath} with an ngcc back up file, which is disallowed.`);
} else {
this.logger.error(
`Tried to write ${backPath} with an ngcc back up file but it already exists so not writing, nor backing up, ${file.path}.\n` +
`This error may be because two or more entry-points overlap and ngcc has been asked to process some files more than once.\n` +
`You should check other entry-points in this package and set up a config to ignore any that you are not using.`);
}
} else {
if (this.fs.exists(file.path)) {
this.fs.moveFile(file.path, backPath);
}
this.fs.writeFile(file.path, file.contents);
}
if (this.fs.exists(file.path)) {
this.fs.moveFile(file.path, backPath);
}
this.fs.writeFile(file.path, file.contents);
}
}

View File

@ -8,6 +8,7 @@
*/
import {AbsoluteFsPath, FileSystem, absoluteFromSourceFile, dirname, join, relative} from '../../../src/ngtsc/file_system';
import {isDtsPath} from '../../../src/ngtsc/util/src/typescript';
import {Logger} from '../logging/logger';
import {EntryPoint, EntryPointJsonProperty} from '../packages/entry_point';
import {EntryPointBundle} from '../packages/entry_point_bundle';
import {FileToWrite} from '../rendering/utils';
@ -27,7 +28,11 @@ export const NGCC_PROPERTY_EXTENSION = '_ivy_ngcc';
* `InPlaceFileWriter`).
*/
export class NewEntryPointFileWriter extends InPlaceFileWriter {
constructor(fs: FileSystem, private pkgJsonUpdater: PackageJsonUpdater) { super(fs); }
constructor(
fs: FileSystem, logger: Logger, errorOnFailedEntryPoint: boolean,
private pkgJsonUpdater: PackageJsonUpdater) {
super(fs, logger, errorOnFailedEntryPoint);
}
writeBundle(
bundle: EntryPointBundle, transformedFiles: FileToWrite[],