From aa6f0d051179d31aad2c3be7b79f9fda8de60f34 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 20 Jan 2025 15:17:19 +0000 Subject: [PATCH] fix(@angular-devkit/schematics): ensure collections can be resolved via test runner in pnpm workspaces Currently when operating within a pnpm workspace and leveraging the schematic test runner, there are situations where e.g. `@schematics/angular` cannot be resolved. Consider this pnpm node modules structure: ``` packages/ pwa/ node_modules/@schematics/angular --> .pnpm-store/@schematics/angular/... node_modules/@angular-devkit/schematics --> .pnpm-store/@angular-devkit/schematics/... index_spec.js // trying to call external schematic `@schematics/angular` ``` This above setup will fail because `@schematics/angular` is attempted to be resolved from within the devkit schematics code, which doesn't have access, or a dependency on `@schematics/angular`. We can use the specified collection of the test runner to determine a good "resolution lookup site", similiar to how it happens with the real `ng update` command. --- .../schematics/testing/schematic-test-runner.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/angular_devkit/schematics/testing/schematic-test-runner.ts b/packages/angular_devkit/schematics/testing/schematic-test-runner.ts index 5b1c783f7519..a37a2ed6921b 100644 --- a/packages/angular_devkit/schematics/testing/schematic-test-runner.ts +++ b/packages/angular_devkit/schematics/testing/schematic-test-runner.ts @@ -43,8 +43,8 @@ export class UnitTestTree extends DelegateTree { } export class SchematicTestRunner { - private _engineHost = new NodeModulesTestEngineHost(); - private _engine: SchematicEngine<{}, {}> = new SchematicEngine(this._engineHost); + private _engineHost: NodeModulesTestEngineHost; + private _engine: SchematicEngine<{}, {}>; private _collection: Collection<{}, {}>; private _logger: logging.Logger; @@ -52,6 +52,14 @@ export class SchematicTestRunner { private _collectionName: string, collectionPath: string, ) { + this._engineHost = new NodeModulesTestEngineHost([ + // Leverage the specified collection path as an additional base for resolving other + // collections by name. This is useful in e.g. pnpm workspaces where `@angular-devkit/schematics` + // doesn't necessarily have access to e.g. `@schematics/angular`. + collectionPath, + ]); + this._engine = new SchematicEngine(this._engineHost); + this._engineHost.registerCollection(_collectionName, collectionPath); this._logger = new logging.Logger('test');