Skip to content

Commit 62bbce6

Browse files
crisbetommalerba
authored andcommitted
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 470dca1 commit 62bbce6

File tree

2 files changed

+29
-46
lines changed

2 files changed

+29
-46
lines changed

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

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

4148
if (!allPaths.length) {
42-
throw new SchematicsException(
43-
'Could not find any tsconfig file. Cannot run the http providers migration.',
49+
context.logger.warn(
50+
'Could not find any tsconfig file. Cannot run the control flow migration.',
4451
);
52+
return;
4553
}
4654

4755
let errors: string[] = [];
56+
let sourceFilesCount = 0;
4857

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

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

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

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)