From 497ae83c82a603acf8f014c1712dcf25e96e9880 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 22 Nov 2019 10:08:38 -0800 Subject: [PATCH] Fix experimental incremental web compiler for Windows (#45286) --- .../lib/src/resident_runner.dart | 4 +- .../flutter_tools/lib/src/web/devfs_web.dart | 48 ++++++++++++++----- .../general.shard/resident_runner_test.dart | 7 ++- .../general.shard/web/devfs_web_test.dart | 25 ++++++++++ 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index ea34ae10d6..f8d6fafb34 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -88,7 +88,9 @@ class FlutterDevice { fileSystemScheme: fileSystemScheme, targetModel: TargetModel.dartdevc, experimentalFlags: experimentalFlags, - platformDill: artifacts.getArtifactPath(Artifact.webPlatformKernelDill, mode: buildMode), + platformDill: fs.file(artifacts + .getArtifactPath(Artifact.webPlatformKernelDill, mode: buildMode)) + .absolute.uri.toString(), dartDefines: dartDefines, ); } else if (flutterProject.hasBuilders) { diff --git a/packages/flutter_tools/lib/src/web/devfs_web.dart b/packages/flutter_tools/lib/src/web/devfs_web.dart index efbc4189da..8f14d12712 100644 --- a/packages/flutter_tools/lib/src/web/devfs_web.dart +++ b/packages/flutter_tools/lib/src/web/devfs_web.dart @@ -12,6 +12,7 @@ import '../asset.dart'; import '../base/common.dart'; import '../base/file_system.dart'; import '../base/io.dart'; +import '../base/platform.dart'; import '../base/utils.dart'; import '../build_info.dart'; import '../bundle.dart'; @@ -61,6 +62,8 @@ class WebAssetServer { final Map _files = {}; final Map _sourcemaps = {}; + final RegExp _drivePath = RegExp(r'\/[A-Z]:\/'); + // handle requests for JavaScript source, dart sources maps, or asset files. Future _handleRequest(HttpRequest request) async { final HttpResponse response = request.response; @@ -79,11 +82,17 @@ class WebAssetServer { await response.close(); return; } + // TODO(jonahwilliams): better path normalization in frontend_server to remove + // this workaround. + String requestPath = request.uri.path; + if (requestPath.startsWith(_drivePath)) { + requestPath = requestPath.substring(3); + } // If this is a JavaScript file, it must be in the in-memory cache. // Attempt to look up the file by URI. - if (_files.containsKey(request.uri.path)) { - final List bytes = _files[request.uri.path]; + if (_files.containsKey(requestPath)) { + final List bytes = _files[requestPath]; response.headers ..add('Content-Length', bytes.length) ..add('Content-Type', 'application/javascript'); @@ -93,8 +102,8 @@ class WebAssetServer { } // If this is a sourcemap file, then it might be in the in-memory cache. // Attempt to lookup the file by URI. - if (_sourcemaps.containsKey(request.uri.path)) { - final List bytes = _sourcemaps[request.uri.path]; + if (_sourcemaps.containsKey(requestPath)) { + final List bytes = _sourcemaps[requestPath]; response.headers ..add('Content-Length', bytes.length) ..add('Content-Type', 'application/json'); @@ -193,7 +202,7 @@ class WebAssetServer { codeStart, codeEnd - codeStart, ); - _files[filePath] = byteView; + _files[_filePathToUriFragment(filePath)] = byteView; final int sourcemapStart = sourcemapOffsets[0]; final int sourcemapEnd = sourcemapOffsets[1]; @@ -206,7 +215,7 @@ class WebAssetServer { sourcemapStart, sourcemapEnd - sourcemapStart , ); - _sourcemaps['$filePath.map'] = sourcemapView; + _sourcemaps['${_filePathToUriFragment(filePath)}.map'] = sourcemapView; modules.add(filePath); } @@ -303,15 +312,18 @@ class WebDevFS implements DevFS { 'dart_stack_trace_mapper.js', )); _webAssetServer.writeFile('/main.dart.js', generateBootstrapScript( - requireUrl: requireJS.path, - mapperUrl: stackTraceMapper.path, - entrypoint: '$mainPath.js', + requireUrl: _filePathToUriFragment(requireJS.path), + mapperUrl: _filePathToUriFragment(stackTraceMapper.path), + entrypoint: '${_filePathToUriFragment(mainPath)}.js', )); _webAssetServer.writeFile('/main_module.js', generateMainModule( - entrypoint: '$mainPath.js', + entrypoint: '${_filePathToUriFragment(mainPath)}.js', )); _webAssetServer.writeFile('/dart_sdk.js', dartSdk.readAsStringSync()); _webAssetServer.writeFile('/dart_sdk.js.map', dartSdkSourcemap.readAsStringSync()); + // TODO(jonahwilliams): refactor the asset code in this and the regular devfs to + // be shared. + await writeBundle(fs.directory(getAssetBuildDirectory()), bundle.entries); } final DateTime candidateCompileTime = DateTime.now(); if (fullRestart) { @@ -344,6 +356,20 @@ class WebDevFS implements DevFS { } return UpdateFSReport(success: true, syncedBytes: codeFile.lengthSync(), invalidatedSourcesCount: invalidatedFiles.length) - ..invalidatedModules = modules; + ..invalidatedModules = modules.map(_filePathToUriFragment).toList(); } } + +String _filePathToUriFragment(String path) { + if (platform.isWindows) { + final bool startWithSlash = path.startsWith('/'); + final String partial = fs.path + .split(path) + .skip(startWithSlash ? 2 : 1).join('/'); + if (partial.startsWith('/')) { + return partial; + } + return '/$partial'; + } + return path; +} diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 00d0e28385..b5204ddd78 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -620,8 +620,11 @@ void main() { expect(residentCompiler.targetModel, TargetModel.dartdevc); expect(residentCompiler.sdkRoot, artifacts.getArtifactPath(Artifact.flutterWebSdk, mode: BuildMode.debug) + '/'); - expect(residentCompiler.platformDill, - artifacts.getArtifactPath(Artifact.webPlatformKernelDill, mode: BuildMode.debug)); + expect( + residentCompiler.platformDill, + fs.file(artifacts.getArtifactPath(Artifact.webPlatformKernelDill, mode: BuildMode.debug)) + .absolute.uri.toString(), + ); }, overrides: { FeatureFlags: () => TestFeatureFlags(isWebIncrementalCompilerEnabled: true), })); diff --git a/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart b/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart index df425f9197..296acc507e 100644 --- a/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart +++ b/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart @@ -127,6 +127,31 @@ void main() { verify(headers.add('Content-Length', source.lengthSync())).called(1); verify(headers.add('Content-Type', 'application/javascript')).called(1); verify(response.add(source.readAsBytesSync())).called(1); + }, overrides: { + Platform: () => linux, + })); + + test('serves JavaScript files from in memory cache on Windows', () => testbed.run(() async { + final File source = fs.file('source') + ..writeAsStringSync('main() {}'); + final File sourcemap = fs.file('sourcemap') + ..writeAsStringSync('{}'); + final File manifest = fs.file('manifest') + ..writeAsStringSync(json.encode({'/C:/foo.js': { + 'code': [0, source.lengthSync()], + 'sourcemap': [0, 2], + }})); + webAssetServer.write(source, manifest, sourcemap); + + when(request.uri).thenReturn(Uri.parse('http://foobar/C:/foo.js')); + requestController.add(request); + await closeCompleter.future; + + verify(headers.add('Content-Length', source.lengthSync())).called(1); + verify(headers.add('Content-Type', 'application/javascript')).called(1); + verify(response.add(source.readAsBytesSync())).called(1); + }, overrides: { + Platform: () => windows, })); test('serves JavaScript files from in memory cache not from manifest', () => testbed.run(() async {