Use source list from the compiler to track invalidated files for hot reload. (#29693)

* Use source list from the compiler to track invalidated files.

* Revert accidental change

* Fix first-time-seen-the-file logic

* Fix/simplify invalidate logic now that we can rely on compiler to let us know what is the cut-off point for invalidation.

* Update devfs mock to accommodate for new fields

* Fix deleted files case

* Analyzer found missing final
This commit is contained in:
Alexander Aprelev
2019-03-20 21:58:15 -07:00
committed by GitHub
parent 553a047898
commit 12c4e050be
24 changed files with 57 additions and 372 deletions

View File

@@ -517,7 +517,7 @@ Future<void> _recompile(
});
final CompilerOutput output = await generator.recompile(
null /* mainPath */,
<String>['/path/to/main.dart'],
<Uri>[Uri.parse('/path/to/main.dart')],
outputPath: '/build/',
);
expect(output.outputFilename, equals('/path/to/main.dart.dill'));

View File

@@ -123,7 +123,7 @@ void main() {
generator: residentCompiler,
pathToReload: 'lib/foo.txt.dill',
trackWidgetCreation: false,
invalidatedFiles: <String>[],
invalidatedFiles: <Uri>[],
);
vmService.expectMessages(<String>[
'writeFile test lib/foo.txt.dill',

View File

@@ -116,6 +116,8 @@ void main() {
UpdateFSReport(success: true, syncedBytes: 1000, invalidatedSourcesCount: 1)));
when(mockDevFs.assetPathsToEvict).thenReturn(<String>{});
when(mockDevFs.baseUri).thenReturn(Uri.file('test'));
when(mockDevFs.sources).thenReturn(<Uri>[Uri.file('test')]);
when(mockDevFs.lastCompiled).thenReturn(DateTime.now());
setUp(() {
mockArtifacts = MockLocalEngineArtifacts();

View File

@@ -4,231 +4,30 @@
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/run_hot.dart';
import 'package:mockito/mockito.dart';
import 'src/common.dart';
import 'src/context.dart';
void main() {
final Platform windowsPlatform = MockPlatform();
final Platform notWindowsPlatform = MockPlatform();
final BufferLogger bufferLogger = BufferLogger();
when(windowsPlatform.isWindows).thenReturn(true);
when(notWindowsPlatform.isWindows).thenReturn(false);
group('ProjectFileInvalidator linux/mac', () {
group('ProjectFileInvalidator', () {
final MemoryFileSystem memoryFileSystem = MemoryFileSystem();
final File packagesFile = memoryFileSystem.file('.packages')
..createSync()
..writeAsStringSync(r'''
foo:file:///foo/lib/
bar:file:///.pub-cache/bar/lib/
baz:file:///baz/lib/
test_package:file:///lib/
''');
final File pubspecFile = memoryFileSystem.file('pubspec.yaml')
..createSync()
..writeAsStringSync(r'''
name: test_package
dependencies:
foo: any
bar:
dev_dependencies:
baz: any
''');
final File mainFile = memoryFileSystem.file('lib/main.dart')
..createSync(recursive: true)
..writeAsStringSync(r'''
void main() {}
''');
final File fooFile = memoryFileSystem.file('foo/lib/foo.dart')
..createSync(recursive: true)
..writeAsStringSync('');
memoryFileSystem.file('bar/lib/bar.dart')
..createSync(recursive: true)
..writeAsStringSync('');
final File bazFile = memoryFileSystem.file('baz/lib/baz.dart')
..createSync(recursive: true)
..writeAsStringSync('');
testUsingContext('No .packages, no pubspec', () async {
// Instead of setting up multiple filesystems, passing a .packages file which does not exist.
final ProjectFileInvalidator invalidator = ProjectFileInvalidator('.packages-wrong', null);
invalidator.findInvalidated();
expect(invalidator.packageMap, isEmpty);
expect(invalidator.updateTime, isEmpty);
testUsingContext('Empty project', () async {
expect(
ProjectFileInvalidator.findInvalidated(lastCompiled: DateTime.now(), urisToMonitor: <Uri>[]),
isEmpty);
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
Platform: () => notWindowsPlatform,
});
testUsingContext('.packages only', () async {
final ProjectFileInvalidator invalidator = ProjectFileInvalidator(packagesFile.path, null);
invalidator.findInvalidated();
expect(invalidator.packageMap, <String, Uri>{
'foo': Uri.parse('file:///foo/lib/'),
// Excluded because it is in pub cache.
// 'bar': Uri.parse('file:///.pub-cache/bar/lib/'),
'baz': Uri.parse('file:///baz/lib/'),
'test_package': Uri.parse('file:///lib/'),
});
expect(invalidator.updateTime, <String, int>{
'/baz/lib/baz.dart': bazFile.statSync().modified.millisecondsSinceEpoch,
'/lib/main.dart': mainFile.statSync().modified.millisecondsSinceEpoch,
'/foo/lib/foo.dart': fooFile.statSync().modified.millisecondsSinceEpoch,
});
testUsingContext('Non-existent files are ignored', () async {
expect(
ProjectFileInvalidator.findInvalidated(
lastCompiled: DateTime.now(),
urisToMonitor: <Uri>[Uri.parse('/not-there-anymore')]),
isEmpty);
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
Platform: () => notWindowsPlatform,
});
testUsingContext('.packages and pubspec', () async {
final FlutterProject flutterProject = await FlutterProject.fromDirectory(pubspecFile.parent);
final ProjectFileInvalidator invalidator = ProjectFileInvalidator(packagesFile.path, flutterProject);
invalidator.findInvalidated();
expect(invalidator.packageMap, <String, Uri>{
'foo': Uri.parse('file:///foo/lib/'),
// Excluded because it is in pub cache.
// 'bar': Uri.parse('file:///.pub-cache/bar/lib/'),
// Excluded because it is a dev dependency/
// 'baz': Uri.parse('file:///baz/lib/'),
'test_package': Uri.parse('file:///lib/'),
});
expect(invalidator.updateTime, <String, int>{
'/foo/lib/foo.dart': fooFile.statSync().modified.millisecondsSinceEpoch,
'/lib/main.dart': mainFile.statSync().modified.millisecondsSinceEpoch,
});
expect(invalidator.findInvalidated(), isEmpty);
// Invalidate main.dart.
mainFile.writeAsStringSync('void main() { }');
expect(invalidator.findInvalidated(), <String>['/lib/main.dart']);
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
Platform: () => notWindowsPlatform,
});
testUsingContext('update to .packages triggers warning', () async {
final FlutterProject flutterProject = await FlutterProject.fromDirectory(pubspecFile.parent);
final ProjectFileInvalidator invalidator = ProjectFileInvalidator(packagesFile.path, flutterProject);
invalidator.findInvalidated();
packagesFile.writeAsStringSync(r'''
foo:file:///foo/lib/
bar:file:///.pub-cache/bar/lib/
baz:file:///baz/lib/
new_dep:file:///new_dep/lib/
test_package:file:///lib/
''');
invalidator.findInvalidated();
expect(bufferLogger.errorText, 'Warning: updated dependencies detected. The Flutter application will require a restart to safely use new packages.\n');
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
Platform: () => notWindowsPlatform,
Logger: () => bufferLogger,
});
});
group('ProjectFileInvalidator windows', () {
final MemoryFileSystem memoryFileSystem = MemoryFileSystem(style: FileSystemStyle.windows);
// On windows .packages still contains file Uris, albeit ones with the Drive prefix.
final File packagesFile = memoryFileSystem.file(r'C:\.packages')
..createSync()
..writeAsStringSync(r'''
foo:file:///C:/foo/lib/
bar:file:///C:/Pub/Cache/bar/lib/
baz:file:///C:/baz/lib/
test_package:file:///C:/lib/
''');
memoryFileSystem.file(r'C:\pubspec.yaml')
..createSync()
..writeAsStringSync(r'''
name: test_package
dependencies:
foo: any
bar:
dev_dependencies:
baz: any
''');
final File mainFile = memoryFileSystem.file(r'C:\lib\main.dart')
..createSync(recursive: true)
..writeAsStringSync(r'''
void main() {}
''');
final File fooFile = memoryFileSystem.file(r'C:\foo\lib\foo.dart')
..createSync(recursive: true)
..writeAsStringSync('');
memoryFileSystem.file(r'C:\bar\lib\bar.dart')
..createSync(recursive: true)
..writeAsStringSync('');
final File bazFile = memoryFileSystem.file(r'C:\baz\lib\baz.dart')
..createSync(recursive: true)
..writeAsStringSync('');
testUsingContext('No .packages, no pubspec', () async {
// Instead of setting up multiple filesystems, passing a .packages file which does not exist.
final ProjectFileInvalidator invalidator = ProjectFileInvalidator('.packages-wrong', null);
invalidator.findInvalidated();
expect(invalidator.packageMap, isEmpty);
expect(invalidator.updateTime, isEmpty);
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
Platform: () => windowsPlatform,
});
testUsingContext('.packages only', () async {
final ProjectFileInvalidator invalidator = ProjectFileInvalidator(packagesFile.path, null);
invalidator.findInvalidated();
expect(invalidator.packageMap, <String, Uri>{
'foo': Uri.file(r'C:\foo\lib\', windows: true),
// Excluded because it is in pub cache.
// 'bar': Uri.parse('file:///Pub/Cache/bar/lib/'),
'baz': Uri.file(r'C:\baz\lib\', windows: true),
'test_package': Uri.file(r'C:\lib\', windows: true),
});
expect(invalidator.updateTime, <String, int>{
r'C:\baz\lib\baz.dart': bazFile.statSync().modified.millisecondsSinceEpoch,
r'C:\lib\main.dart': mainFile.statSync().modified.millisecondsSinceEpoch,
r'C:\foo\lib\foo.dart': fooFile.statSync().modified.millisecondsSinceEpoch,
});
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
Platform: () => windowsPlatform,
});
testUsingContext('.packages and pubspec', () async {
final FlutterProject flutterProject = await FlutterProject.fromDirectory(fs.directory(r'C:\'));
final ProjectFileInvalidator invalidator = ProjectFileInvalidator(packagesFile.path, flutterProject);
invalidator.findInvalidated();
expect(invalidator.packageMap, <String, Uri>{
'foo': Uri.file(r'C:\foo\lib\', windows: true),
// Excluded because it is in pub cache.
// 'bar': Uri.parse('file:///C:/Pub/Cache/bar/lib/'),
// Excluded because it is a dev dependency/
// 'baz': Uri.parse('file:///baz/lib/'),
'test_package': Uri.file(r'C:\lib\', windows: true),
});
expect(invalidator.updateTime, <String, int>{
r'C:\lib\main.dart': mainFile.statSync().modified.millisecondsSinceEpoch,
r'C:\foo\lib\foo.dart': fooFile.statSync().modified.millisecondsSinceEpoch,
});
expect(invalidator.findInvalidated(), isEmpty);
// Invalidate main.dart.
mainFile.writeAsStringSync('void main() { }');
expect(invalidator.findInvalidated(), <String>['file:///C:/lib/main.dart']);
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
Platform: () => windowsPlatform,
});
});
}
class MockPlatform extends Mock implements Platform {}

View File

@@ -487,7 +487,7 @@ class MockResidentCompiler extends BasicMock implements ResidentCompiler {
return null;
}
@override
Future<CompilerOutput> recompile(String mainPath, List<String> invalidatedFiles, { String outputPath, String packagesFilePath }) async {
Future<CompilerOutput> recompile(String mainPath, List<Uri> invalidatedFiles, { String outputPath, String packagesFilePath }) async {
fs.file(outputPath).createSync(recursive: true);
fs.file(outputPath).writeAsStringSync('compiled_kernel_output');
return CompilerOutput(outputPath, 0, <Uri>[]);