Skip to content

Commit 0670bc0

Browse files
committed
[flutter_tools] Simplify ExtensionArtifactManager stamp verification and drop sha256Url
Simplifies `ExtensionArtifactManager` by removing `_fetchRemoteSha256` and `sha256Url` handling, unifying cached single-file and extracted archive verification via `.sha256` stamp files in `_isArtifactCachedAndVerified`, deleting `_verifyCachedFile` and `_verifyCachedDirectory`, and enforcing built-in artifact precedence in `resolveExtensionBuildTargetArtifacts`. Part of flutter#190755 Part of flutter#190754
1 parent 03e14ce commit 0670bc0

4 files changed

Lines changed: 222 additions & 324 deletions

File tree

‎packages/flutter_tools/lib/src/build_system/targets/extension.dart‎

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ Directory resolveExtensionOutputDir({
5353
/// Resolves all engine and host artifacts declared on [target] using
5454
/// [artifacts], [buildMode], and optional downloaded [extensionArtifacts].
5555
///
56-
/// Throws a [ToolExit] if any requested artifact name is invalid or unsupported.
56+
/// Built-in engine and host artifacts always take precedence over
57+
/// [extensionArtifacts]. Throws a [ToolExit] if any requested artifact name is
58+
/// neither a supported built-in artifact nor present in [extensionArtifacts].
5759
// TODO(bkonyi): Download required artifacts on demand if not already cached.
5860
Map<String, String> resolveExtensionBuildTargetArtifacts({
5961
required Artifacts artifacts,
@@ -64,21 +66,22 @@ Map<String, String> resolveExtensionBuildTargetArtifacts({
6466
final TargetPlatform? targetPlatform = _tryResolveTargetPlatform(target.targetPlatform);
6567
final resolvedArtifacts = <String, String>{...extensionArtifacts};
6668
for (final ExtensionArtifact artifact in target.artifacts) {
67-
if (extensionArtifacts.containsKey(artifact.name)) {
68-
continue;
69+
if (_builtInArtifacts[artifact.name] case final Artifact builtIn) {
70+
resolvedArtifacts[artifact.name] = artifacts.getArtifactPath(
71+
builtIn,
72+
platform: targetPlatform,
73+
mode: buildMode,
74+
);
75+
} else if (!extensionArtifacts.containsKey(artifact.name)) {
76+
throwToolExit('Unsupported artifact "${artifact.name}" requested by a tool extension.');
6977
}
70-
resolvedArtifacts[artifact.name] = artifacts.getArtifactPath(
71-
_resolveArtifact(artifact),
72-
platform: targetPlatform,
73-
mode: buildMode,
74-
);
7578
}
7679
for (final ExtensionHostArtifact artifact in target.hostArtifacts) {
77-
if (extensionArtifacts.containsKey(artifact.name)) {
78-
continue;
80+
if (_builtInHostArtifacts[artifact.name] case final HostArtifact builtIn) {
81+
resolvedArtifacts[artifact.name] = artifacts.getHostArtifact(builtIn).path;
82+
} else if (!extensionArtifacts.containsKey(artifact.name)) {
83+
throwToolExit('Unsupported host artifact "${artifact.name}" requested by a tool extension.');
7984
}
80-
final HostArtifact hostArtifact = _resolveHostArtifact(artifact);
81-
resolvedArtifacts[artifact.name] = artifacts.getHostArtifact(hostArtifact).path;
8285
}
8386
return resolvedArtifacts;
8487
}
@@ -95,19 +98,15 @@ TargetPlatform? _tryResolveTargetPlatform(String platformName) {
9598
}
9699
}
97100

98-
Artifact _resolveArtifact(ExtensionArtifact artifact) => switch (artifact.name) {
99-
'flutterPatchedSdkPath' => Artifact.flutterPatchedSdkPath,
100-
'genSnapshot' => Artifact.genSnapshot,
101-
'icuData' => Artifact.icuData,
102-
'linuxDesktopPath' => Artifact.linuxDesktopPath,
103-
'linuxHeaders' => Artifact.linuxHeaders,
104-
final String name => throwToolExit('Unsupported artifact "$name" requested by a tool extension.'),
101+
const _builtInArtifacts = <String, Artifact>{
102+
'flutterPatchedSdkPath': Artifact.flutterPatchedSdkPath,
103+
'genSnapshot': Artifact.genSnapshot,
104+
'icuData': Artifact.icuData,
105+
'linuxDesktopPath': Artifact.linuxDesktopPath,
106+
'linuxHeaders': Artifact.linuxHeaders,
105107
};
106108

107-
HostArtifact _resolveHostArtifact(ExtensionHostArtifact artifact) => switch (artifact.name) {
108-
'impellerc' => HostArtifact.impellerc,
109-
'libtessellator' => HostArtifact.libtessellator,
110-
final String name => throwToolExit(
111-
'Unsupported host artifact "$name" requested by a tool extension.',
112-
),
109+
const _builtInHostArtifacts = <String, HostArtifact>{
110+
'impellerc': HostArtifact.impellerc,
111+
'libtessellator': HostArtifact.libtessellator,
113112
};

‎packages/flutter_tools/lib/src/experimental/extension_artifact_manager.dart‎

Lines changed: 53 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ class ExtensionArtifactManager {
4141
static const String _hiddenExtensionsCacheDirName = '.flutter_tool_extensions';
4242
static const String _artifactsCacheSubdirName = 'artifacts';
4343
static const String _defaultEngineVersion = 'unknown';
44-
static const String _zipExtension = '.zip';
4544
static const String _extractedDirName = 'extracted';
4645
static const String _sha256StampSuffix = '.sha256';
4746
static const String _lockFileSuffix = '.lock';
48-
static final RegExp _whitespaceRegExp = RegExp(r'\s+');
4947

5048
final ExtensionManager _extensionManager;
5149
final FeatureFlags _featureFlags;
@@ -214,6 +212,13 @@ class ExtensionArtifactManager {
214212
];
215213

216214
for (final dep in filteredDeps) {
215+
if (resolvedArtifacts.containsKey(dep.name)) {
216+
logger.printWarning(
217+
'Artifact "${dep.name}" from extension "${ext.extensionName}" conflicts with an '
218+
'artifact of the same name from another extension and will be ignored.',
219+
);
220+
continue;
221+
}
217222
resolvedArtifacts[dep.name] = await _ensureArtifactCached(
218223
ext.extensionName,
219224
engineVersion,
@@ -309,18 +314,16 @@ class ExtensionArtifactManager {
309314
final Directory artifactDir = engineDir.childDirectory(dep.name);
310315
final File stampFile = engineDir.childFile('.${dep.name}$_sha256StampSuffix');
311316
final File lockFile = engineDir.childFile('.${dep.name}$_lockFileSuffix');
312-
final bool isZip = dep.url.path.endsWith(_zipExtension);
317+
final bool isZip = dep.isZipArchive;
313318
final String resolvedPath = isZip ? artifactDir.path : artifactFile.path;
314319

315320
if (!force &&
316-
await _isArtifactCachedAndVerified(
317-
artifactDir: artifactDir,
318-
artifactFile: artifactFile,
319-
dep: dep,
321+
_isArtifactCachedAndVerified(
322+
engineDir,
323+
dep,
324+
deleteStaleOnMismatch: false,
320325
extensionName: extensionName,
321-
isZip: isZip,
322326
logger: logger,
323-
stampFile: stampFile,
324327
)) {
325328
return resolvedPath;
326329
}
@@ -331,14 +334,12 @@ class ExtensionArtifactManager {
331334
logger: logger,
332335
scope: () async {
333336
if (!force &&
334-
await _isArtifactCachedAndVerified(
335-
artifactDir: artifactDir,
336-
artifactFile: artifactFile,
337-
dep: dep,
337+
_isArtifactCachedAndVerified(
338+
engineDir,
339+
dep,
340+
deleteStaleOnMismatch: true,
338341
extensionName: extensionName,
339-
isZip: isZip,
340342
logger: logger,
341-
stampFile: stampFile,
342343
)) {
343344
return;
344345
}
@@ -350,7 +351,7 @@ class ExtensionArtifactManager {
350351
'Downloading artifact "${dep.name}" for extension "$extensionName"...',
351352
);
352353
try {
353-
final String expectedSha = dep.sha256?.toLowerCase() ?? await _fetchRemoteSha256(dep);
354+
final String expectedSha = dep.sha256.toLowerCase();
354355
await _downloadAndVerifyToStagingFile(
355356
dep: dep,
356357
expectedSha: expectedSha,
@@ -378,167 +379,54 @@ class ExtensionArtifactManager {
378379
return resolvedPath;
379380
}
380381

381-
/// Checks whether [dep] is already cached and matches its expected SHA-256
382-
/// checksum.
382+
/// Checks whether [dep] is already cached in [versionDir] and its `.sha256`
383+
/// stamp file matches [ArtifactDependency.sha256].
383384
///
384-
/// When [dep] provides an inline [ArtifactDependency.sha256], single-file
385-
/// artifacts are hashed directly via [_verifyCachedFile] and extracted `.zip`
386-
/// directories are verified against [stampFile] via [_verifyCachedDirectory].
387-
/// When [dep] uses a remote [ArtifactDependency.sha256Url], offline cache
388-
/// hits rely on the previously recorded checksum in [stampFile] so already
389-
/// cached artifacts do not require network access to verify.
390-
Future<bool> _isArtifactCachedAndVerified({
391-
required Directory artifactDir,
392-
required File artifactFile,
393-
required ArtifactDependency dep,
385+
/// When [deleteStaleOnMismatch] is `true` (inside the cross-process lock),
386+
/// deletes any stale cached file/directory and stamp file if the stamp does
387+
/// not match [ArtifactDependency.sha256].
388+
bool _isArtifactCachedAndVerified(
389+
Directory versionDir,
390+
ArtifactDependency dep, {
391+
required bool deleteStaleOnMismatch,
394392
required String extensionName,
395-
required bool isZip,
396393
required Logger logger,
397-
required File stampFile,
398-
}) async {
399-
if (dep.sha256 case final String sha) {
400-
final String expectedSha = sha.toLowerCase();
401-
if (!isZip && artifactFile.existsSync()) {
402-
return _verifyCachedFile(
403-
artifactFile: artifactFile,
404-
dep: dep,
405-
expectedSha: expectedSha,
406-
extensionName: extensionName,
407-
logger: logger,
408-
stampFile: stampFile,
409-
);
410-
}
411-
if (isZip && stampFile.existsSync() && artifactDir.existsSync()) {
412-
return _verifyCachedDirectory(
413-
artifactDir: artifactDir,
414-
dep: dep,
415-
expectedSha: expectedSha,
416-
extensionName: extensionName,
417-
logger: logger,
418-
stampFile: stampFile,
419-
);
420-
}
394+
}) {
395+
final bool isZip = dep.isZipArchive;
396+
final FileSystemEntity target = isZip
397+
? versionDir.childDirectory(dep.name)
398+
: versionDir.childFile(dep.name);
399+
final File stampFile = versionDir.childFile('.${dep.name}$_sha256StampSuffix');
400+
if (!target.existsSync() || !stampFile.existsSync()) {
421401
return false;
422402
}
423-
424-
if (stampFile.existsSync()) {
403+
try {
425404
final String recordedSha = stampFile.readAsStringSync().trim().toLowerCase();
426-
if (ArtifactDependency.isValidSha256(recordedSha)) {
427-
if (isZip && artifactDir.existsSync()) {
428-
logger.printTrace(
429-
'Unzipped artifact "${dep.name}" for extension "$extensionName" already cached and verified.',
430-
);
431-
return true;
432-
}
433-
if (!isZip && artifactFile.existsSync()) {
434-
return _verifyCachedFile(
435-
artifactFile: artifactFile,
436-
dep: dep,
437-
expectedSha: recordedSha,
438-
extensionName: extensionName,
439-
logger: logger,
440-
stampFile: stampFile,
441-
);
442-
}
405+
if (recordedSha == dep.sha256.toLowerCase()) {
406+
logger.printTrace(
407+
'Artifact "${dep.name}" for extension "$extensionName" already cached and verified.',
408+
);
409+
return true;
443410
}
411+
} on FileSystemException {
412+
// Fall through to re-download.
444413
}
445-
return false;
446-
}
447-
448-
/// Computes the SHA-256 digest of a cached single-file artifact [artifactFile]
449-
/// and compares it against [expectedSha].
450-
///
451-
/// Deletes [artifactFile] and [stampFile] and returns `false` if the digest
452-
/// does not match so the caller can re-download the artifact cleanly.
453-
Future<bool> _verifyCachedFile({
454-
required File artifactFile,
455-
required ArtifactDependency dep,
456-
required String expectedSha,
457-
required String extensionName,
458-
required Logger logger,
459-
required File stampFile,
460-
}) async {
461-
final Digest existingDigest = await sha256.bind(artifactFile.openRead()).first;
462-
if (existingDigest.toString().toLowerCase() == expectedSha) {
414+
if (deleteStaleOnMismatch) {
463415
logger.printTrace(
464-
'Artifact "${dep.name}" for extension "$extensionName" already cached and verified.',
416+
'Cached artifact "${dep.name}" failed SHA-256 verification; re-downloading.',
465417
);
466-
return true;
467-
}
468-
logger.printTrace('Cached artifact "${dep.name}" failed SHA-256 verification; re-downloading.');
469-
artifactFile.deleteSync();
470-
if (stampFile.existsSync()) {
471-
stampFile.deleteSync();
472-
}
473-
return false;
474-
}
475-
476-
/// Checks whether the digest recorded in [stampFile] for an extracted `.zip`
477-
/// artifact directory [artifactDir] matches [expectedSha].
478-
///
479-
/// Deletes [artifactDir] and [stampFile] and returns `false` if the stamp does
480-
/// not match.
481-
bool _verifyCachedDirectory({
482-
required Directory artifactDir,
483-
required ArtifactDependency dep,
484-
required String expectedSha,
485-
required String extensionName,
486-
required Logger logger,
487-
required File stampFile,
488-
}) {
489-
final String existingSha = stampFile.readAsStringSync().trim().toLowerCase();
490-
if (existingSha == expectedSha) {
491-
logger.printTrace(
492-
'Unzipped artifact "${dep.name}" for extension "$extensionName" already cached and verified.',
493-
);
494-
return true;
495-
}
496-
logger.printTrace(
497-
'Cached unzipped artifact "${dep.name}" failed SHA-256 verification; re-downloading.',
498-
);
499-
artifactDir.deleteSync(recursive: true);
500-
stampFile.deleteSync();
501-
return false;
502-
}
503-
504-
/// Downloads and parses a remote SHA-256 checksum from [ArtifactDependency.sha256Url].
505-
///
506-
/// Supports both raw 64-character hex strings and `sha256sum`-formatted lines
507-
/// (`<hex> <filename>`). Throws a [ToolExit] on network failure, non-200 HTTP
508-
/// status, or malformed checksum content.
509-
Future<String> _fetchRemoteSha256(ArtifactDependency dep) async {
510-
final Uri sha256Url = dep.sha256Url!;
511-
final String body;
512-
try {
513-
final HttpClientRequest request = await _httpClient.getUrl(sha256Url);
514-
final HttpClientResponse response = await request.close();
515-
if (response.statusCode != HttpStatus.ok) {
516-
await response.drain<void>();
517-
throwToolExit(
518-
'Failed to download SHA-256 checksum for artifact "${dep.name}" from $sha256Url: '
519-
'HTTP ${response.statusCode}',
520-
);
418+
try {
419+
if (target.existsSync()) {
420+
target.deleteSync(recursive: isZip);
421+
}
422+
if (stampFile.existsSync()) {
423+
stampFile.deleteSync();
424+
}
425+
} on FileSystemException {
426+
// Ignore cleanup errors; _installStagedArtifact will overwrite.
521427
}
522-
body = await utf8.decodeStream(response);
523-
} on IOException catch (e) {
524-
throwToolExit(
525-
'Failed to download SHA-256 checksum for artifact "${dep.name}" from $sha256Url: $e',
526-
);
527-
} on OSError catch (e) {
528-
throwToolExit(
529-
'Failed to download SHA-256 checksum for artifact "${dep.name}" from $sha256Url: $e',
530-
);
531428
}
532-
final String trimmed = body.trim();
533-
final String token = trimmed.isEmpty
534-
? ''
535-
: trimmed.split(_whitespaceRegExp).first.toLowerCase();
536-
if (!ArtifactDependency.isValidSha256(token)) {
537-
throwToolExit(
538-
'Invalid SHA-256 checksum for artifact "${dep.name}" from $sha256Url: "$trimmed"',
539-
);
540-
}
541-
return token;
429+
return false;
542430
}
543431

544432
/// Streams the artifact from [ArtifactDependency.url] into [stagingFile] while

‎packages/flutter_tools/test/commands.shard/hermetic/tool_extensions_build_test.dart‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,14 @@ void main() {
545545
final fakeArtifactManager = _FakeExtensionArtifactManager(
546546
downloadedArtifacts: const <String, String>{
547547
'custom-ext-artifact': '/cache/custom-ext-artifact',
548+
'genSnapshot': '/cache/shadowed-gen-snapshot',
548549
},
549550
);
550551
const target = ExtensionBuildTarget(
551-
artifacts: <ExtensionArtifact>{ExtensionArtifact('custom-ext-artifact')},
552+
artifacts: <ExtensionArtifact>{
553+
BuiltInArtifacts.genSnapshot,
554+
ExtensionArtifact('custom-ext-artifact'),
555+
},
552556
description: 'Custom hermetic build target.',
553557
name: 'custom-hermetic-build',
554558
outputDir:
@@ -581,7 +585,10 @@ void main() {
581585
);
582586
expect(
583587
fakeBuildManager.lastBuildArgs!['resolvedArtifacts'],
584-
equals(const <String, String>{'custom-ext-artifact': '/cache/custom-ext-artifact'}),
588+
equals(const <String, String>{
589+
'custom-ext-artifact': '/cache/custom-ext-artifact',
590+
'genSnapshot': 'Artifact.genSnapshot.TargetPlatform.linux_x64.release',
591+
}),
585592
);
586593
},
587594
overrides: <Type, Generator>{

0 commit comments

Comments
 (0)