Skip to content

Commit 2dd87fb

Browse files
authored
Fix --local-engine for the new web/wasm mode (#113759)
1 parent 903e9fb commit 2dd87fb

File tree

6 files changed

+352
-28
lines changed

6 files changed

+352
-28
lines changed

packages/flutter_tools/lib/src/artifacts.dart

+52-9
Original file line numberDiff line numberDiff line change
@@ -768,24 +768,22 @@ class CachedLocalEngineArtifacts implements LocalEngineArtifacts {
768768

769769

770770
@override
771-
FileSystemEntity getHostArtifact(
772-
HostArtifact artifact,
773-
) {
771+
FileSystemEntity getHostArtifact(HostArtifact artifact) {
774772
switch (artifact) {
775773
case HostArtifact.engineDartSdkPath:
776-
final String path = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk');
774+
final String path = _getDartSdkPath();
777775
return _fileSystem.directory(path);
778776
case HostArtifact.engineDartBinary:
779-
final String path = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk', 'bin', _hostArtifactToFileName(artifact, _platform));
777+
final String path = _fileSystem.path.join(_getDartSdkPath(), 'bin', _hostArtifactToFileName(artifact, _platform));
780778
return _fileSystem.file(path);
781779
case HostArtifact.dart2jsSnapshot:
782-
final String path = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk', 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform));
780+
final String path = _fileSystem.path.join(_getDartSdkPath(), 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform));
783781
return _fileSystem.file(path);
784782
case HostArtifact.dartdevcSnapshot:
785-
final String path = _fileSystem.path.join(_dartSdkPath(_cache), 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform));
783+
final String path = _fileSystem.path.join(_getDartSdkPath(), 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform));
786784
return _fileSystem.file(path);
787785
case HostArtifact.kernelWorkerSnapshot:
788-
final String path = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk', 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform));
786+
final String path = _fileSystem.path.join(_getDartSdkPath(), 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform));
789787
return _fileSystem.file(path);
790788
case HostArtifact.flutterWebSdk:
791789
final String path = _getFlutterWebSdkPath();
@@ -908,7 +906,7 @@ class CachedLocalEngineArtifacts implements LocalEngineArtifacts {
908906
return _fileSystem.path.join(_hostEngineOutPath, artifactFileName);
909907
case Artifact.frontendServerSnapshotForEngineDartSdk:
910908
return _fileSystem.path.join(
911-
_hostEngineOutPath, 'dart-sdk', 'bin', 'snapshots', artifactFileName,
909+
_getDartSdkPath(), 'bin', 'snapshots', artifactFileName,
912910
);
913911
}
914912
}
@@ -923,6 +921,51 @@ class CachedLocalEngineArtifacts implements LocalEngineArtifacts {
923921
buildMode == BuildMode.release ? 'flutter_patched_sdk_product' : 'flutter_patched_sdk');
924922
}
925923

924+
String _getDartSdkPath() {
925+
final String builtPath = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk');
926+
if (_fileSystem.isDirectorySync(_fileSystem.path.join(builtPath, 'bin'))) {
927+
return builtPath;
928+
}
929+
930+
// If we couldn't find a built dart sdk, let's look for a prebuilt one.
931+
final String prebuiltPath = _fileSystem.path.join(_getFlutterPrebuiltsPath(), _getPrebuiltTarget(), 'dart-sdk');
932+
if (_fileSystem.isDirectorySync(prebuiltPath)) {
933+
return prebuiltPath;
934+
}
935+
936+
throw ToolExit('Unable to find a built dart sdk at: "$builtPath" or a prebuilt dart sdk at: "$prebuiltPath"');
937+
}
938+
939+
String _getFlutterPrebuiltsPath() {
940+
final String engineSrcPath = _fileSystem.path.dirname(_fileSystem.path.dirname(_hostEngineOutPath));
941+
return _fileSystem.path.join(engineSrcPath, 'flutter', 'prebuilts');
942+
}
943+
944+
String _getPrebuiltTarget() {
945+
final TargetPlatform hostPlatform = _currentHostPlatform(_platform, _operatingSystemUtils);
946+
switch (hostPlatform) {
947+
case TargetPlatform.darwin:
948+
return 'macos-x64';
949+
case TargetPlatform.linux_arm64:
950+
return 'linux-arm64';
951+
case TargetPlatform.linux_x64:
952+
return 'linux-x64';
953+
case TargetPlatform.windows_x64:
954+
return 'windows-x64';
955+
case TargetPlatform.ios:
956+
case TargetPlatform.android:
957+
case TargetPlatform.android_arm:
958+
case TargetPlatform.android_arm64:
959+
case TargetPlatform.android_x64:
960+
case TargetPlatform.android_x86:
961+
case TargetPlatform.fuchsia_arm64:
962+
case TargetPlatform.fuchsia_x64:
963+
case TargetPlatform.web_javascript:
964+
case TargetPlatform.tester:
965+
throwToolExit('Unsupported host platform: $hostPlatform');
966+
}
967+
}
968+
926969
String _getFlutterWebSdkPath() {
927970
return _fileSystem.path.join(engineOutPath, 'flutter_web_sdk');
928971
}

