Skip to content

Commit ab37f42

Browse files
authored
[flutter_tools] Include data assets from hooks when pubspec.yaml is empty (#193434)
When `flutterManifest.isEmpty` (for example, a `pubspec.yaml` without a `flutter:` section), `ManifestAssetBundle.build` previously returned early with an empty `AssetManifest.bin`, ignoring any data assets produced by build/link hooks (`flutterHookResult.dataAssets`). Bug: * #146264 ### Changes - Only short-circuit with an empty manifest in `ManifestAssetBundle.build` when both `flutterManifest.isEmpty` and `flutterHookResult?.dataAssets.isEmpty ?? true`. - Skip `computeTransitiveDependencies` when `flutterManifest.isEmpty`. - Add a unit test in `asset_bundle_test.dart` verifying that data assets from hooks are bundled when `pubspec.yaml` is empty. ### PR Stack | # | PR | Branch | Commit | | :-: | :--- | :--- | :--- | | **1** | **👉 #193434 (this PR)** | `data-assets-empty-pubspec` | 62199c4 | | 2 | #193435 | `data-assets-flutter-test` | 25be1ab | | 3 | #193436 | `data-assets-dev-dependencies-release-test` | 79abad9 | | 4 | #193437 | `data-assets-widget-preview` | 98f1001 | ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
1 parent 8650cf2 commit ab37f42

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎packages/flutter_tools/lib/src/asset.dart‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ class ManifestAssetBundle implements AssetBundle {
337337
// device.
338338
_lastBuildTimestamp = DateTime.now();
339339
_lastHookResult = flutterHookResult ?? FlutterHookResult.empty();
340-
if (flutterManifest.isEmpty) {
340+
if (flutterManifest.isEmpty && (flutterHookResult?.dataAssets.isEmpty ?? true)) {
341341
final ByteData emptyAssetManifest = const StandardMessageCodec().encodeMessage(
342342
<dynamic, dynamic>{},
343343
)!;
@@ -415,10 +415,9 @@ class ManifestAssetBundle implements AssetBundle {
415415
// dependencies.
416416
// To avoid bundling assets from dev_dependencies and other pub workspace
417417
// packages, we compute the set of transitive dependencies.
418-
final List<Dependency> transitiveDependencies = computeTransitiveDependencies(
419-
flutterProject,
420-
packageConfig,
421-
);
418+
final List<Dependency> transitiveDependencies = flutterManifest.isEmpty
419+
? <Dependency>[]
420+
: computeTransitiveDependencies(flutterProject, packageConfig);
422421
final additionalLicenseFiles = <String, List<File>>{};
423422
for (final dependency in transitiveDependencies) {
424423
if (!includeAssetsFromDevDependencies && dependency.isExclusiveDevDependency) {

‎packages/flutter_tools/test/general.shard/asset_bundle_test.dart‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,51 @@ void main() {
9090
},
9191
);
9292

93+
testUsingContext(
94+
'empty pubspec with data assets from hook',
95+
() async {
96+
writePackageConfigFiles(directory: globals.fs.currentDirectory, mainLibName: 'my_app');
97+
globals.fs.file('pubspec.yaml')
98+
..createSync()
99+
..writeAsStringSync('');
100+
final File dataAssetFile = globals.fs.file('data/foo.txt')
101+
..createSync(recursive: true)
102+
..writeAsStringSync('hello');
103+
104+
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
105+
await bundle.build(
106+
packageConfigPath: '.dart_tool/package_config.json',
107+
targetPlatform: TargetPlatform.tester,
108+
flutterHookResult: FlutterHookResult(
109+
buildStart: DateTime.now(),
110+
buildEnd: DateTime.now(),
111+
dataAssets: <HookAsset>[
112+
HookAsset(file: dataAssetFile.uri, name: 'data/foo.txt', package: 'my_app'),
113+
],
114+
dependencies: <Uri>[dataAssetFile.uri],
115+
),
116+
);
117+
expect(bundle.entries.keys, contains('packages/my_app/data/foo.txt'));
118+
expect(
119+
const StandardMessageCodec().decodeMessage(
120+
ByteData.sublistView(
121+
Uint8List.fromList(await bundle.entries['AssetManifest.bin']!.contentsAsBytes()),
122+
),
123+
),
124+
<Object?, Object?>{
125+
'packages/my_app/data/foo.txt': <Object?>[
126+
<Object?, Object?>{'asset': 'packages/my_app/data/foo.txt'},
127+
],
128+
},
129+
);
130+
},
131+
overrides: <Type, Generator>{
132+
FileSystem: () => testFileSystem,
133+
Platform: () => platform,
134+
ProcessManager: () => FakeProcessManager.any(),
135+
},
136+
);
137+
93138
testUsingContext(
94139
'wildcard directories do not include subdirectories',
95140
() async {

0 commit comments

Comments
 (0)