diff --git a/packages/flutter_tools/lib/src/devfs.dart b/packages/flutter_tools/lib/src/devfs.dart index 8795c4d296..366c698f65 100644 --- a/packages/flutter_tools/lib/src/devfs.dart +++ b/packages/flutter_tools/lib/src/devfs.dart @@ -445,6 +445,7 @@ class DevFS { }) async { assert(trackWidgetCreation != null); assert(generator != null); + final DateTime candidateCompileTime = DateTime.now(); // Update modified files final String assetBuildDirPrefix = _asUriPath(getAssetBuildDirectory()); @@ -476,7 +477,6 @@ class DevFS { generator.reset(); } printTrace('Compiling dart to kernel with ${invalidatedFiles.length} updated files'); - lastCompiled = DateTime.now(); final CompilerOutput compilerOutput = await generator.recompile( mainPath, invalidatedFiles, @@ -486,6 +486,8 @@ class DevFS { if (compilerOutput == null || compilerOutput.errorCount > 0) { return UpdateFSReport(success: false); } + // Only update the last compiled time if we successfully compiled. + lastCompiled = candidateCompileTime; // list of sources that needs to be monitored are in [compilerOutput.sources] sources = compilerOutput.sources; // diff --git a/packages/flutter_tools/test/general.shard/devfs_test.dart b/packages/flutter_tools/test/general.shard/devfs_test.dart index a892efa133..0a49b42763 100644 --- a/packages/flutter_tools/test/general.shard/devfs_test.dart +++ b/packages/flutter_tools/test/general.shard/devfs_test.dart @@ -247,12 +247,14 @@ void main() { testUsingContext('reports unsuccessful compile when errors are returned', () async { devFS = DevFS(vmService, 'test', tempDir); await devFS.create(); + final DateTime previousCompile = devFS.lastCompiled; final RealMockResidentCompiler residentCompiler = RealMockResidentCompiler(); when(residentCompiler.recompile( any, any, outputPath: anyNamed('outputPath'), + packagesFilePath: anyNamed('packagesFilePath'), )).thenAnswer((Invocation invocation) { return Future.value(const CompilerOutput('example', 2, [])); }); @@ -266,6 +268,40 @@ void main() { ); expect(report.success, false); + expect(devFS.lastCompiled, previousCompile); + }, overrides: { + FileSystem: () => fs, + }); + + testUsingContext('correctly updates last compiled time when compilation does not fail', () async { + devFS = DevFS(vmService, 'test', tempDir); + // simulate package + final File sourceFile = await _createPackage(fs, 'somepkg', 'main.dart'); + + await devFS.create(); + final DateTime previousCompile = devFS.lastCompiled; + + final RealMockResidentCompiler residentCompiler = RealMockResidentCompiler(); + when(residentCompiler.recompile( + any, + any, + outputPath: anyNamed('outputPath'), + packagesFilePath: anyNamed('packagesFilePath'), + )).thenAnswer((Invocation invocation) { + fs.file('example').createSync(); + return Future.value(CompilerOutput('example', 0, [sourceFile.uri])); + }); + + final UpdateFSReport report = await devFS.update( + mainPath: 'lib/main.dart', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + trackWidgetCreation: false, + invalidatedFiles: [], + ); + + expect(report.success, true); + expect(devFS.lastCompiled, isNot(previousCompile)); }, overrides: { FileSystem: () => fs, }); @@ -375,7 +411,7 @@ void _cleanupTempDirs() { } } -Future _createPackage(FileSystem fs, String pkgName, String pkgFileName, { bool doubleSlash = false }) async { +Future _createPackage(FileSystem fs, String pkgName, String pkgFileName, { bool doubleSlash = false }) async { final Directory pkgTempDir = _newTempDir(fs); String pkgFilePath = fs.path.join(pkgTempDir.path, pkgName, 'lib', pkgFileName); if (doubleSlash) { @@ -391,7 +427,8 @@ Future _createPackage(FileSystem fs, String pkgName, String pkgFileName, { _packages.forEach((String pkgName, Uri pkgUri) { sb.writeln('$pkgName:$pkgUri'); }); - fs.file(fs.path.join(_tempDirs[0].path, '.packages')).writeAsStringSync(sb.toString()); + return fs.file(fs.path.join(_tempDirs[0].path, '.packages')) + ..writeAsStringSync(sb.toString()); } class RealMockVM extends Mock implements VM { diff --git a/packages/flutter_tools/test/integration.shard/test_data/hot_reload_project.dart b/packages/flutter_tools/test/integration.shard/test_data/hot_reload_project.dart index 68de1d40eb..7f155d4fb3 100644 --- a/packages/flutter_tools/test/integration.shard/test_data/hot_reload_project.dart +++ b/packages/flutter_tools/test/integration.shard/test_data/hot_reload_project.dart @@ -24,7 +24,7 @@ class HotReloadProject extends Project { import 'package:flutter/material.dart'; import 'package:flutter/scheduler.dart'; - void main() => runApp(new MyApp()); + void main() => runApp(MyApp()); int count = 1;