packages/flutter_tools/lib/src/asset.dart

+24-17
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ class ManifestAssetBundle implements AssetBundle {
252252
flutterManifest,
253253
wildcardDirectories,
254254
assetBasePath,
255+
targetPlatform,
255256
);
256257

257258
if (assetVariants == null) {
@@ -316,6 +317,7 @@ class ManifestAssetBundle implements AssetBundle {
316317
// Do not track wildcard directories for dependencies.
317318
<Uri>[],
318319
packageBasePath,
320+
targetPlatform,
319321
packageName: package.name,
320322
attributedPackage: package,
321323
);
@@ -407,10 +409,11 @@ class ManifestAssetBundle implements AssetBundle {
407409
final List<_Asset> materialAssets = <_Asset>[
408410
if (flutterManifest.usesMaterialDesign)
409411
..._getMaterialFonts(),
410-
// Include the shaders unconditionally. They are small, and whether
411-
// they're used is determined only by the app source code and not by
412-
// the Flutter manifest.
413-
..._getMaterialShaders(),
412+
// For non-web platforms, include the shaders unconditionally. They are
413+
// small, and whether they're used is determined only by the app source
414+
// code and not by the Flutter manifest.
415+
if (targetPlatform != TargetPlatform.web_javascript)
416+
..._getMaterialShaders(),
414417
];
415418
for (final _Asset asset in materialAssets) {
416419
final File assetFile = asset.lookupAssetFile(_fileSystem);
@@ -716,7 +719,8 @@ class ManifestAssetBundle implements AssetBundle {
716719
PackageConfig packageConfig,
717720
FlutterManifest flutterManifest,
718721
List<Uri> wildcardDirectories,
719-
String assetBase, {
722+
String assetBase,
723+
TargetPlatform? targetPlatform, {
720724
String? packageName,
721725
Package? attributedPackage,
722726
}) {
@@ -750,18 +754,21 @@ class ManifestAssetBundle implements AssetBundle {
750754
}
751755
}
752756

753-
for (final Uri shaderUri in flutterManifest.shaders) {
754-
_parseAssetFromFile(
755-
packageConfig,
756-
flutterManifest,
757-
assetBase,
758-
cache,
759-
result,
760-
shaderUri,
761-
packageName: packageName,
762-
attributedPackage: attributedPackage,
763-
assetKind: AssetKind.shader,
764-
);
757+
// No shader compilation for the web.
758+
if (targetPlatform != TargetPlatform.web_javascript) {
759+
for (final Uri shaderUri in flutterManifest.shaders) {
760+
_parseAssetFromFile(
761+
packageConfig,
762+
flutterManifest,
763+
assetBase,
764+
cache,
765+
result,
766+
shaderUri,
767+
packageName: packageName,
768+
attributedPackage: attributedPackage,
769+
assetKind: AssetKind.shader,
770+
);
771+
}
765772
}
766773

767774
// Add assets referenced in the fonts section of the manifest.

packages/flutter_tools/lib/src/runner/local_engine.dart

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ class LocalEngineLocator {
150150
// Determine the host engine directory associated with the local engine:
151151
// Strip '_sim_' since there are no host simulator builds.
152152
String _getHostEngineBasename(String localEngineBasename) {
153+
if (localEngineBasename.startsWith('web_') || localEngineBasename.startsWith('wasm_')) {
154+
// Don't modify the web local engine's basename.
155+
return localEngineBasename;
156+
}
157+
153158
String tmpBasename = localEngineBasename.replaceFirst('_sim_', '_');
154159
tmpBasename = tmpBasename.substring(tmpBasename.indexOf('_') + 1);
155160
// Strip suffix for various archs.

packages/flutter_tools/test/general.shard/artifacts_test.dart

+148-2
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ void main() {
273273
.childDirectory('ios-arm64_armv7')
274274
.childDirectory('Flutter.framework')
275275
.createSync(recursive: true);
276+
fileSystem
277+
.directory('out')
278+
.childDirectory('host_debug_unopt')
279+
.childDirectory('dart-sdk')
280+
.childDirectory('bin')
281+
.createSync(recursive: true);
282+
276283
expect(
277284
artifacts.getArtifactPath(
278285
Artifact.flutterFramework,
@@ -325,6 +332,75 @@ void main() {
325332
);
326333
});
327334

335+
testWithoutContext('falls back to prebuilt dart sdk', () {
336+
final String failureMessage = 'Unable to find a built dart sdk at:'
337+
' "${fileSystem.path.join('/out', 'host_debug_unopt', 'dart-sdk')}"'
338+
' or a prebuilt dart sdk at:'
339+
' "${fileSystem.path.join('/flutter', 'prebuilts', 'linux-x64', 'dart-sdk')}"';
340+
341+
expect(
342+
() => artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
343+
throwsToolExit(message: failureMessage),
344+
);
345+
expect(
346+
() => artifacts.getHostArtifact(HostArtifact.engineDartSdkPath),
347+
throwsToolExit(message: failureMessage),
348+
);
349+
expect(
350+
() => artifacts.getHostArtifact(HostArtifact.engineDartBinary),
351+
throwsToolExit(message: failureMessage),
352+
);
353+
expect(
354+
() => artifacts.getHostArtifact(HostArtifact.dart2jsSnapshot),
355+
throwsToolExit(message: failureMessage),
356+
);
357+
expect(
358+
() => artifacts.getHostArtifact(HostArtifact.dartdevcSnapshot),
359+
throwsToolExit(message: failureMessage),
360+
);
361+
expect(
362+
() => artifacts.getHostArtifact(HostArtifact.kernelWorkerSnapshot),
363+
throwsToolExit(message: failureMessage),
364+
);
365+
366+
fileSystem
367+
.directory('flutter')
368+
.childDirectory('prebuilts')
369+
.childDirectory('linux-x64')
370+
.childDirectory('dart-sdk')
371+
.childDirectory('bin')
372+
.createSync(recursive: true);
373+
374+
expect(
375+
artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
376+
fileSystem.path.join('/flutter', 'prebuilts', 'linux-x64', 'dart-sdk', 'bin',
377+
'snapshots', 'frontend_server.dart.snapshot'),
378+
);
379+
expect(
380+
artifacts.getHostArtifact(HostArtifact.engineDartSdkPath).path,
381+
fileSystem.path.join('/flutter', 'prebuilts', 'linux-x64', 'dart-sdk'),
382+
);
383+
expect(
384+
artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
385+
fileSystem.path.join('/flutter', 'prebuilts', 'linux-x64', 'dart-sdk', 'bin', 'dart'),
386+
);
387+
expect(
388+
artifacts.getHostArtifact(HostArtifact.dart2jsSnapshot).path,
389+
fileSystem.path.join('/flutter', 'prebuilts', 'linux-x64', 'dart-sdk',
390+
'bin', 'snapshots', 'dart2js.dart.snapshot'),
391+
);
392+
expect(
393+
artifacts.getHostArtifact(HostArtifact.dartdevcSnapshot).path,
394+
fileSystem.path.join('/flutter', 'prebuilts', 'linux-x64', 'dart-sdk',
395+
'bin', 'snapshots', 'dartdevc.dart.snapshot'),
396+
);
397+
expect(
398+
artifacts.getHostArtifact(HostArtifact.kernelWorkerSnapshot).path,
399+
fileSystem.path.join('/flutter', 'prebuilts', 'linux-x64', 'dart-sdk',
400+
'bin', 'snapshots', 'kernel_worker.dart.snapshot'),
401+
);
402+
});
403+
328404
testWithoutContext('getEngineType', () {
329405
expect(
330406
artifacts.getEngineType(TargetPlatform.android_arm, BuildMode.debug),
@@ -351,11 +427,81 @@ void main() {
351427
operatingSystemUtils: FakeOperatingSystemUtils(),
352428
);
353429

354-
expect(artifacts.getHostArtifact(HostArtifact.engineDartBinary).path, contains('.exe'));
430+
fileSystem
431+
.directory('out')
432+
.childDirectory('host_debug_unopt')
433+
.childDirectory('dart-sdk')
434+
.childDirectory('bin')
435+
.createSync(recursive: true);
436+
437+
expect(
438+
artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
439+
fileSystem.path.join('/out', 'host_debug_unopt', 'dart-sdk', 'bin', 'dart.exe'),
440+
);
355441
});
356442

357443
testWithoutContext('Looks up dart on linux platforms', () async {
358-
expect(artifacts.getHostArtifact(HostArtifact.engineDartBinary).path, isNot(contains('.exe')));
444+
fileSystem
445+
.directory('/out')
446+
.childDirectory('host_debug_unopt')
447+
.childDirectory('dart-sdk')
448+
.childDirectory('bin')
449+
.createSync(recursive: true);
450+
451+
expect(
452+
artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
453+
fileSystem.path.join('/out', 'host_debug_unopt', 'dart-sdk', 'bin', 'dart'),
454+
);
455+
});
456+
457+
testWithoutContext('Finds dart-sdk in windows prebuilts', () async {
458+
artifacts = LocalEngineArtifacts(
459+
fileSystem.path.join(fileSystem.currentDirectory.path, 'out', 'android_debug_unopt'),
460+
fileSystem.path.join(fileSystem.currentDirectory.path, 'out', 'host_debug_unopt'),
461+
cache: cache,
462+
fileSystem: fileSystem,
463+
platform: FakePlatform(operatingSystem: 'windows'),
464+
processManager: FakeProcessManager.any(),
465+
operatingSystemUtils: FakeOperatingSystemUtils(),
466+
);
467+
468+
fileSystem
469+
.directory('/flutter')
470+
.childDirectory('prebuilts')
471+
.childDirectory('windows-x64')
472+
.childDirectory('dart-sdk')
473+
.childDirectory('bin')
474+
.createSync(recursive: true);
475+
476+
expect(
477+
artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
478+
fileSystem.path.join('/flutter', 'prebuilts', 'windows-x64', 'dart-sdk', 'bin', 'dart.exe'),
479+
);
480+
});
481+
482+
testWithoutContext('Finds dart-sdk in macos prebuilts', () async {
483+
artifacts = LocalEngineArtifacts(
484+
fileSystem.path.join(fileSystem.currentDirectory.path, 'out', 'android_debug_unopt'),
485+
fileSystem.path.join(fileSystem.currentDirectory.path, 'out', 'host_debug_unopt'),
486+
cache: cache,
487+
fileSystem: fileSystem,
488+
platform: FakePlatform(operatingSystem: 'macos'),
489+
processManager: FakeProcessManager.any(),
490+
operatingSystemUtils: FakeOperatingSystemUtils(),
491+
);
492+
493+
fileSystem
494+
.directory('/flutter')
495+
.childDirectory('prebuilts')
496+
.childDirectory('macos-x64')
497+
.childDirectory('dart-sdk')
498+
.childDirectory('bin')
499+
.createSync(recursive: true);
500+
501+
expect(
502+
artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
503+
fileSystem.path.join('/flutter', 'prebuilts', 'macos-x64', 'dart-sdk', 'bin', 'dart'),
504+
);
359505
});
360506
});
361507
}

0 commit comments

Comments
 (0)