Skip to content

Commit a873a27

Browse files
authored
[tool] makes listing a shader also as an asset a build failure (#176866)
fixes #126829 Turns listing an asset as a `shader` and an `asset` a build error. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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 documentation (doc comments with `///`). - [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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent a27b7cd commit a873a27

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

packages/flutter_tools/lib/src/asset.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,26 @@ class ManifestAssetBundle implements AssetBundle {
10701070
});
10711071

10721072
for (final Uri shaderUri in flutterManifest.shaders) {
1073+
for (final AssetsEntry assetEntry in flutterManifest.assets) {
1074+
final String assetPath = assetEntry.uri.path;
1075+
final String shaderPath = shaderUri.path;
1076+
if (assetPath == shaderPath) {
1077+
_logger.printError(
1078+
'Error: Shader "$shaderPath" is also defined as an asset. Shaders '
1079+
'should only be defined in the "shaders" section of the '
1080+
'pubspec.yaml, not in the "assets" section.',
1081+
);
1082+
return null;
1083+
}
1084+
if (assetPath.endsWith('/') && shaderPath.startsWith(assetPath)) {
1085+
_logger.printError(
1086+
'Error: Shader "$shaderPath" is included in the asset directory '
1087+
'"$assetPath". Shaders should only be defined in the "shaders" '
1088+
'section of the pubspec.yaml, not in the "assets" section.',
1089+
);
1090+
return null;
1091+
}
1092+
}
10731093
_parseAssetFromFile(
10741094
packageConfig,
10751095
flutterManifest,

packages/flutter_tools/test/general.shard/asset_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,66 @@ flutter:
375375
expect(assetBundle.entries.keys, contains('shaders/$shader'));
376376
}
377377
});
378+
379+
testWithoutContext('fails if shader is also in assets', () async {
380+
writePackageConfigFiles(directory: fileSystem.currentDirectory, mainLibName: 'my_package');
381+
fileSystem.file('pubspec.yaml').writeAsStringSync(r'''
382+
name: my_package
383+
flutter:
384+
shaders:
385+
- shaders/my_shader.frag
386+
assets:
387+
- shaders/my_shader.frag
388+
''');
389+
fileSystem.file('shaders/my_shader.frag').createSync(recursive: true);
390+
final assetBundle = ManifestAssetBundle(
391+
logger: logger,
392+
fileSystem: fileSystem,
393+
platform: platform,
394+
flutterRoot: flutterRoot,
395+
);
396+
397+
final int result = await assetBundle.build(
398+
packageConfigPath: '.dart_tool/package_config.json',
399+
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
400+
);
401+
expect(result, isNot(0));
402+
expect(
403+
logger.errorText,
404+
contains('Error: Shader "shaders/my_shader.frag" is also defined as an asset.'),
405+
);
406+
});
407+
408+
testWithoutContext('fails if shader is in asset directory', () async {
409+
writePackageConfigFiles(directory: fileSystem.currentDirectory, mainLibName: 'my_package');
410+
fileSystem.file('pubspec.yaml').writeAsStringSync(r'''
411+
name: my_package
412+
flutter:
413+
shaders:
414+
- shaders/my_shader.frag
415+
assets:
416+
- shaders/
417+
''');
418+
fileSystem.file('shaders/my_shader.frag').createSync(recursive: true);
419+
final assetBundle = ManifestAssetBundle(
420+
logger: logger,
421+
fileSystem: fileSystem,
422+
platform: platform,
423+
flutterRoot: flutterRoot,
424+
);
425+
426+
final int result = await assetBundle.build(
427+
packageConfigPath: '.dart_tool/package_config.json',
428+
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
429+
);
430+
expect(result, isNot(0));
431+
expect(
432+
logger.errorText,
433+
contains(
434+
'Error: Shader "shaders/my_shader.frag" is included in the asset directory "shaders/".',
435+
),
436+
);
437+
});
378438
});
379439
}
380440
}

0 commit comments

Comments
 (0)