Skip to content

Commit abfe127

Browse files
authored
[flutter_tools] Fix cache download progress calculation for non-downloading artifacts (flutter#192090)
## Description Fixes an issue where `flutter precache` reported an incorrect artifact progress count (e.g. `[1/3] Web SDK`) and terminated without downloading items 2 or 3. ### Root Cause 1. `ArtifactSet.downloadCount` defaulted to 1, causing non-downloading maintenance artifacts (`LegacyCanvasKitRemover`, `PubDependencies`, `AndroidMavenArtifacts`) to increment the progress total in `Cache.updateAll`. 2. For `flutter precache --web`, `artifactsToUpdate` contained `FlutterWebSdk` (downloadable) along with `LegacyCanvasKitRemover` and `PubDependencies` (non-downloadable cleanup/resolution tasks), resulting in `total = 3`. 3. `FlutterWebSdk` was tracked as `[1/3]`, and the remaining non-downloading artifacts ran without invoking `ArtifactUpdater`, leaving the impression of incomplete progress. ### Changes * Default `ArtifactSet.downloadCount` to 0. * Override `CachedArtifact.downloadCount` to return 1. * In `Cache.updateAll`, compute `total` and progress index based only on artifacts with `downloadCount > 0`. * Add `artifactUpdater` constructor parameter to `Cache` and `Cache.test` for dependency injection. * Add unit tests in `cache_test.dart` verifying `downloadCount` defaults and progress calculation. ## Issues Fixed Fixes flutter#192023 ## 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 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. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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 a48bddc commit abfe127

2 files changed

Lines changed: 168 additions & 35 deletions

File tree

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

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,11 @@ class DevelopmentArtifact {
151151
class Cache {
152152
/// [rootOverride] is configurable for testing.
153153
/// [artifacts] is configurable for testing.
154+
/// [artifactUpdater] is configurable for testing.
154155
Cache({
155156
@protected Directory? rootOverride,
156157
@protected List<ArtifactSet>? artifacts,
158+
@visibleForTesting ArtifactUpdater? artifactUpdater,
157159
required Logger logger,
158160
required FileSystem fileSystem,
159161
required Platform platform,
@@ -167,7 +169,8 @@ class Cache {
167169
_stdio = stdio,
168170
_net = Net(logger: logger, platform: platform),
169171
_fsUtils = FileSystemUtils(fileSystem: fileSystem, platform: platform),
170-
_artifacts = artifacts ?? <ArtifactSet>[];
172+
_artifacts = artifacts ?? <ArtifactSet>[],
173+
_artifactUpdaterOverride = artifactUpdater;
171174

172175
/// Create a [Cache] for testing.
173176
///
@@ -177,6 +180,7 @@ class Cache {
177180
factory Cache.test({
178181
Directory? rootOverride,
179182
List<ArtifactSet>? artifacts,
183+
ArtifactUpdater? artifactUpdater,
180184
Logger? logger,
181185
FileSystem? fileSystem,
182186
Platform? platform,
@@ -199,6 +203,7 @@ class Cache {
199203
return Cache(
200204
rootOverride: rootOverride ?? fileSystem.currentDirectory,
201205
artifacts: artifacts ?? <ArtifactSet>[],
206+
artifactUpdater: artifactUpdater,
202207
logger: logger,
203208
fileSystem: fileSystem,
204209
platform: platform,
@@ -224,7 +229,8 @@ class Cache {
224229
final Net _net;
225230
final FileSystemUtils _fsUtils;
226231

227-
late final ArtifactUpdater _artifactUpdater = _createUpdater();
232+
final ArtifactUpdater? _artifactUpdaterOverride;
233+
late final ArtifactUpdater _artifactUpdater = _artifactUpdaterOverride ?? _createUpdater();
228234

229235
@visibleForTesting
230236
@protected
@@ -798,38 +804,45 @@ class Cache {
798804
}
799805

800806
// Download artifacts and display progress
801-
final int total = artifactsToUpdate.length;
802-
for (var i = 0; i < artifactsToUpdate.length; i++) {
803-
final ArtifactSet artifact = artifactsToUpdate[i];
804-
final int current = i + 1;
805-
806-
// Set progress context for the artifact updater
807-
_artifactUpdater.setProgressContext(
808-
artifactIndex: current,
809-
artifactTotal: total,
810-
downloadTotal: artifact.downloadCount,
811-
);
807+
final int total = artifactsToUpdate
808+
.where((ArtifactSet artifact) => artifact.downloadCount > 0)
809+
.length;
810+
var current = 0;
811+
try {
812+
for (final artifact in artifactsToUpdate) {
813+
if (artifact.downloadCount > 0) {
814+
current += 1;
815+
816+
// Set progress context for the artifact updater
817+
_artifactUpdater.setProgressContext(
818+
artifactIndex: current,
819+
artifactTotal: total,
820+
downloadTotal: artifact.downloadCount,
821+
);
812822

813-
// For artifacts containing multiple downloads, print the artifact name
814-
if (artifact.downloadCount > 1) {
815-
_logger.printStatus('[$current/$total] ${artifact.displayName}');
816-
}
823+
// For artifacts containing multiple downloads, print the artifact name
824+
if (artifact.downloadCount > 1) {
825+
_logger.printStatus('[$current/$total] ${artifact.displayName}');
826+
}
827+
}
817828

818-
try {
819-
await artifact.update(_artifactUpdater, _logger, _fileSystem, _osUtils, offline: offline);
820-
} on SocketException catch (e) {
821-
if (_hostsBlockedInChina.contains(e.address?.host)) {
822-
_logger.printError(
823-
'Failed to retrieve Flutter tool dependencies: ${e.message}.\n'
824-
"If you're in China, please see this page: "
825-
'https://flutter.dev/to/china-setup',
826-
emphasis: true,
827-
);
829+
try {
830+
await artifact.update(_artifactUpdater, _logger, _fileSystem, _osUtils, offline: offline);
831+
} on SocketException catch (e) {
832+
if (_hostsBlockedInChina.contains(e.address?.host)) {
833+
_logger.printError(
834+
'Failed to retrieve Flutter tool dependencies: ${e.message}.\n'
835+
"If you're in China, please see this page: "
836+
'https://flutter.dev/to/china-setup',
837+
emphasis: true,
838+
);
839+
}
840+
rethrow;
828841
}
829-
rethrow;
830842
}
843+
} finally {
844+
_artifactUpdater.resetProgressContext();
831845
}
832-
_artifactUpdater.resetProgressContext();
833846
}
834847

835848
Future<bool> areRemoteArtifactsAvailable({
@@ -899,8 +912,8 @@ abstract class ArtifactSet {
899912

900913
/// The number of individual downloads this artifact will perform.
901914
///
902-
/// Defaults to 1.
903-
int get downloadCount => 1;
915+
/// Defaults to 0.
916+
int get downloadCount => 0;
904917
}
905918

906919
/// An artifact set managed by the cache.
@@ -913,6 +926,12 @@ abstract class CachedArtifact extends ArtifactSet {
913926
@override
914927
final String name;
915928

929+
/// The number of individual downloads this artifact will perform.
930+
///
931+
/// Defaults to 1 for cached artifacts.
932+
@override
933+
int get downloadCount => 1;
934+
916935
@override
917936
String get stampName => name;
918937

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

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,72 @@ void main() {
452452
expect(artifact.displayName, 'fake');
453453
});
454454

455-
testWithoutContext('ArtifactSet.downloadCount defaults to 1', () {
455+
testWithoutContext('ArtifactSet.downloadCount defaults to 0', () {
456+
final artifact = _FakeArtifactSet();
457+
458+
expect(artifact.downloadCount, 0);
459+
});
460+
461+
testWithoutContext('CachedArtifact.downloadCount defaults to 1', () {
456462
final cache = Cache.test(processManager: FakeProcessManager.any());
457463
final artifact = FakeSimpleArtifact(cache);
458464

459465
expect(artifact.downloadCount, 1);
460466
});
461467

468+
testUsingContext('Cache.updateAll calculates progress only for downloading artifacts', () async {
469+
final fileSystem = MemoryFileSystem.test();
470+
final logger = BufferLogger.test();
471+
final artifactUpdater = _FakeProgressRecordingArtifactUpdater();
472+
final artifact1 = FakeSecondaryCachedArtifact()
473+
..upToDate = false
474+
..artifactName = 'downloading_1'
475+
..downloads = 2;
476+
final artifact2 = _FakeArtifactSet(name: 'non_downloading')..upToDate = false;
477+
final artifact3 = FakeSecondaryCachedArtifact()
478+
..upToDate = false
479+
..artifactName = 'downloading_2';
480+
481+
final cacheWithArtifacts = Cache.test(
482+
fileSystem: fileSystem,
483+
logger: logger,
484+
artifacts: <ArtifactSet>[artifact1, artifact2, artifact3],
485+
artifactUpdater: artifactUpdater,
486+
processManager: FakeProcessManager.any(),
487+
);
488+
489+
await cacheWithArtifacts.updateAll(<DevelopmentArtifact>{DevelopmentArtifact.universal});
490+
491+
expect(artifact1.didUpdate, true);
492+
expect(artifact2.didUpdate, true);
493+
expect(artifact3.didUpdate, true);
494+
expect(logger.statusText, contains('[1/2] downloading_1'));
495+
expect(artifactUpdater.progressContexts, <ProgressContext>[
496+
(artifactIndex: 1, artifactTotal: 2, downloadTotal: 2, downloadIndex: 0),
497+
(artifactIndex: 2, artifactTotal: 2, downloadTotal: 1, downloadIndex: 0),
498+
]);
499+
});
500+
501+
testUsingContext('Cache.updateAll succeeds when all artifacts are non-downloading', () async {
502+
final fileSystem = MemoryFileSystem.test();
503+
final artifactUpdater = _FakeProgressRecordingArtifactUpdater();
504+
final artifact1 = _FakeArtifactSet(name: 'non_downloading_1')..upToDate = false;
505+
final artifact2 = _FakeArtifactSet(name: 'non_downloading_2')..upToDate = false;
506+
507+
final cacheWithArtifacts = Cache.test(
508+
fileSystem: fileSystem,
509+
artifacts: <ArtifactSet>[artifact1, artifact2],
510+
artifactUpdater: artifactUpdater,
511+
processManager: FakeProcessManager.any(),
512+
);
513+
514+
await cacheWithArtifacts.updateAll(<DevelopmentArtifact>{DevelopmentArtifact.universal});
515+
516+
expect(artifact1.didUpdate, true);
517+
expect(artifact2.didUpdate, true);
518+
expect(artifactUpdater.progressContexts, isEmpty);
519+
});
520+
462521
testWithoutContext(
463522
'EngineCachedArtifact.downloadCount returns sum of package and binary dirs',
464523
() {
@@ -1841,9 +1900,11 @@ class FakeSecondaryCachedArtifact extends Fake implements CachedArtifact {
18411900
bool upToDate = false;
18421901
bool didUpdate = false;
18431902
Exception? updateException;
1903+
String artifactName = 'fake';
1904+
int downloads = 1;
18441905

18451906
@override
1846-
String get name => 'fake';
1907+
String get name => artifactName;
18471908

18481909
@override
18491910
Future<bool> isUpToDate(FileSystem fileSystem) async => upToDate;
@@ -1866,10 +1927,10 @@ class FakeSecondaryCachedArtifact extends Fake implements CachedArtifact {
18661927
DevelopmentArtifact get developmentArtifact => DevelopmentArtifact.universal;
18671928

18681929
@override
1869-
String get displayName => 'fake';
1930+
String get displayName => artifactName;
18701931

18711932
@override
1872-
int get downloadCount => 1;
1933+
int get downloadCount => downloads;
18731934
}
18741935

18751936
class FakeIosUsbArtifacts extends Fake implements IosUsbArtifacts {
@@ -2096,3 +2157,56 @@ class FakeArtifactUpdaterDownload extends ArtifactUpdater {
20962157
downloadedFiles.addAll(files);
20972158
}
20982159
}
2160+
2161+
class _FakeArtifactSet extends ArtifactSet {
2162+
_FakeArtifactSet({this.name = 'fake_set'}) : super(DevelopmentArtifact.universal);
2163+
2164+
@override
2165+
final String name;
2166+
2167+
bool didUpdate = false;
2168+
bool upToDate = false;
2169+
2170+
@override
2171+
Future<bool> isUpToDate(FileSystem fileSystem) async => upToDate;
2172+
2173+
@override
2174+
Future<void> update(
2175+
ArtifactUpdater artifactUpdater,
2176+
Logger logger,
2177+
FileSystem fileSystem,
2178+
OperatingSystemUtils operatingSystemUtils, {
2179+
bool offline = false,
2180+
}) async {
2181+
didUpdate = true;
2182+
}
2183+
}
2184+
2185+
typedef ProgressContext = ({
2186+
int artifactIndex,
2187+
int artifactTotal,
2188+
int downloadTotal,
2189+
int downloadIndex,
2190+
});
2191+
2192+
class _FakeProgressRecordingArtifactUpdater extends Fake implements ArtifactUpdater {
2193+
final progressContexts = <ProgressContext>[];
2194+
2195+
@override
2196+
void setProgressContext({
2197+
required int artifactIndex,
2198+
required int artifactTotal,
2199+
required int downloadTotal,
2200+
int downloadIndex = 0,
2201+
}) {
2202+
progressContexts.add((
2203+
artifactIndex: artifactIndex,
2204+
artifactTotal: artifactTotal,
2205+
downloadTotal: downloadTotal,
2206+
downloadIndex: downloadIndex,
2207+
));
2208+
}
2209+
2210+
@override
2211+
void resetProgressContext() {}
2212+
}

0 commit comments

Comments
 (0)