From a9700ffdd54b60f39e8cdfc282f9f2f9959cfe03 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Wed, 8 Mar 2023 14:45:51 -0500 Subject: [PATCH] Revert "[web:tools] always use CanvasKit from the cache when building web apps (#93002)" (#117693) Revert "[web:tools] always use CanvasKit from the cache when building web apps (#93002)" --- .../lib/src/build_system/targets/web.dart | 29 +++++++---------- .../flutter_tools/lib/src/flutter_cache.dart | 14 --------- .../lib/src/test/flutter_web_platform.dart | 31 +++++++------------ .../flutter_tools/lib/src/test/runner.dart | 1 - .../flutter_tools/lib/src/web/compile.dart | 2 +- .../build_system/targets/web_test.dart | 22 ++++++------- 6 files changed, 35 insertions(+), 64 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_system/targets/web.dart b/packages/flutter_tools/lib/src/build_system/targets/web.dart index 5f2f0c510c7..e08d822e72f 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/web.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/web.dart @@ -347,7 +347,7 @@ class Dart2WasmTarget extends Dart2WebTarget { /// Unpacks the dart2js or dart2wasm compilation and resources to a given /// output directory. class WebReleaseBundle extends Target { - const WebReleaseBundle(this.webRenderer, this.isWasm); + const WebReleaseBundle(this.webRenderer, {required this.isWasm}); final WebRendererMode webRenderer; final bool isWasm; @@ -488,10 +488,9 @@ class WebReleaseBundle extends Target { /// These assets can be cached forever and are only invalidated when the /// Flutter SDK is upgraded to a new version. class WebBuiltInAssets extends Target { - const WebBuiltInAssets(this.fileSystem, this.cache, this.isWasm); + const WebBuiltInAssets(this.fileSystem, {required this.isWasm}); final FileSystem fileSystem; - final Cache cache; final bool isWasm; @override @@ -511,16 +510,13 @@ class WebBuiltInAssets extends Target { @override Future build(Environment environment) async { - // TODO(yjbanov): https://github.com/flutter/flutter/issues/52588 - // - // Update this when we start building CanvasKit from sources. In the - // meantime, get the Web SDK directory from cache rather than through - // Artifacts. The latter is sensitive to `--local-engine`, which changes - // the directory to point to ENGINE/src/out. However, CanvasKit is not yet - // built as part of the engine, but fetched from CIPD, and so it won't be - // found in ENGINE/src/out. - final Directory flutterWebSdk = cache.getWebSdkDirectory(); - final Directory canvasKitDirectory = flutterWebSdk.childDirectory('canvaskit'); + final Directory canvasKitDirectory = globals.fs.directory( + globals.artifacts!.getArtifactPath( + Artifact.canvasKitPath, + platform: TargetPlatform.web_javascript, + ), + ); + for (final File file in canvasKitDirectory.listSync(recursive: true).whereType()) { final String relativePath = fileSystem.path.relative(file.path, from: canvasKitDirectory.path); final String targetPath = fileSystem.path.join(environment.outputDir.path, 'canvaskit', relativePath); @@ -543,10 +539,9 @@ class WebBuiltInAssets extends Target { /// Generate a service worker for a web target. class WebServiceWorker extends Target { - const WebServiceWorker(this.fileSystem, this.cache, this.webRenderer, this.isWasm); + const WebServiceWorker(this.fileSystem, this.webRenderer, {required this.isWasm}); final FileSystem fileSystem; - final Cache cache; final WebRendererMode webRenderer; final bool isWasm; @@ -556,8 +551,8 @@ class WebServiceWorker extends Target { @override List get dependencies => [ if (isWasm) Dart2WasmTarget(webRenderer) else Dart2JSTarget(webRenderer), - WebReleaseBundle(webRenderer, isWasm), - WebBuiltInAssets(fileSystem, cache, isWasm), + WebReleaseBundle(webRenderer, isWasm: isWasm), + WebBuiltInAssets(fileSystem, isWasm: isWasm), ]; @override diff --git a/packages/flutter_tools/lib/src/flutter_cache.dart b/packages/flutter_tools/lib/src/flutter_cache.dart index a18b55f1dfb..8bcff1f08e0 100644 --- a/packages/flutter_tools/lib/src/flutter_cache.dart +++ b/packages/flutter_tools/lib/src/flutter_cache.dart @@ -181,20 +181,6 @@ class FlutterWebSdk extends CachedArtifact { final Uri url = Uri.parse('${cache.storageBaseUrl}/flutter_infra_release/flutter/$version/flutter-web-sdk.zip'); ErrorHandlingFileSystem.deleteIfExists(location, recursive: true); await artifactUpdater.downloadZipArchive('Downloading Web SDK...', url, location); - // This is a temporary work-around for not being able to safely download into a shared directory. - final FileSystem fileSystem = location.fileSystem; - for (final FileSystemEntity entity in location.listSync(recursive: true)) { - if (entity is File) { - final List segments = fileSystem.path.split(entity.path); - segments.remove('flutter_web_sdk'); - final String newPath = fileSystem.path.joinAll(segments); - final File newFile = fileSystem.file(newPath); - if (!newFile.existsSync()) { - newFile.createSync(recursive: true); - } - entity.copySync(newPath); - } - } } } diff --git a/packages/flutter_tools/lib/src/test/flutter_web_platform.dart b/packages/flutter_tools/lib/src/test/flutter_web_platform.dart index 9d23910b98b..a780ca5b232 100644 --- a/packages/flutter_tools/lib/src/test/flutter_web_platform.dart +++ b/packages/flutter_tools/lib/src/test/flutter_web_platform.dart @@ -51,14 +51,12 @@ class FlutterWebPlatform extends PlatformPlugin { required Logger logger, required Artifacts? artifacts, required ProcessManager processManager, - required Cache cache, TestTimeRecorder? testTimeRecorder, }) : _fileSystem = fileSystem, _flutterToolPackageConfig = flutterToolPackageConfig, _chromiumLauncher = chromiumLauncher, _logger = logger, - _artifacts = artifacts, - _cache = cache { + _artifacts = artifacts { final shelf.Cascade cascade = shelf.Cascade() .add(_webSocketHandler.handler) .add(createStaticHandler( @@ -98,7 +96,6 @@ class FlutterWebPlatform extends PlatformPlugin { final OneOffHandler _webSocketHandler = OneOffHandler(); final AsyncMemoizer _closeMemo = AsyncMemoizer(); final String _root; - final Cache _cache; /// Allows only one test suite (typically one test file) to be loaded and run /// at any given point in time. Loading more than one file at a time is known @@ -121,7 +118,6 @@ class FlutterWebPlatform extends PlatformPlugin { required ChromiumLauncher chromiumLauncher, required Artifacts? artifacts, required ProcessManager processManager, - required Cache cache, TestTimeRecorder? testTimeRecorder, }) async { final shelf_io.IOServer server = shelf_io.IOServer(await HttpMultiServer.loopback(0)); @@ -151,7 +147,6 @@ class FlutterWebPlatform extends PlatformPlugin { logger: logger, nullAssertions: nullAssertions, processManager: processManager, - cache: cache, testTimeRecorder: testTimeRecorder, ); } @@ -226,17 +221,12 @@ class FlutterWebPlatform extends PlatformPlugin { )); File _canvasKitFile(String relativePath) { - // TODO(yjbanov): https://github.com/flutter/flutter/issues/52588 - // - // Update this when we start building CanvasKit from sources. In the - // meantime, get the Web SDK directory from cache rather than through - // Artifacts. The latter is sensitive to `--local-engine`, which changes - // the directory to point to ENGINE/src/out. However, CanvasKit is not yet - // built as part of the engine, but fetched from CIPD, and so it won't be - // found in ENGINE/src/out. - final Directory webSdkDirectory = _cache.getWebSdkDirectory(); + final String canvasKitPath = _artifacts!.getArtifactPath( + Artifact.canvasKitPath, + platform: TargetPlatform.web_javascript, + ); final File canvasKitFile = _fileSystem.file(_fileSystem.path.join( - webSdkDirectory.path, + canvasKitPath, relativePath, )); return canvasKitFile; @@ -388,12 +378,13 @@ class FlutterWebPlatform extends PlatformPlugin { /// Serves a local build of CanvasKit, replacing the CDN build, which can /// cause test flakiness due to reliance on network. shelf.Response _localCanvasKitHandler(shelf.Request request) { - final String path = _fileSystem.path.fromUri(request.url); - if (!path.startsWith('canvaskit/')) { + final String fullPath = _fileSystem.path.fromUri(request.url); + if (!fullPath.startsWith('canvaskit/')) { return shelf.Response.notFound('Not a CanvasKit file request'); } - final String extension = _fileSystem.path.extension(path); + final String relativePath = fullPath.replaceFirst('canvaskit/', ''); + final String extension = _fileSystem.path.extension(relativePath); String contentType; switch (extension) { case '.js': @@ -409,7 +400,7 @@ class FlutterWebPlatform extends PlatformPlugin { } return shelf.Response.ok( - _canvasKitFile(path).openRead(), + _canvasKitFile(relativePath).openRead(), headers: { HttpHeaders.contentTypeHeader: contentType, }, diff --git a/packages/flutter_tools/lib/src/test/runner.dart b/packages/flutter_tools/lib/src/test/runner.dart index cb0390c368e..08972f4cb9a 100644 --- a/packages/flutter_tools/lib/src/test/runner.dart +++ b/packages/flutter_tools/lib/src/test/runner.dart @@ -176,7 +176,6 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner { browserFinder: findChromeExecutable, logger: globals.logger, ), - cache: globals.cache, testTimeRecorder: testTimeRecorder, ); }, diff --git a/packages/flutter_tools/lib/src/web/compile.dart b/packages/flutter_tools/lib/src/web/compile.dart index be30080e7a1..3e094464cff 100644 --- a/packages/flutter_tools/lib/src/web/compile.dart +++ b/packages/flutter_tools/lib/src/web/compile.dart @@ -54,7 +54,7 @@ Future buildWeb( final Stopwatch sw = Stopwatch()..start(); try { final BuildResult result = await globals.buildSystem.build( - WebServiceWorker(globals.fs, globals.cache, buildInfo.webRenderer, isWasm), + WebServiceWorker(globals.fs, buildInfo.webRenderer, isWasm: isWasm), Environment( projectDir: globals.fs.currentDirectory, outputDir: outputDirectory, diff --git a/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart b/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart index 151aa45beeb..91ebd293cea 100644 --- a/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart @@ -101,7 +101,7 @@ void main() { webResources.childFile('index.html') .createSync(recursive: true); environment.buildDir.childFile('main.dart.js').createSync(); - await const WebReleaseBundle(WebRendererMode.autoDetect, false).build(environment); + await const WebReleaseBundle(WebRendererMode.autoDetect, isWasm: false).build(environment); expect(environment.outputDir.childFile('version.json'), exists); })); @@ -113,7 +113,7 @@ void main() { final Directory webResources = environment.projectDir.childDirectory('web'); webResources.childFile('index.html').createSync(recursive: true); environment.buildDir.childFile('main.dart.js').createSync(); - await const WebReleaseBundle(WebRendererMode.autoDetect, false).build(environment); + await const WebReleaseBundle(WebRendererMode.autoDetect, isWasm: false).build(environment); final String versionFile = environment.outputDir .childFile('version.json') @@ -131,7 +131,7 @@ void main() { '''); environment.buildDir.childFile('main.dart.js').createSync(); - await const WebReleaseBundle(WebRendererMode.autoDetect, false).build(environment); + await const WebReleaseBundle(WebRendererMode.autoDetect, isWasm: false).build(environment); expect(environment.outputDir.childFile('index.html').readAsStringSync(), contains('/basehreftest/')); })); @@ -144,7 +144,7 @@ void main() { '''); environment.buildDir.childFile('main.dart.js').createSync(); - await const WebReleaseBundle(WebRendererMode.autoDetect, false).build(environment); + await const WebReleaseBundle(WebRendererMode.autoDetect, isWasm: false).build(environment); expect(environment.outputDir.childFile('index.html').readAsStringSync(), contains('/basehreftest/')); })); @@ -166,7 +166,7 @@ void main() { .writeAsStringSync('A'); environment.buildDir.childFile('main.dart.js').createSync(); - await const WebReleaseBundle(WebRendererMode.autoDetect, false).build(environment); + await const WebReleaseBundle(WebRendererMode.autoDetect, isWasm: false).build(environment); expect(environment.outputDir.childFile('foo.txt') .readAsStringSync(), 'A'); @@ -178,7 +178,7 @@ void main() { // Update to arbitrary resource file triggers rebuild. webResources.childFile('foo.txt').writeAsStringSync('B'); - await const WebReleaseBundle(WebRendererMode.autoDetect, false).build(environment); + await const WebReleaseBundle(WebRendererMode.autoDetect, isWasm: false).build(environment); expect(environment.outputDir.childFile('foo.txt') .readAsStringSync(), 'B'); @@ -827,7 +827,7 @@ void main() { environment.outputDir.childDirectory('a').childFile('a.txt') ..createSync(recursive: true) ..writeAsStringSync('A'); - await WebServiceWorker(globals.fs, globals.cache, WebRendererMode.autoDetect, false).build(environment); + await WebServiceWorker(globals.fs, WebRendererMode.autoDetect, isWasm: false).build(environment); expect(environment.outputDir.childFile('flutter_service_worker.js'), exists); // Contains file hash. @@ -846,7 +846,7 @@ void main() { environment.outputDir .childFile('index.html') .createSync(recursive: true); - await WebServiceWorker(globals.fs, globals.cache, WebRendererMode.autoDetect, false).build(environment); + await WebServiceWorker(globals.fs, WebRendererMode.autoDetect, isWasm: false).build(environment); expect(environment.outputDir.childFile('flutter_service_worker.js'), exists); // Contains file hash for both `/` and index.html. @@ -864,7 +864,7 @@ void main() { environment.outputDir .childFile('main.dart.js.map') .createSync(recursive: true); - await WebServiceWorker(globals.fs, globals.cache, WebRendererMode.autoDetect, false).build(environment); + await WebServiceWorker(globals.fs, WebRendererMode.autoDetect, isWasm: false).build(environment); // No caching of source maps. expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(), @@ -896,7 +896,7 @@ void main() { ..createSync(recursive: true) ..writeAsStringSync('OL'); - await WebBuiltInAssets(globals.fs, globals.cache, false).build(environment); + await WebBuiltInAssets(globals.fs, isWasm: false).build(environment); // No caching of source maps. final String fileGeneratorsPath = environment.artifacts @@ -913,7 +913,7 @@ void main() { globals.fs.file('bin/cache/flutter_web_sdk/canvaskit/canvaskit.wasm') .createSync(recursive: true); - await WebBuiltInAssets(globals.fs, globals.cache, true).build(environment); + await WebBuiltInAssets(globals.fs, isWasm: true).build(environment); expect(environment.outputDir.childFile('main.dart.js').existsSync(), true); expect(environment.outputDir.childDirectory('canvaskit')