@@ -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
0 commit comments