Skip to content

Commit 30758d1

Browse files
committed
fix(@angular/cli): resolve migration collections from containing package
This change ensures that migration collections defined in a package's `ng-update` metadata are resolved from the containing package. This avoids issues that may arise from package manager hoisting behavior which can result in the wrong migration collection being chosen. The `--migrate-only` option already contained this resolution logic and now the full update contains it as well.
1 parent ab84fc5 commit 30758d1

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

packages/angular/cli/commands/update-impl.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,35 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
650650

651651
if (success && migrations) {
652652
for (const migration of migrations) {
653+
// Resolve the package from the workspace root, as otherwise it will be resolved from the temp
654+
// installed CLI version.
655+
const packagePath = require.resolve(migration.package, { paths: [this.context.root] });
656+
let migrations;
657+
658+
// Check if it is a package-local location
659+
const localMigrations = path.join(packagePath, migration.collection);
660+
if (fs.existsSync(localMigrations)) {
661+
migrations = localMigrations;
662+
} else {
663+
// Try to resolve from package location.
664+
// This avoids issues with package hoisting.
665+
try {
666+
migrations = require.resolve(migration.collection, { paths: [packagePath] });
667+
} catch (e) {
668+
if (e.code === 'MODULE_NOT_FOUND') {
669+
this.logger.error(`Migrations for package (${migration.package}) were not found.`);
670+
} else {
671+
this.logger.error(
672+
`Unable to resolve migrations for package (${migration.package}). [${e.message}]`,
673+
);
674+
}
675+
676+
return 1;
677+
}
678+
}
653679
const result = await this.executeMigrations(
654680
migration.package,
655-
// Resolve the collection from the workspace root, as otherwise it will be resolved from the temp
656-
// installed CLI version.
657-
require.resolve(migration.collection, { paths: [this.context.root] }),
681+
migrations,
658682
new semver.Range('>' + migration.from + ' <=' + migration.to),
659683
options.createCommits,
660684
);

0 commit comments

Comments
 (0)