Fix asset transformation in the presence of resolution-aware asset variants (#151932)

For the necessary background knowledge, see the flutter.dev content on [Resolution-aware image assets](https://docs.flutter.dev/ui/assets/assets-and-images#resolution-aware) and [Conditional bundling of assets based on app flavor](https://docs.flutter.dev/ui/assets/assets-and-images#conditional-bundling-of-assets-based-on-app-flavor) if you don't have a basic understanding of these features.

Fixes https://github.com/flutter/flutter/issues/151813 by using unique temporary directories, per asset file, for transformations. Currently, only a single directory is used and the name of the temporary files was based only on the basename of files. This means that `assets/image.png` and `assets/2x/image.png` would share an output path (`<temp dir path>/image.png`), causing a race. If this quick and rough explanation is a bit confusing, the original issue—#151813—provides a full repro and correct identification of the exact cause of the failure that can occur in the asset transformation process.
This commit is contained in:
Andrew Kolos
2024-07-22 16:46:19 -07:00
committed by GitHub
parent a8d11d2134
commit ebe53d570a
7 changed files with 136 additions and 48 deletions

View File

@@ -154,7 +154,8 @@ Future<Depfile> copyAssets(
);
doCopy = false;
if (failure != null) {
throwToolExit(failure.message);
throwToolExit('User-defined transformation of asset "${entry.key}" failed.\n'
'${failure.message}');
}
}
case AssetKind.font:

View File

@@ -55,16 +55,21 @@ final class AssetTransformer {
required Logger logger,
}) async {
String getTempFilePath(int transformStep) {
final Directory tempDirectory = _fileSystem.systemTempDirectory.createTempSync();
int transformStep = 0;
File nextTempFile() {
final String basename = _fileSystem.path.basename(asset.path);
final String ext = _fileSystem.path.extension(asset.path);
return '$basename-transformOutput$transformStep$ext';
final File result = tempDirectory.childFile('$basename-transformOutput$transformStep$ext');
transformStep++;
return result;
}
File tempInputFile = _fileSystem.systemTempDirectory.childFile(getTempFilePath(0));
File tempInputFile = nextTempFile();
await asset.copy(tempInputFile.path);
File tempOutputFile = _fileSystem.systemTempDirectory.childFile(getTempFilePath(1));
ErrorHandlingFileSystem.deleteIfExists(tempOutputFile);
File tempOutputFile = nextTempFile();
final Stopwatch stopwatch = Stopwatch()..start();
try {
@@ -78,10 +83,7 @@ final class AssetTransformer {
);
if (transformerFailure != null) {
return AssetTransformationFailure(
'User-defined transformation of asset "${asset.path}" failed.\n'
'${transformerFailure.message}',
);
return AssetTransformationFailure(transformerFailure.message);
}
ErrorHandlingFileSystem.deleteIfExists(tempInputFile);
@@ -90,15 +92,13 @@ final class AssetTransformer {
await tempOutputFile.copy(outputPath);
} else {
tempInputFile = tempOutputFile;
tempOutputFile = _fileSystem.systemTempDirectory.childFile(getTempFilePath(i+2));
ErrorHandlingFileSystem.deleteIfExists(tempOutputFile);
tempOutputFile = nextTempFile();
}
}
logger.printTrace("Finished transforming asset at path '${asset.path}' (${stopwatch.elapsedMilliseconds}ms)");
} finally {
ErrorHandlingFileSystem.deleteIfExists(tempInputFile);
ErrorHandlingFileSystem.deleteIfExists(tempOutputFile);
ErrorHandlingFileSystem.deleteIfExists(tempDirectory, recursive: true);
}
return null;
@@ -124,6 +124,11 @@ final class AssetTransformer {
...transformerArguments,
];
// Delete the output file if it already exists for whatever reason.
// With this, we can check for the existence of the file after transformation
// to make sure the transformer produced an output file.
ErrorHandlingFileSystem.deleteIfExists(output);
logger.printTrace("Transforming asset using command '${command.join(' ')}'");
final ProcessResult result = await _processManager.run(
command,

View File

@@ -213,7 +213,8 @@ Future<void> writeBundle(
);
doCopy = false;
if (failure != null) {
throwToolExit(failure.message);
throwToolExit('User-defined transformation of asset "${entry.key}" failed.\n'
'${failure.message}');
}
case AssetKind.font:
break;