Skip to content

Commit 2c79ca0

Browse files
crisbetoAndrewKushnir
authored andcommitted
fix(migrations): remove error for no matching files in control flow migration (#64253) (#64314)
Now that the control flow migration is running as a part of `ng update`, we need to be a bit forgiving about there not being any matching files since it can break the entire update process. These changes switch the error to a warning and it counts the files for the entire workspace, not the individual projects. PR Close #64253 PR Close #64314
1 parent 6e4bcc7 commit 2c79ca0

2 files changed

Lines changed: 29 additions & 46 deletions

File tree

‎packages/core/schematics/migrations/control-flow-migration/index.ts‎

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
1010
import {join, relative} from 'node:path';
11+
import ts from 'typescript';
1112

1213
import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host';
1314

@@ -28,6 +29,12 @@ export function migrate(options: Options): Rule {
2829
const basePath = process.cwd();
2930
let pathToMigrate: string | undefined;
3031
if (options.path) {
32+
if (options.path.startsWith('..')) {
33+
throw new SchematicsException(
34+
'Cannot run control flow migration outside of the current project.',
35+
);
36+
}
37+
3138
pathToMigrate = normalizePath(join(basePath, options.path));
3239
if (pathToMigrate.trim() !== '') {
3340
allPaths.push(pathToMigrate);
@@ -38,60 +45,45 @@ export function migrate(options: Options): Rule {
3845
}
3946

4047
if (!allPaths.length) {
41-
throw new SchematicsException(
42-
'Could not find any tsconfig file. Cannot run the http providers migration.',
48+
context.logger.warn(
49+
'Could not find any tsconfig file. Cannot run the control flow migration.',
4350
);
51+
return;
4452
}
4553

4654
let errors: string[] = [];
55+
let sourceFilesCount = 0;
4756

4857
for (const tsconfigPath of allPaths) {
49-
const migrateErrors = runControlFlowMigration(
50-
tree,
51-
tsconfigPath,
52-
basePath,
53-
pathToMigrate,
54-
options,
55-
);
58+
const program = createMigrationProgram(tree, tsconfigPath, basePath);
59+
const sourceFiles = program
60+
.getSourceFiles()
61+
.filter(
62+
(sourceFile) =>
63+
(pathToMigrate ? sourceFile.fileName.startsWith(pathToMigrate) : true) &&
64+
canMigrateFile(basePath, sourceFile, program),
65+
);
66+
67+
const migrateErrors = runControlFlowMigration(tree, sourceFiles, basePath, options);
5668
errors = [...errors, ...migrateErrors];
69+
sourceFilesCount += sourceFiles.length;
5770
}
5871

5972
if (errors.length > 0) {
6073
context.logger.warn(`WARNING: ${errors.length} errors occurred during your migration:\n`);
61-
errors.forEach((err: string) => {
62-
context.logger.warn(err);
63-
});
74+
errors.forEach((err) => context.logger.warn(err));
75+
} else if (sourceFilesCount === 0) {
76+
context.logger.warn('Control flow migration did not find any files to migrate');
6477
}
6578
};
6679
}
6780

6881
function runControlFlowMigration(
6982
tree: Tree,
70-
tsconfigPath: string,
83+
sourceFiles: ts.SourceFile[],
7184
basePath: string,
72-
pathToMigrate?: string,
7385
schematicOptions?: Options,
7486
): string[] {
75-
if (schematicOptions?.path?.startsWith('..')) {
76-
throw new SchematicsException(
77-
'Cannot run control flow migration outside of the current project.',
78-
);
79-
}
80-
81-
const program = createMigrationProgram(tree, tsconfigPath, basePath);
82-
const sourceFiles = program
83-
.getSourceFiles()
84-
.filter(
85-
(sourceFile) =>
86-
(pathToMigrate ? sourceFile.fileName.startsWith(pathToMigrate) : true) &&
87-
canMigrateFile(basePath, sourceFile, program),
88-
);
89-
90-
if (sourceFiles.length === 0) {
91-
throw new SchematicsException(
92-
`Could not find any files to migrate under the path ${pathToMigrate}. Cannot run the control flow migration.`,
93-
);
94-
}
9587
const analysis = new Map<string, AnalyzedFile>();
9688
const migrateErrors = new Map<string, MigrateError[]>();
9789

‎packages/core/schematics/test/control_flow_migration_spec.ts‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6926,9 +6926,7 @@ describe('control flow migration (ng generate)', () => {
69266926
});
69276927

69286928
describe('path', () => {
6929-
it('should throw an error if no files match the passed-in path', async () => {
6930-
let error: string | null = null;
6931-
6929+
it('should warn if no files match the passed-in path', async () => {
69326930
writeFile(
69336931
'dir.ts',
69346932
`
@@ -6938,15 +6936,8 @@ describe('control flow migration (ng generate)', () => {
69386936
`,
69396937
);
69406938

6941-
try {
6942-
await runMigration('./foo');
6943-
} catch (e: any) {
6944-
error = e.message;
6945-
}
6946-
6947-
expect(error).toMatch(
6948-
/Could not find any files to migrate under the path .*\/foo\. Cannot run the control flow migration/,
6949-
);
6939+
await runMigration('./foo');
6940+
expect(warnOutput).toContain('Control flow migration did not find any files to migrate');
69506941
});
69516942

69526943
it('should throw an error if a path outside of the project is passed in', async () => {

0 commit comments

Comments
 (0)