Skip to content

Commit a81e136

Browse files
committed
fix(migrations): remove error for no matching files in control flow migration (angular#64253)
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 angular#64253
1 parent 660444f commit a81e136

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 '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
@@ -6888,9 +6888,7 @@ describe('control flow migration (ng generate)', () => {
68886888
});
68896889

68906890
describe('path', () => {
6891-
it('should throw an error if no files match the passed-in path', async () => {
6892-
let error: string | null = null;
6893-
6891+
it('should warn if no files match the passed-in path', async () => {
68946892
writeFile(
68956893
'dir.ts',
68966894
`
@@ -6900,15 +6898,8 @@ describe('control flow migration (ng generate)', () => {
69006898
`,
69016899
);
69026900

6903-
try {
6904-
await runMigration('./foo');
6905-
} catch (e: any) {
6906-
error = e.message;
6907-
}
6908-
6909-
expect(error).toMatch(
6910-
/Could not find any files to migrate under the path .*\/foo\. Cannot run the control flow migration/,
6911-
);
6901+
await runMigration('./foo');
6902+
expect(warnOutput).toContain('Control flow migration did not find any files to migrate');
69126903
});
69136904

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

0 commit comments

Comments
 (0)