Skip to content

Commit 5f0baaa

Browse files
Tim Blasikegluneq
authored andcommitted
fix(dart/transform): Ensure template codegen is completed sync
Previously, template codegen was done asynchronously, which could result in reflector state being overwritten and leading to compile errors. Update the codegen to run synchronously to ensure this does not happen. Closes angular#6603
1 parent b5b6ece commit 5f0baaa

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

modules_dart/transform/lib/src/transform/common/logging.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,31 @@ Future logElapsedAsync(Future asyncOperation(),
2222
final timer = new Stopwatch()..start();
2323
final result = await asyncOperation();
2424
timer.stop();
25+
_logElapsed(timer, operationName, assetId);
26+
return result;
27+
}
28+
29+
/// Writes a log entry at `LogLevel.FINE` granularity with the time taken by
30+
/// `operation`.
31+
///
32+
/// Returns the result of executing `operation`.
33+
dynamic logElapsedSync(dynamic operation(),
34+
{String operationName: 'unknown', AssetId assetId}) {
35+
final timer = new Stopwatch()..start();
36+
final result = operation();
37+
timer.stop();
38+
_logElapsed(timer, operationName, assetId);
39+
return result;
40+
}
41+
42+
/// Logs the time since `timer` was started.
43+
void _logElapsed(Stopwatch timer, String operationName, AssetId assetId) {
2544
final buf =
2645
new StringBuffer('[$operationName] took ${timer.elapsedMilliseconds} ms');
2746
if (assetId != null) {
2847
buf.write(' on $assetId');
2948
}
3049
log.fine(buf.toString(), asset: assetId);
31-
return result;
3250
}
3351

3452
/// Prints logged messages to the console.

modules_dart/transform/lib/src/transform/template_compiler/generator.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ Future<Outputs> processTemplates(AssetReader reader, AssetId assetId,
6969

7070
var savedReflectionCapabilities = reflector.reflectionCapabilities;
7171
reflector.reflectionCapabilities = const NullReflectionCapabilities();
72-
final compiledTemplates = await logElapsedAsync(() async {
72+
// Since we need global state to remain consistent here, make sure not to do
73+
// any asynchronous operations here.
74+
final compiledTemplates = logElapsedSync(() {
7375
return templateCompiler.compileTemplatesCodeGen(compileData);
7476
}, operationName: 'compileTemplatesCodegen', assetId: assetId);
7577
reflector.reflectionCapabilities = savedReflectionCapabilities;

0 commit comments

Comments
 (0)