Skip to content

Commit 0c0d059

Browse files
authored
[flutter_tools] Handle null version gracefully in CachedArtifact and MaterialFonts (#191494)
When artifact version files (such as `bin/internal/material_fonts.version`) are missing or cannot be resolved, `CachedArtifact.version` evaluates to `null`. Previously, `CachedArtifact.update` invoked `updateInner` before checking if `version` was `null`, causing `MaterialFonts.updateInner` (and other artifact subclasses) to evaluate `version!` and throw an unhandled `TypeError` / `_CastError` with `Null check operator used on a null value`. This change: 1. Returns early with a warning in `CachedArtifact.update` before attempting `updateInner` or writing stamps when `version == null`. 2. Guards against `version == null` in `MaterialFonts`, `GradleWrapper`, and `FlutterRunnerDebugSymbols`. 3. Adds an automated regression test in `cache_test.dart`. Fixes #90092
1 parent 2483da2 commit 0c0d059

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -957,17 +957,18 @@ abstract class CachedArtifact extends ArtifactSet {
957957
);
958958
}
959959
}
960+
final String? version = this.version;
961+
if (version == null) {
962+
logger.printWarning(
963+
'No known version for the artifact name "$name". '
964+
'Flutter can continue, but the artifact may be re-downloaded on '
965+
'subsequent invocations until the problem is resolved.',
966+
);
967+
return;
968+
}
960969
await updateInner(artifactUpdater, fileSystem, operatingSystemUtils);
961970
try {
962-
if (version == null) {
963-
logger.printWarning(
964-
'No known version for the artifact name "$name". '
965-
'Flutter can continue, but the artifact may be re-downloaded on '
966-
'subsequent invocations until the problem is resolved.',
967-
);
968-
} else {
969-
cache.setStampFor(stampName, version!);
970-
}
971+
cache.setStampFor(stampName, version);
971972
} on FileSystemException catch (err) {
972973
logger.printWarning(
973974
'The new artifact "$name" was downloaded, but Flutter failed to update '

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,35 @@ void main() {
161161
expect(logger.warningText, contains('No known version for the artifact name "fake"'));
162162
});
163163

164+
testWithoutContext('MaterialFonts continues on missing version file', () async {
165+
final FileSystem fileSystem = MemoryFileSystem.test();
166+
final logger = BufferLogger.test();
167+
final Directory artifactDir = fileSystem.systemTempDirectory.createTempSync(
168+
'flutter_cache_test_artifact.',
169+
);
170+
final Directory downloadDir = fileSystem.systemTempDirectory.createTempSync(
171+
'flutter_cache_test_download.',
172+
);
173+
final Cache cache = FakeSecondaryCache()
174+
..version =
175+
null // version is missing.
176+
..artifactDirectory = artifactDir
177+
..downloadDir = downloadDir;
178+
179+
final materialFonts = MaterialFonts(cache);
180+
await materialFonts.update(
181+
FakeArtifactUpdater(),
182+
logger,
183+
fileSystem,
184+
FakeOperatingSystemUtils(),
185+
);
186+
187+
expect(
188+
logger.warningText,
189+
contains('No known version for the artifact name "material_fonts"'),
190+
);
191+
});
192+
164193
testWithoutContext(
165194
'Gradle wrapper should not be up to date, if some cached artifact is not available',
166195
() {

0 commit comments

Comments
 (0)