Skip to content

Commit 0b423d4

Browse files
authored
fix(material/schematics): gracefully skip broken css files (#25767)
1 parent f05685f commit 0b423d4

File tree

9 files changed

+36
-6
lines changed

9 files changed

+36
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Does not compile (missing semicolons) */
2+
.mat-button {
3+
width: 100
4+
height: 100
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Should not be migrated (filename is not .css or .scss) */
2+
.mat-button {
3+
width: 100px;
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Does not compile (missing semicolons) */
2+
.mat-button {
3+
width: 100
4+
height: 100
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Should not be migrated (filename is not .css or .scss) */
2+
.mat-button {
3+
width: 100px;
4+
}

src/cdk/schematics/ng-update/find-stylesheets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {join, Path} from '@angular-devkit/core';
1010
import {Tree} from '@angular-devkit/schematics';
1111

1212
/** Regular expression that matches stylesheet paths */
13-
const STYLESHEET_REGEX = /.*\.(css|scss)/;
13+
const STYLESHEET_REGEX = /.*\.(css|scss)$/;
1414

1515
/**
1616
* Finds stylesheets in the given directory from within the specified tree.

src/material/schematics/ng-generate/mdc-migration/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ jasmine_node_test(
9191
"//src/material/schematics:collection_assets",
9292
"//src/material/schematics:ng_generate_assets",
9393
],
94+
shard_count = 10,
9495
deps = [
9596
":unit_tests_bundle",
9697
# Runtime dependencies needed by the test and actual migration sources. These need

src/material/schematics/ng-generate/mdc-migration/rules/theming-styles.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
2222
this.fileSystem
2323
.edit(stylesheet.filePath)
2424
.remove(stylesheet.start, stylesheet.content.length)
25-
.insertRight(stylesheet.start, this.migrate(stylesheet.content));
25+
.insertRight(stylesheet.start, this.migrate(stylesheet.content, stylesheet.filePath));
2626
}
2727

28-
migrate(styles: string): string {
28+
migrate(styles: string, filename: string): string {
2929
const processor = new postcss.Processor([
3030
{
3131
postcssPlugin: 'theming-styles-migration-plugin',
@@ -37,7 +37,13 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
3737
},
3838
]);
3939

40-
return processor.process(styles, {syntax: scss}).toString();
40+
try {
41+
return processor.process(styles, {syntax: scss}).toString();
42+
} catch (e) {
43+
this.context.logger.error(`${e}`);
44+
this.context.logger.warn(`Failed to process stylesheet: ${filename} (see error above).`);
45+
return styles;
46+
}
4147
}
4248

4349
atUseHandler(atRule: postcss.AtRule) {

src/material/schematics/ng-generate/mdc-migration/rules/ts-migration/runtime-migration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
134134
node: ts.StringLiteralLike | ts.Identifier,
135135
migration: TemplateMigration | ThemingStylesMigration,
136136
) {
137-
let migratedText = migration.migrate(node.text);
137+
let migratedText = migration.migrate(node.text, node.getSourceFile().fileName);
138138
let migratedTextLines = migratedText.split('\n');
139139

140140
// Update quotes based on if its multiline or not to avoid compilation errors

src/material/schematics/ng-update/migrations/legacy-components-v15/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ export class LegacyComponentsMigration extends Migration<null> {
3737
RootExit: root => this._handleRootNode(root, stylesheet.filePath, namespace),
3838
},
3939
]);
40-
processor.process(stylesheet.content, {syntax: scss}).sync();
40+
try {
41+
processor.process(stylesheet.content, {syntax: scss}).sync();
42+
} catch (e) {
43+
this.logger.error(`${e}`);
44+
this.logger.warn(`Failed to process stylesheet: ${stylesheet.filePath} (see error above).`);
45+
}
4146
}
4247

4348
/** Returns the namespace of the given at-rule if it is importing from @angular/material. */

0 commit comments

Comments
 (0)