Skip to content

Commit c6b9816

Browse files
bkonyiloic-sharma
andauthored
[tool] Fix Windows hot reload mtime truncation issue (flutter#191271)
On Windows, `dart:io` reports `FileStat.modified` truncated to second precision (via `GetFileAttributesExW`). When `ProjectFileInvalidator.findInvalidated` compares file modification times with `lastCompiled` (which is recorded with millisecond precision), files edited in the same second as the previous compilation are ignored because `updatedAt.isAfter(lastCompiled)` returns `false`. This PR fixes the issue by truncating `lastCompiled` to second precision on Windows and using `!updatedAt.isBefore(lastCompiledTruncated)` to ensure files modified within the same second as the compilation start time are marked as invalidated. Fixes flutter#191198 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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 [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/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Loïc Sharma <[email protected]>
1 parent 74b8f23 commit c6b9816

2 files changed

Lines changed: 97 additions & 3 deletions

File tree

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,20 @@ class ProjectFileInvalidator {
14861486
if (_isNotInPubCache(uri)) uri,
14871487
];
14881488
final invalidatedFiles = <Uri>[];
1489+
1490+
final bool Function(DateTime) isInvalidated;
1491+
if (_platform.isWindows) {
1492+
// On Windows, FileStat.modified truncates to second precision (via GetFileAttributesExW).
1493+
// However, lastCompiled is recorded with millisecond precision.
1494+
final lastCompiledTruncated = DateTime.fromMillisecondsSinceEpoch(
1495+
(lastCompiled.millisecondsSinceEpoch ~/ 1000) * 1000,
1496+
isUtc: lastCompiled.isUtc,
1497+
);
1498+
isInvalidated = (DateTime updatedAt) => !updatedAt.isBefore(lastCompiledTruncated);
1499+
} else {
1500+
isInvalidated = (DateTime updatedAt) => updatedAt.isAfter(lastCompiled);
1501+
}
1502+
14891503
if (asyncScanning) {
14901504
final pool = Pool(_kMaxPendingStats);
14911505
final waitList = <Future<void>>[];
@@ -1502,7 +1516,7 @@ class ProjectFileInvalidator {
15021516
: _fileSystem.stat(uri.toFilePath(windows: _platform.isWindows)))
15031517
.then((FileStat stat) {
15041518
final DateTime updatedAt = stat.modified;
1505-
if (updatedAt.isAfter(lastCompiled)) {
1519+
if (isInvalidated(updatedAt)) {
15061520
invalidatedFiles.add(uri);
15071521
}
15081522
}),
@@ -1517,7 +1531,7 @@ class ProjectFileInvalidator {
15171531
final DateTime updatedAt = uri.hasScheme && uri.scheme != 'file'
15181532
? _fileSystem.file(uri).statSync().modified
15191533
: _fileSystem.statSync(uri.toFilePath(windows: _platform.isWindows)).modified;
1520-
if (updatedAt.isAfter(lastCompiled)) {
1534+
if (isInvalidated(updatedAt)) {
15211535
invalidatedFiles.add(uri);
15221536
}
15231537
}
@@ -1527,7 +1541,7 @@ class ProjectFileInvalidator {
15271541
final File packageFile = _fileSystem.file(packagesPath);
15281542
final Uri packageUri = packageFile.uri;
15291543
final DateTime updatedAt = packageFile.statSync().modified;
1530-
if (updatedAt.isAfter(lastCompiled)) {
1544+
if (isInvalidated(updatedAt)) {
15311545
invalidatedFiles.add(packageUri);
15321546
}
15331547

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,85 @@ void main() {
132132
);
133133
},
134134
);
135+
136+
testWithoutContext(
137+
'File edited in same second as last compile is invalidated on Windows, asyncScanning: $asyncScanning',
138+
() async {
139+
final FileSystem fileSystem = MemoryFileSystem.test();
140+
final projectFileInvalidator = ProjectFileInvalidator(
141+
fileSystem: fileSystem,
142+
platform: FakePlatform(operatingSystem: 'windows'),
143+
logger: BufferLogger.test(),
144+
);
145+
146+
final lastCompiled = DateTime(2026, 8, 18, 3, 0, 0, 500); // 3:00:00.500
147+
final fileMtime = DateTime(2026, 8, 18, 3); // 3:00:00.000 (truncated)
148+
149+
final File file = fileSystem.file('foo.dart')
150+
..createSync(recursive: true)
151+
..writeAsStringSync('content');
152+
file.setLastModifiedSync(fileMtime);
153+
154+
fileSystem.file('.dart_tool/package_config.json')
155+
..createSync(recursive: true)
156+
..writeAsStringSync('''
157+
{
158+
"packages": [],
159+
"configVersion": 2
160+
}
161+
''');
162+
163+
expect(
164+
(await projectFileInvalidator.findInvalidated(
165+
lastCompiled: lastCompiled,
166+
urisToMonitor: <Uri>[file.uri],
167+
packagesPath: '.dart_tool/package_config.json',
168+
asyncScanning: asyncScanning,
169+
packageConfig: PackageConfig.empty,
170+
)).uris,
171+
contains(file.uri),
172+
);
173+
},
174+
);
175+
176+
testWithoutContext(
177+
'File edited in same second as last compile is NOT invalidated on non-Windows, asyncScanning: $asyncScanning',
178+
() async {
179+
final FileSystem fileSystem = MemoryFileSystem.test();
180+
final projectFileInvalidator = ProjectFileInvalidator(
181+
fileSystem: fileSystem,
182+
platform: FakePlatform(),
183+
logger: BufferLogger.test(),
184+
);
185+
186+
final lastCompiled = DateTime(2026, 8, 18, 3, 0, 0, 500); // 3:00:00.500
187+
final fileMtime = DateTime(2026, 8, 18, 3); // 3:00:00.000
188+
189+
final File file = fileSystem.file('foo.dart')
190+
..createSync(recursive: true)
191+
..writeAsStringSync('content');
192+
file.setLastModifiedSync(fileMtime);
193+
194+
fileSystem.file('.dart_tool/package_config.json')
195+
..createSync(recursive: true)
196+
..writeAsStringSync('''
197+
{
198+
"packages": [],
199+
"configVersion": 2
200+
}
201+
''');
202+
203+
expect(
204+
(await projectFileInvalidator.findInvalidated(
205+
lastCompiled: lastCompiled,
206+
urisToMonitor: <Uri>[file.uri],
207+
packagesPath: '.dart_tool/package_config.json',
208+
asyncScanning: asyncScanning,
209+
packageConfig: PackageConfig.empty,
210+
)).uris,
211+
isEmpty,
212+
);
213+
},
214+
);
135215
}
136216
}

0 commit comments

Comments
 (0)