Skip to content

Commit

Permalink
fix: do not run ngcc when node_modules does not exist
Browse files Browse the repository at this point in the history
Prior to this change, ng-packagr 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.
  • Loading branch information
JoostK authored and alan-agius4 committed Jul 1, 2022
1 parent 1030616 commit 97beddc
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/lib/ngc/ngcc-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as log from '../utils/log';
// Transform a package and its typings when NGTSC is resolving a module.
export class NgccProcessor {
private _logger: NgccLogger;
private _nodeModulesDirectory: string;
private _nodeModulesDirectory: string | null;
private _entryPointsUrl: string[];
private readonly propertiesToConsider = ['es2015', 'browser', 'module', 'main'];
private skipProcessing = false;
Expand All @@ -33,8 +33,10 @@ export class NgccProcessor {

/** Process the entire node modules tree. */
async process(): Promise<void> {
// 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 @@ -139,16 +141,18 @@ 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.ngccProcessingCache.hasProcessed(moduleName) ||
this._entryPointsUrl.includes(ngUrl(moduleName))
) {
// Skip when module is unknown, relative, an entrypoint or already processed.
// Skip when module_modules directory is not present, module is unknown, relative, an
// entrypoint or already processed.
return;
}

Expand Down Expand Up @@ -201,7 +205,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 @@ -212,7 +216,7 @@ export class NgccProcessor {
current = path.dirname(current);
}

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

Expand Down

0 comments on commit 97beddc

Please sign in to comment.