Skip to content

Commit

Permalink
print traces when transforming an asset (flutter#146374)
Browse files Browse the repository at this point in the history
From flutter#143348 (comment):

> before we ship, we should add a printTrace to the tool about each asset transformer we're invoking and the path/arguments we called it with

I think this is a good idea since asset transformers can be arbitrary Dart programs�meaning that a lot can go wrong when running them. For example, they can hang indefinitely or perform some sort of I/O that later results in a tool crash. Knowing that asset transformation was involved when debugging a crash (or a slow/stuck `flutter build`) could be useful, so I think adding a `printTrace` or two is a good idea (or at least not a bad one).
  • Loading branch information
andrewkolos committed Apr 22, 2024
1 parent af439ac commit 4c46030
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
Expand Up @@ -150,6 +150,7 @@ Future<Depfile> copyAssets(
outputPath: file.path,
workingDirectory: environment.projectDir.path,
transformerEntries: entry.value.transformers,
logger: environment.logger,
);
doCopy = false;
if (failure != null) {
Expand Down
Expand Up @@ -52,6 +52,7 @@ final class AssetTransformer {
required String outputPath,
required String workingDirectory,
required List<AssetTransformerEntry> transformerEntries,
required Logger logger,
}) async {

String getTempFilePath(int transformStep) {
Expand All @@ -65,13 +66,15 @@ final class AssetTransformer {
File tempOutputFile = _fileSystem.systemTempDirectory.childFile(getTempFilePath(1));
ErrorHandlingFileSystem.deleteIfExists(tempOutputFile);

final Stopwatch stopwatch = Stopwatch()..start();
try {
for (final (int i, AssetTransformerEntry transformer) in transformerEntries.indexed) {
final AssetTransformationFailure? transformerFailure = await _applyTransformer(
asset: tempInputFile,
output: tempOutputFile,
transformer: transformer,
workingDirectory: workingDirectory,
logger: logger,
);

if (transformerFailure != null) {
Expand All @@ -91,6 +94,8 @@ final class AssetTransformer {
ErrorHandlingFileSystem.deleteIfExists(tempOutputFile);
}
}

logger.printTrace("Finished transforming asset at path '${asset.path}' (${stopwatch.elapsedMilliseconds}ms)");
} finally {
ErrorHandlingFileSystem.deleteIfExists(tempInputFile);
ErrorHandlingFileSystem.deleteIfExists(tempOutputFile);
Expand All @@ -104,6 +109,7 @@ final class AssetTransformer {
required File output,
required AssetTransformerEntry transformer,
required String workingDirectory,
required Logger logger,
}) async {
final List<String> transformerArguments = <String>[
'--input=${asset.absolute.path}',
Expand All @@ -118,6 +124,7 @@ final class AssetTransformer {
...transformerArguments,
];

logger.printTrace("Transforming asset using command '${command.join(' ')}'");
final ProcessResult result = await _processManager.run(
command,
workingDirectory: workingDirectory,
Expand Down Expand Up @@ -199,6 +206,7 @@ final class DevelopmentAssetTransformer {
outputPath: output.path,
transformerEntries: transformerEntries,
workingDirectory: workingDirectory,
logger: _logger,
);
if (failure != null) {
_logger.printError(failure.message);
Expand Down
1 change: 1 addition & 0 deletions packages/flutter_tools/lib/src/bundle_builder.dart
Expand Up @@ -209,6 +209,7 @@ Future<void> writeBundle(
outputPath: file.path,
workingDirectory: projectDir.path,
transformerEntries: entry.value.transformers,
logger: logger,
);
doCopy = false;
if (failure != null) {
Expand Down
Expand Up @@ -71,6 +71,7 @@ void main() {
],
)
],
logger: logger,
);

expect(transformationFailure, isNull, reason: logger.errorText);
Expand Down Expand Up @@ -127,6 +128,7 @@ void main() {
args: <String>[],
)
],
logger: BufferLogger.test(),
);

expect(asset, exists);
Expand Down Expand Up @@ -187,6 +189,7 @@ Something went wrong''');
args: <String>[],
)
],
logger: BufferLogger.test(),
);

expect(processManager, hasNoRemainingExpectations);
Expand Down Expand Up @@ -286,6 +289,7 @@ Transformation failed, but I forgot to exit with a non-zero code.'''
args: <String>[],
),
],
logger: BufferLogger.test(),
);

expect(processManager, hasNoRemainingExpectations);
Expand Down Expand Up @@ -364,6 +368,7 @@ Transformation failed, but I forgot to exit with a non-zero code.'''
args: <String>[],
),
],
logger: BufferLogger.test(),
);

expect(failure, isNotNull);
Expand Down

0 comments on commit 4c46030

Please sign in to comment.