print traces when transforming an asset (#146374)

From https://github.com/flutter/flutter/issues/143348#issuecomment-2016047148:

> 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).
This commit is contained in:
Andrew Kolos
2024-04-22 09:37:24 -07:00
committed by GitHub
parent af439ac374
commit 4c46030927
4 changed files with 15 additions and 0 deletions

View File

@@ -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) {

View File

@@ -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) {
@@ -65,6 +66,7 @@ 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(
@@ -72,6 +74,7 @@ final class AssetTransformer {
output: tempOutputFile,
transformer: transformer,
workingDirectory: workingDirectory,
logger: logger,
);
if (transformerFailure != null) {
@@ -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);
@@ -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}',
@@ -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,
@@ -199,6 +206,7 @@ final class DevelopmentAssetTransformer {
outputPath: output.path,
transformerEntries: transformerEntries,
workingDirectory: workingDirectory,
logger: _logger,
);
if (failure != null) {
_logger.printError(failure.message);

View File

@@ -209,6 +209,7 @@ Future<void> writeBundle(
outputPath: file.path,
workingDirectory: projectDir.path,
transformerEntries: entry.value.transformers,
logger: logger,
);
doCopy = false;
if (failure != null) {

View File

@@ -71,6 +71,7 @@ void main() {
],
)
],
logger: logger,
);
expect(transformationFailure, isNull, reason: logger.errorText);
@@ -127,6 +128,7 @@ void main() {
args: <String>[],
)
],
logger: BufferLogger.test(),
);
expect(asset, exists);
@@ -187,6 +189,7 @@ Something went wrong''');
args: <String>[],
)
],
logger: BufferLogger.test(),
);
expect(processManager, hasNoRemainingExpectations);
@@ -286,6 +289,7 @@ Transformation failed, but I forgot to exit with a non-zero code.'''
args: <String>[],
),
],
logger: BufferLogger.test(),
);
expect(processManager, hasNoRemainingExpectations);
@@ -364,6 +368,7 @@ Transformation failed, but I forgot to exit with a non-zero code.'''
args: <String>[],
),
],
logger: BufferLogger.test(),
);
expect(failure, isNotNull);