Skip to content

Commit

Permalink
fix(@ngtools/webpack): do not run ngcc when node_modules does not e…
Browse files Browse the repository at this point in the history
…xist

Prior to this change the CLI would fail with an error when ngcc could not
be run due to a missing `node_modules` directory, as is the case with Yarn
Plug'n'Play for example. The workaround was to create an empty `node_modules`
directory, but this is inconvenient and shouldn't make any difference.

This commit removes the error and simply skips ngcc processing.

(cherry picked from commit 5319428)
  • Loading branch information
JoostK authored and alan-agius4 committed Jul 4, 2022
1 parent 094aa16 commit 7aed975
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/ngtools/webpack/src/ngcc_processor.ts
Expand Up @@ -32,7 +32,7 @@ type ResolverWithOptions = ReturnType<Compiler['resolverFactory']['get']>;
export class NgccProcessor {
private _processedModules = new Set<string>();
private _logger: NgccLogger;
private _nodeModulesDirectory: string;
private _nodeModulesDirectory: string | null;

constructor(
private readonly compilerNgcc: typeof import('@angular/compiler-cli/ngcc'),
Expand All @@ -54,8 +54,10 @@ export class NgccProcessor {

/** Process the entire node modules tree. */
process() {
// Under Bazel when running in sandbox mode parts of the filesystem is read-only.
if (process.env.BAZEL_TARGET) {
// Under Bazel when running in sandbox mode parts of the filesystem is read-only, or when using
// Yarn PnP there may not be a node_modules directory. ngcc can't run in those cases, so the
// processing is skipped.
if (process.env.BAZEL_TARGET || !this._nodeModulesDirectory) {
return;
}

Expand Down Expand Up @@ -162,18 +164,20 @@ export class NgccProcessor {
}
}

/** Process a module and it's depedencies. */
/** Process a module and its dependencies. */
processModule(
moduleName: string,
resolvedModule: ts.ResolvedModule | ts.ResolvedTypeReferenceDirective,
): void {
const resolvedFileName = resolvedModule.resolvedFileName;
if (
!this._nodeModulesDirectory ||
!resolvedFileName ||
moduleName.startsWith('.') ||
this._processedModules.has(resolvedFileName)
) {
// Skip when module is unknown, relative or NGCC compiler is not found or already processed.
// Skip when module_modules directory is not present, module is unknown, relative or the
// NGCC compiler is not found or already processed.
return;
}

Expand Down Expand Up @@ -232,7 +236,7 @@ export class NgccProcessor {
}
}

private findNodeModulesDirectory(startPoint: string): string {
private findNodeModulesDirectory(startPoint: string): string | null {
let current = startPoint;
while (path.dirname(current) !== current) {
const nodePath = path.join(current, 'node_modules');
Expand All @@ -243,7 +247,7 @@ export class NgccProcessor {
current = path.dirname(current);
}

throw new Error(`Cannot locate the 'node_modules' directory.`);
return null;
}

private findPackageManagerLockFile(projectBasePath: string): {
Expand Down

0 comments on commit 7aed975

Please sign in to comment.