Skip to content

Commit 79df3bd

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. (cherry picked from commit 30758d1)
1 parent 038d40e commit 79df3bd

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
@@ -670,11 +670,35 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
670670

671671
if (success && migrations) {
672672
for (const migration of migrations) {
673+
// Resolve the package from the workspace root, as otherwise it will be resolved from the temp
674+
// installed CLI version.
675+
const packagePath = require.resolve(migration.package, { paths: [this.context.root] });
676+
let migrations;
677+
678+
// Check if it is a package-local location
679+
const localMigrations = path.join(packagePath, migration.collection);
680+
if (fs.existsSync(localMigrations)) {
681+
migrations = localMigrations;
682+
} else {
683+
// Try to resolve from package location.
684+
// This avoids issues with package hoisting.
685+
try {
686+
migrations = require.resolve(migration.collection, { paths: [packagePath] });
687+
} catch (e) {
688+
if (e.code === 'MODULE_NOT_FOUND') {
689+
this.logger.error(`Migrations for package (${migration.package}) were not found.`);
690+
} else {
691+
this.logger.error(
692+
`Unable to resolve migrations for package (${migration.package}). [${e.message}]`,
693+
);
694+
}
695+
696+
return 1;
697+
}
698+
}
673699
const result = await this.executeMigrations(
674700
migration.package,
675-
// Resolve the collection from the workspace root, as otherwise it will be resolved from the temp
676-
// installed CLI version.
677-
require.resolve(migration.collection, { paths: [this.context.root] }),
701+
migrations,
678702
new semver.Range('>' + migration.from + ' <=' + migration.to),
679703
options.createCommits,
680704
);

0 commit comments

Comments
 (0)