fix(mobile): replace an unreadable diagnostics install ID file

A file that cannot be read back, such as one that is not text, made the install ID lookup give up, so every report went out without one. Replace it with a new ID instead, as already happens for an empty file.

Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
Signed-off-by: Tom Brow <tomb@block.xyz>
This commit is contained in:
Tom Brow
2026-11-20 10:27:07 -08:00
parent 9c0bcd6239
commit 1096191b21
2 changed files with 35 additions and 2 deletions

View File

@@ -60,8 +60,13 @@ Future<String?> diagnosticInstallId({
final dir = await (directory ?? _installIdDirectory)();
final file = File('${dir.path}/$_installIdFile');
if (await file.exists()) {
final saved = (await file.readAsString()).trim();
if (saved.isNotEmpty) return saved;
try {
final saved = (await file.readAsString()).trim();
if (saved.isNotEmpty) return saved;
} on FileSystemException {
// Unreadable, for instance not text: a damaged file is replaced below,
// not a reason to send reports without an install ID.
}
}
final created = const Uuid().v4();
await dir.create(recursive: true);

View File

@@ -0,0 +1,28 @@
import 'dart:io';
import 'package:buzz/shared/diagnostics/diagnostic_startup.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
// A damaged file is replaced, so reports still carry one install ID from
// then on.
for (final (name, contents) in <(String, List<int>)>[
('empty', []),
('blank', [0x20, 0x0a]),
('not text', [0xff, 0xfe, 0xc3, 0x28]),
]) {
test('a $name install ID file is replaced with one that is kept', () async {
final directory = await Directory.systemTemp.createTemp('install');
addTearDown(() => directory.delete(recursive: true));
await File(
'${directory.path}/diagnostics_install_id',
).writeAsBytes(contents);
final id = await diagnosticInstallId(directory: () async => directory);
expect(id, isNotNull);
expect(id, isNotEmpty);
expect(await diagnosticInstallId(directory: () async => directory), id);
});
}
}