From 0833929c997c8a9db11c1e0df9a731ab17b77606 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Thu, 18 Jan 2024 00:35:59 -0800 Subject: [PATCH] Catch UnsupportedError thrown when user provides an asset directory path containing invalid characters (#141214) Fixes https://github.com/flutter/flutter/issues/140092 --- packages/flutter_tools/lib/src/asset.dart | 12 ++++++++-- .../test/general.shard/asset_bundle_test.dart | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/asset.dart b/packages/flutter_tools/lib/src/asset.dart index 9d936156d5f..370b2a6a242 100644 --- a/packages/flutter_tools/lib/src/asset.dart +++ b/packages/flutter_tools/lib/src/asset.dart @@ -912,8 +912,16 @@ class ManifestAssetBundle implements AssetBundle { Package? attributedPackage, List? flavors, }) { - final String directoryPath = _fileSystem.path.join( - assetBase, assetUri.toFilePath(windows: _platform.isWindows)); + final String directoryPath; + try { + directoryPath = _fileSystem.path + .join(assetBase, assetUri.toFilePath(windows: _platform.isWindows)); + } on UnsupportedError catch (e) { + throwToolExit( + 'Unable to search for asset files in directory path "${assetUri.path}". ' + 'Please ensure that this is valid URI that points to a directory ' + 'that is available on the local file system.\nError details:\n$e'); + } if (!_fileSystem.directory(directoryPath).existsSync()) { _logger.printError('Error: unable to find directory entry in pubspec.yaml: $directoryPath'); diff --git a/packages/flutter_tools/test/general.shard/asset_bundle_test.dart b/packages/flutter_tools/test/general.shard/asset_bundle_test.dart index 8864e871fce..117e7c5d4f0 100644 --- a/packages/flutter_tools/test/general.shard/asset_bundle_test.dart +++ b/packages/flutter_tools/test/general.shard/asset_bundle_test.dart @@ -153,6 +153,30 @@ flutter: ProcessManager: () => FakeProcessManager.any(), }); + testUsingContext('throws ToolExit when directory entry contains invalid characters', () async { + testFileSystem.file('.packages').createSync(); + testFileSystem.file('pubspec.yaml') + ..createSync() + ..writeAsStringSync(r''' +name: example +flutter: + assets: + - https://mywebsite.com/images/ +'''); + final AssetBundle bundle = AssetBundleFactory.instance.createBundle(); + expect(() => bundle.build(packagesPath: '.packages'), throwsToolExit( + message: 'Unable to search for asset files in directory path "https%3A//mywebsite.com/images/". ' + 'Please ensure that this is valid URI that points to a directory that is ' + 'available on the local file system.\n' + 'Error details:\n' + 'Unsupported operation: Illegal character in path: https:', + )); + }, overrides: { + FileSystem: () => testFileSystem, + ProcessManager: () => FakeProcessManager.any(), + Platform: () => FakePlatform(operatingSystem: 'windows'), + }); + testUsingContext('handle removal of wildcard directories', () async { globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true); final File pubspec = globals.fs.file('pubspec.yaml')