Skip to content

Commit 5319428

Browse files
JoostKalan-agius4
authored andcommitted
fix(@ngtools/webpack): do not run ngcc when node_modules does not exist
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.
1 parent dbe0dc1 commit 5319428

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

packages/ngtools/webpack/src/ngcc_processor.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type ResolverWithOptions = ReturnType<Compiler['resolverFactory']['get']>;
3232
export class NgccProcessor {
3333
private _processedModules = new Set<string>();
3434
private _logger: NgccLogger;
35-
private _nodeModulesDirectory: string;
35+
private _nodeModulesDirectory: string | null;
3636

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

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

@@ -162,18 +164,20 @@ export class NgccProcessor {
162164
}
163165
}
164166

165-
/** Process a module and it's depedencies. */
167+
/** Process a module and its dependencies. */
166168
processModule(
167169
moduleName: string,
168170
resolvedModule: ts.ResolvedModule | ts.ResolvedTypeReferenceDirective,
169171
): void {
170172
const resolvedFileName = resolvedModule.resolvedFileName;
171173
if (
174+
!this._nodeModulesDirectory ||
172175
!resolvedFileName ||
173176
moduleName.startsWith('.') ||
174177
this._processedModules.has(resolvedFileName)
175178
) {
176-
// Skip when module is unknown, relative or NGCC compiler is not found or already processed.
179+
// Skip when module_modules directory is not present, module is unknown, relative or the
180+
// NGCC compiler is not found or already processed.
177181
return;
178182
}
179183

@@ -232,7 +236,7 @@ export class NgccProcessor {
232236
}
233237
}
234238

235-
private findNodeModulesDirectory(startPoint: string): string {
239+
private findNodeModulesDirectory(startPoint: string): string | null {
236240
let current = startPoint;
237241
while (path.dirname(current) !== current) {
238242
const nodePath = path.join(current, 'node_modules');
@@ -243,7 +247,7 @@ export class NgccProcessor {
243247
current = path.dirname(current);
244248
}
245249

246-
throw new Error(`Cannot locate the 'node_modules' directory.`);
250+
return null;
247251
}
248252

249253
private findPackageManagerLockFile(projectBasePath: string): {

0 commit comments

Comments
 (0)