Skip to content

Commit

Permalink
Revert "[web:tools] always use CanvasKit from the cache when building…
Browse files Browse the repository at this point in the history
… web apps (#93002)" (#117693)

Revert "[web:tools] always use CanvasKit from the cache when building web apps (#93002)"
  • Loading branch information
mdebbar authored Mar 8, 2023
1 parent 2a67bf7 commit a9700ff
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 64 deletions.
29 changes: 12 additions & 17 deletions packages/flutter_tools/lib/src/build_system/targets/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -511,16 +510,13 @@ class WebBuiltInAssets extends Target {

@override
Future<void> 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<File>()) {
final String relativePath = fileSystem.path.relative(file.path, from: canvasKitDirectory.path);
final String targetPath = fileSystem.path.join(environment.outputDir.path, 'canvaskit', relativePath);
Expand All @@ -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;

Expand All @@ -556,8 +551,8 @@ class WebServiceWorker extends Target {
@override
List<Target> get dependencies => <Target>[
if (isWasm) Dart2WasmTarget(webRenderer) else Dart2JSTarget(webRenderer),
WebReleaseBundle(webRenderer, isWasm),
WebBuiltInAssets(fileSystem, cache, isWasm),
WebReleaseBundle(webRenderer, isWasm: isWasm),
WebBuiltInAssets(fileSystem, isWasm: isWasm),
];

@override
Expand Down
14 changes: 0 additions & 14 deletions packages/flutter_tools/lib/src/flutter_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
}
}
}
}

Expand Down
31 changes: 11 additions & 20 deletions packages/flutter_tools/lib/src/test/flutter_web_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -98,7 +96,6 @@ class FlutterWebPlatform extends PlatformPlugin {
final OneOffHandler _webSocketHandler = OneOffHandler();
final AsyncMemoizer<void> _closeMemo = AsyncMemoizer<void>();
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
Expand All @@ -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));
Expand Down Expand Up @@ -151,7 +147,6 @@ class FlutterWebPlatform extends PlatformPlugin {
logger: logger,
nullAssertions: nullAssertions,
processManager: processManager,
cache: cache,
testTimeRecorder: testTimeRecorder,
);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand All @@ -409,7 +400,7 @@ class FlutterWebPlatform extends PlatformPlugin {
}

return shelf.Response.ok(
_canvasKitFile(path).openRead(),
_canvasKitFile(relativePath).openRead(),
headers: <String, Object>{
HttpHeaders.contentTypeHeader: contentType,
},
Expand Down
1 change: 0 additions & 1 deletion packages/flutter_tools/lib/src/test/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
browserFinder: findChromeExecutable,
logger: globals.logger,
),
cache: globals.cache,
testTimeRecorder: testTimeRecorder,
);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/web/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Future<void> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
Expand All @@ -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')
Expand All @@ -131,7 +131,7 @@ void main() {
<!DOCTYPE html><html><base href="$kBaseHrefPlaceholder"><head></head></html>
''');
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/'));
}));
Expand All @@ -144,7 +144,7 @@ void main() {
<!DOCTYPE html><html><head><base href='/basehreftest/'></head></html>
''');
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/'));
}));
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand Down

0 comments on commit a9700ff

Please sign in to comment.