Skip to content

Commit 8604847

Browse files
authored
[native_assets_cli] Validate assets for linking (#2113)
We were missing some validation for assets for linking: * `BuildOutput` both code and data assets. * `LinkInput` for code assets. Code assets for linking should not have their link mode checked.
1 parent 8663dfa commit 8604847

File tree

2 files changed

+66
-32
lines changed

2 files changed

+66
-32
lines changed

pkgs/native_assets_cli/lib/src/code_assets/validation.dart

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ import 'config.dart';
99
import 'link_mode.dart';
1010

1111
Future<ValidationErrors> validateCodeAssetBuildInput(BuildInput input) async =>
12-
_validateCodeConfig(
13-
'BuildInput.config.code',
12+
_validateCodeConfig('BuildInput.config.code', input.config.code);
1413

15-
// ignore: deprecated_member_use_from_same_package
16-
input.config.code,
17-
);
18-
19-
Future<ValidationErrors> validateCodeAssetLinkInput(LinkInput input) async =>
20-
_validateCodeConfig('LinkInput.config.code', input.config.code);
14+
Future<ValidationErrors> validateCodeAssetLinkInput(LinkInput input) async => [
15+
..._validateCodeConfig('LinkInput.config.code', input.config.code),
16+
...await _validateCodeAssetLinkInput(input.assets.encodedAssets),
17+
];
2118

2219
ValidationErrors _validateCodeConfig(String inputName, CodeConfig code) {
2320
final errors = <String>[];
@@ -74,13 +71,25 @@ ValidationErrors _validateCodeConfig(String inputName, CodeConfig code) {
7471
return errors;
7572
}
7673

74+
Future<ValidationErrors> _validateCodeAssetLinkInput(
75+
List<EncodedAsset> encodedAssets,
76+
) async => [
77+
for (final asset in encodedAssets)
78+
if (asset.type == CodeAsset.type)
79+
..._validateCodeAssetFile(CodeAsset.fromEncoded(asset)),
80+
];
81+
7782
Future<ValidationErrors> validateCodeAssetBuildOutput(
7883
BuildInput input,
7984
BuildOutput output,
8085
) => _validateCodeAssetBuildOrLinkOutput(
8186
input,
8287
input.config.code,
8388
output.assets.encodedAssets,
89+
[
90+
for (final assetList in output.assets.encodedAssetsForLinking.values)
91+
...assetList,
92+
],
8493
output,
8594
true,
8695
);
@@ -92,6 +101,7 @@ Future<ValidationErrors> validateCodeAssetLinkOutput(
92101
input,
93102
input.config.code,
94103
output.assets.encodedAssets,
104+
[],
95105
output,
96106
false,
97107
);
@@ -121,6 +131,7 @@ Future<ValidationErrors> _validateCodeAssetBuildOrLinkOutput(
121131
HookInput input,
122132
CodeConfig codeConfig,
123133
List<EncodedAsset> encodedAssets,
134+
List<EncodedAsset> encodedAssetsForLinking,
124135
HookOutput output,
125136
bool isBuild,
126137
) async {
@@ -130,49 +141,68 @@ Future<ValidationErrors> _validateCodeAssetBuildOrLinkOutput(
130141

131142
for (final asset in encodedAssets) {
132143
if (asset.type != CodeAsset.type) continue;
133-
_validateCodeAssets(
144+
_validateCodeAsset(
134145
input,
135146
codeConfig,
136147
CodeAsset.fromEncoded(asset),
137148
errors,
138149
ids,
139150
isBuild,
151+
true,
140152
);
141153
_groupCodeAssetsByFilename(
142154
CodeAsset.fromEncoded(asset),
143155
fileNameToEncodedAssetId,
144156
);
145157
}
158+
159+
for (final asset in encodedAssetsForLinking) {
160+
if (asset.type != CodeAsset.type) continue;
161+
_validateCodeAsset(
162+
input,
163+
codeConfig,
164+
CodeAsset.fromEncoded(asset),
165+
errors,
166+
ids,
167+
isBuild,
168+
false,
169+
);
170+
}
146171
_validateNoDuplicateDylibNames(errors, fileNameToEncodedAssetId);
147172
return errors;
148173
}
149174

150-
void _validateCodeAssets(
175+
void _validateCodeAsset(
151176
HookInput input,
152177
CodeConfig codeConfig,
153178
CodeAsset codeAsset,
154179
List<String> errors,
155180
Set<String> ids,
156-
bool isBuild,
181+
bool validateAssetId,
182+
bool validateLinkMode,
157183
) {
158184
final id = codeAsset.id;
159185
final prefix = 'package:${input.packageName}/';
160-
if (isBuild && !id.startsWith(prefix)) {
186+
if (validateAssetId && !id.startsWith(prefix)) {
161187
errors.add('Code asset "$id" does not start with "$prefix".');
162188
}
163189
if (!ids.add(id)) {
164190
errors.add('More than one code asset with same "$id" id.');
165191
}
166192

167-
final preference = codeConfig.linkModePreference;
168-
final linkMode = codeAsset.linkMode;
169-
if ((linkMode is DynamicLoading && preference == LinkModePreference.static) ||
170-
(linkMode is StaticLinking && preference == LinkModePreference.dynamic)) {
171-
errors.add(
172-
'CodeAsset "$id" has a link mode "$linkMode", which '
173-
'is not allowed by by the input link mode preference '
174-
'"$preference".',
175-
);
193+
if (validateLinkMode) {
194+
final preference = codeConfig.linkModePreference;
195+
final linkMode = codeAsset.linkMode;
196+
if ((linkMode is DynamicLoading &&
197+
preference == LinkModePreference.static) ||
198+
(linkMode is StaticLinking &&
199+
preference == LinkModePreference.dynamic)) {
200+
errors.add(
201+
'CodeAsset "$id" has a link mode "$linkMode", which '
202+
'is not allowed by by the input link mode preference '
203+
'"$preference".',
204+
);
205+
}
176206
}
177207

178208
final os = codeAsset.os;
@@ -194,13 +224,17 @@ void _validateCodeAssets(
194224
);
195225
}
196226

227+
errors.addAll(_validateCodeAssetFile(codeAsset));
228+
}
229+
230+
List<String> _validateCodeAssetFile(CodeAsset codeAsset) {
231+
final id = codeAsset.id;
197232
final file = codeAsset.file;
198-
if (file == null && _mustHaveFile(codeAsset.linkMode)) {
199-
errors.add('CodeAsset "$id" has no file.');
200-
}
201-
if (file != null) {
202-
errors.addAll(_validateFile('Code asset "$id" file', file));
203-
}
233+
return [
234+
if (file == null && _mustHaveFile(codeAsset.linkMode))
235+
'CodeAsset "$id" has no file.',
236+
if (file != null) ..._validateFile('Code asset "$id" file', file),
237+
];
204238
}
205239

206240
bool _mustHaveFile(LinkMode linkMode) => switch (linkMode) {

pkgs/native_assets_cli/lib/src/data_assets/validation.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ Future<ValidationErrors> validateDataAssetLinkInput(LinkInput input) async {
2323
Future<ValidationErrors> validateDataAssetBuildOutput(
2424
BuildInput input,
2525
BuildOutput output,
26-
) => _validateDataAssetBuildOrLinkOutput(
27-
input,
28-
output.assets.encodedAssets,
29-
true,
30-
);
26+
) => _validateDataAssetBuildOrLinkOutput(input, [
27+
...output.assets.encodedAssets,
28+
for (final assetList in output.assets.encodedAssetsForLinking.values)
29+
...assetList,
30+
], true);
3131

3232
Future<ValidationErrors> validateDataAssetLinkOutput(
3333
LinkInput input,

0 commit comments

Comments
 (0)