Skip to content

Commit fb6f25c

Browse files
committed
[flutter_tools] Add executable extension artifacts and device wiring
Wire `ExtensionArtifactManager` through `FlutterDeviceManager`, `ExtensionDevices`, and `ExtensionBackedDevice.startApp` so `flutter run`, `flutter drive`, and `flutter test integration_test` resolve and pass `extensionArtifacts` scoped to the owning extension rather than failing on custom `ArtifactService` dependencies. Add `executables` (`Set<String>`) and `isExecutable` (`bool`) to `RemoteArtifactDependency` and apply `chmod a+x` during `ExtensionArtifactManager` staging (rejecting missing or symlinked paths), require `type` and `archiveFormat` on the JSON wire, scope `ExtensionBuildSubCommand` artifact resolution to the target's owning extension, and normalize the `macos` host alias to `darwin` on `HostPlatform`. Part of flutter#190755 Part of flutter#190754
1 parent 8165e11 commit fb6f25c

19 files changed

Lines changed: 1185 additions & 329 deletions

File tree

‎packages/flutter_tools/lib/executable.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ List<FlutterCommand> generateCommands({
324324
androidContext: toolDependencies.androidContext,
325325
androidWorkflow: toolDependencies.androidWorkflow,
326326
deviceManager: toolDependencies.deviceManager,
327-
extensionArtifactManager: toolDependencies.extensionArtifactManager,
328327
verboseHelp: verboseHelp,
329328
),
330329
ScreenshotCommand(toolContext: toolContext),

‎packages/flutter_tools/lib/src/build_system/targets/extension.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ const _targetPlatformIndependentArtifacts = <ExtensionArtifact>{
206206
/// neither a supported built-in artifact nor present in [extensionArtifacts],
207207
/// or if a target-platform-specific built-in artifact is requested on an
208208
/// out-of-tree target platform.
209-
// TODO(bkonyi): Download required artifacts on demand if not already cached.
210209
Map<String, String> resolveExtensionBuildTargetArtifacts({
211210
required Artifacts artifacts,
212211
required BuildMode buildMode,

‎packages/flutter_tools/lib/src/commands/build.dart‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,13 @@ class BuildCommand extends FlutterCommand {
199199
@override
200200
Future<void> initializeDynamicOptions() async {
201201
final ExtensionBuildManager? extensionBuildManager = _extensionBuildManager;
202-
final ExtensionArtifactManager? extensionArtifactManager = _extensionArtifactManager;
203-
if (extensionBuildManager != null && extensionArtifactManager != null) {
202+
if (extensionBuildManager != null) {
204203
final List<ExtensionBuildTarget> targets = await extensionBuildManager.getBuildTargets();
205204
for (final target in targets) {
206205
if (!subcommands.containsKey(target.name)) {
207206
_addSubcommand(
208207
ExtensionBuildSubCommand(
209-
artifactManager: extensionArtifactManager,
208+
artifactManager: _extensionArtifactManager,
210209
buildManager: extensionBuildManager,
211210
target: target,
212211
toolContext: toolContext,
@@ -271,11 +270,11 @@ abstract class BuildSubCommand extends FlutterCommand {
271270
/// A dynamically registered `flutter build` subcommand backed by a tool extension.
272271
class ExtensionBuildSubCommand extends BuildSubCommand {
273272
ExtensionBuildSubCommand({
274-
required this._artifactManager,
275273
required this._buildManager,
276274
required this.target,
277275
required super.toolContext,
278276
required super.verboseHelp,
277+
this._artifactManager,
279278
}) : super(logger: toolContext.logger) {
280279
registerOptionBundle(const BuildModeOptionsBundle());
281280
argParser.addDescriptors(const <OptionDescriptor<Object?>>[
@@ -284,7 +283,7 @@ class ExtensionBuildSubCommand extends BuildSubCommand {
284283
], verboseHelp: verboseHelp);
285284
}
286285

287-
final ExtensionArtifactManager _artifactManager;
286+
final ExtensionArtifactManager? _artifactManager;
288287
final ExtensionBuildManager _buildManager;
289288

290289
/// The custom build target definition provided by the tool extension.
@@ -298,9 +297,13 @@ class ExtensionBuildSubCommand extends BuildSubCommand {
298297

299298
@override
300299
Future<FlutterCommandResult> runCommand() async {
301-
final Map<String, String> extensionArtifacts = await _artifactManager.ensureArtifactsDownloaded(
302-
targetPlatforms: <String>{target.targetPlatform},
303-
);
300+
final String extensionName = _buildManager.extensionNameForBuildTarget(target.name);
301+
final Map<String, String> extensionArtifacts = _artifactManager != null
302+
? await _artifactManager.ensureArtifactsDownloaded(
303+
extensionName: extensionName,
304+
targetPlatforms: <String>{target.targetPlatform},
305+
)
306+
: const <String, String>{};
304307
final ToolContext(:Artifacts artifacts, :Cache cache, :Config config, :FileSystem fs) =
305308
toolContext;
306309
final Directory projectDir = fs.currentDirectory;

‎packages/flutter_tools/lib/src/commands/precache.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ class PrecacheCommand extends FlutterCommand {
219219
if (_extensionArtifactManager != null) {
220220
final Set<String>? targetPlatforms;
221221
if (includeAllPlatforms || downloadDefaultArtifacts) {
222+
// Built-in DevelopmentArtifact flags do not enumerate out-of-tree extension
223+
// target platforms, so pass null on default or --all-platforms runs to let
224+
// active extensions precache their own target platforms for this host.
222225
targetPlatforms = null;
223226
} else {
224227
targetPlatforms = <String>{

‎packages/flutter_tools/lib/src/commands/run.dart‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import '../context/android_context.dart';
2525
import '../context/apple_context.dart';
2626
import '../context/tool_context.dart';
2727
import '../device.dart';
28-
import '../experimental/extension_artifact_manager.dart';
2928
import '../features.dart';
3029
import '../hook_runner.dart' show hookRunner;
3130
import '../ios/devices.dart';
@@ -339,7 +338,6 @@ class RunCommand extends RunCommandBase {
339338
this._androidContext,
340339
this._androidWorkflow,
341340
this.deviceManager,
342-
this._extensionArtifactManager,
343341
super.verboseHelp = false,
344342
}) : super(deviceManager: deviceManager) {
345343
requiresPubspecYaml();
@@ -406,9 +404,6 @@ class RunCommand extends RunCommandBase {
406404
final AndroidWorkflow? _androidWorkflow;
407405
final AppleContext appleContext;
408406
final DeviceManager? deviceManager;
409-
final ExtensionArtifactManager? _extensionArtifactManager;
410-
411-
static const String _defaultCustomTargetPlatformDisplayName = 'custom';
412407

413408
@override
414409
final name = 'run';
@@ -811,35 +806,6 @@ class RunCommand extends RunCommandBase {
811806
}
812807
}
813808

814-
// Ensure any tool extension artifacts required by the target devices are
815-
// downloaded and verified before creating FlutterDevice instances and
816-
// starting the application.
817-
if (_extensionArtifactManager case final ExtensionArtifactManager artifactManager) {
818-
final List<String?> resolvedPlatforms = await Future.wait(
819-
devices!.map((Device device) async {
820-
final TargetPlatform tp = await device.targetPlatform;
821-
// Built-in target platforms resolve directly via TargetPlatform.getName().
822-
if (tp != TargetPlatform.unsupported) {
823-
return tp.getName();
824-
}
825-
// Custom extension devices report TargetPlatform.unsupported and expose
826-
// their platform identifier via targetPlatformDisplayName (or fall back
827-
// to the generic 'custom' display name when unspecified).
828-
final String displayName = await device.targetPlatformDisplayName;
829-
return displayName.isNotEmpty && displayName != _defaultCustomTargetPlatformDisplayName
830-
? displayName
831-
: null;
832-
}),
833-
);
834-
final Set<String> runTargetPlatforms = resolvedPlatforms.nonNulls.toSet();
835-
// If no specific target platform names were resolved (e.g. a custom device
836-
// using the generic 'custom' display name), pass null to download artifacts
837-
// for all target platforms supported by the active extension.
838-
await artifactManager.ensureArtifactsDownloaded(
839-
targetPlatforms: runTargetPlatforms.isNotEmpty ? runTargetPlatforms : null,
840-
);
841-
}
842-
843809
final flutterDevices = <FlutterDevice>[
844810
for (final Device device in devices!)
845811
await FlutterDevice.create(

‎packages/flutter_tools/lib/src/context/tool_dependencies.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ class ToolDependencies {
758758
appleContext: appleContext,
759759
buildSystem: finalBuildSystem,
760760
buildTargets: finalBuildTargets,
761+
extensionArtifactManager: extensionArtifactManager,
761762
extensionBuildManager: extensionBuildManager,
762763
extensionManager: extensionManager,
763764
featureFlags: finalFeatureFlags,

‎packages/flutter_tools/lib/src/experimental/extension_artifact_manager.dart‎

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ExtensionArtifactManager {
4444
static const String _extractedDirName = 'extracted';
4545
static const String _sha256StampSuffix = '.sha256';
4646
static const String _lockFileSuffix = '.lock';
47+
static const String _executableChmodMode = 'a+x';
4748

4849
final ExtensionManager _extensionManager;
4950
final FeatureFlags _featureFlags;
@@ -136,25 +137,33 @@ class ExtensionArtifactManager {
136137
return artifactsCacheDirectory.childDirectory(extensionName).childDirectory(normalizedSha256);
137138
}
138139

139-
/// Queries all active extensions implementing [ArtifactService] and ensures
140-
/// required artifacts for the current engine version, host platform, and
141-
/// optional [targetPlatforms] are resolved on disk or downloaded and verified
142-
/// in the shared cache.
140+
/// Queries active extensions implementing [ArtifactService] (or only the
141+
/// extension matching [extensionName] when specified) and ensures required
142+
/// artifacts for the current engine version, host platform, and optional
143+
/// [targetPlatforms] are resolved on disk or downloaded and verified in the
144+
/// shared cache.
143145
///
144146
/// When [targetPlatforms] is `null`, artifacts for all target platforms
145-
/// supported by each active extension are requested.
147+
/// supported by each queried extension are requested.
146148
///
147149
/// Returns a map from each resolved artifact's [ArtifactDependency.name] to
148150
/// its local filesystem path, or an empty map when tool extensions are
149-
/// disabled, extension initialization fails, or no active extension implements
150-
/// [ArtifactService].
151+
/// disabled, extension initialization fails, or no matching active extension
152+
/// implements [ArtifactService]. When [extensionName] is provided (for
153+
/// example, during `flutter build` or `ExtensionBackedDevice.startApp`), the
154+
/// returned map is scoped strictly to that owning extension; when
155+
/// [extensionName] is `null` (for example, during `flutter precache`), all
156+
/// extensions' artifacts are downloaded into their isolated
157+
/// `<cacheDir>/<extensionName>/<sha256>/<name>` directories.
151158
///
152159
/// RPC failures when querying an extension's artifact list log a warning and
153160
/// skip that extension, whereas missing local artifacts or download,
154-
/// extraction, or SHA-256 verification failures throw a [ToolExit].
161+
/// extraction, executable permission validation, or SHA-256 verification
162+
/// failures throw a [ToolExit].
155163
///
156164
/// When [force] is `true`, existing cached remote artifacts are re-downloaded.
157165
Future<Map<String, String>> ensureArtifactsDownloaded({
166+
String? extensionName,
158167
bool force = false,
159168
Set<String>? targetPlatforms,
160169
}) async {
@@ -172,7 +181,9 @@ class ExtensionArtifactManager {
172181

173182
final artifactExtensions = <ExtensionConnection>[
174183
for (final ext in _extensionManager.connections)
175-
if (ext.capabilities.services.contains(ArtifactService.serviceNamespace)) ext,
184+
if (ext.capabilities.services.contains(ArtifactService.serviceNamespace) &&
185+
(extensionName == null || ext.extensionName == extensionName))
186+
ext,
176187
];
177188

178189
if (artifactExtensions.isEmpty) {
@@ -181,12 +192,6 @@ class ExtensionArtifactManager {
181192

182193
final HostPlatform currentHostPlatform = os.hostPlatform;
183194
final String engineVersion = _engineVersion;
184-
if (!ArtifactDependency.isValidSinglePathSegment(engineVersion)) {
185-
logger.printWarning(
186-
'Skipping extension artifacts due to invalid engine version "$engineVersion".',
187-
);
188-
return const <String, String>{};
189-
}
190195

191196
final resolvedArtifacts = <String, String>{};
192197
for (final ext in artifactExtensions) {
@@ -213,15 +218,16 @@ class ExtensionArtifactManager {
213218
dep,
214219
];
215220

221+
final seenForExtension = <String>{};
216222
for (final dep in filteredDeps) {
217-
if (resolvedArtifacts.containsKey(dep.name)) {
223+
if (!seenForExtension.add(dep.name)) {
218224
logger.printWarning(
219-
'Artifact "${dep.name}" from extension "${ext.extensionName}" conflicts with an '
220-
'artifact of the same name from another extension and will be ignored.',
225+
'Extension "${ext.extensionName}" declared duplicate artifact "${dep.name}"; '
226+
'keeping the first declaration.',
221227
);
222228
continue;
223229
}
224-
resolvedArtifacts[dep.name] = switch (dep) {
230+
final String resolvedPath = switch (dep) {
225231
final LocalArtifactDependency localDep => _resolveLocalArtifact(
226232
ext.extensionName,
227233
localDep,
@@ -232,6 +238,7 @@ class ExtensionArtifactManager {
232238
force: force,
233239
),
234240
};
241+
resolvedArtifacts.putIfAbsent(dep.name, () => resolvedPath);
235242
}
236243
}
237244
return resolvedArtifacts;
@@ -390,6 +397,8 @@ class ExtensionArtifactManager {
390397
artifactFile: artifactFile,
391398
dep: dep,
392399
expectedSha: expectedSha,
400+
extensionName: extensionName,
401+
fs: fs,
393402
isZip: isZip,
394403
os: os,
395404
stagingDir: stagingDir,
@@ -507,13 +516,16 @@ Actual: $actualDigest''');
507516
}
508517

509518
/// Installs a verified [stagingFile] into its final cache destination
510-
/// ([artifactDir] when [isZip] is `true`, or [artifactFile] otherwise) and
511-
/// writes [expectedSha] to [stampFile] last via renames from [stagingDir].
519+
/// ([artifactDir] when [isZip] is `true`, or [artifactFile] otherwise),
520+
/// applies declared POSIX executable permissions, and writes [expectedSha] to
521+
/// [stampFile] last via renames from [stagingDir].
512522
void _installStagedArtifact({
513523
required Directory artifactDir,
514524
required File artifactFile,
515525
required RemoteArtifactDependency dep,
516526
required String expectedSha,
527+
required String extensionName,
528+
required FileSystem fs,
517529
required bool isZip,
518530
required OperatingSystemUtils os,
519531
required Directory stagingDir,
@@ -531,11 +543,21 @@ Actual: $actualDigest''');
531543
} on Object catch (e) {
532544
throwToolExit('Failed to extract artifact "${dep.name}" from ${dep.url}: $e');
533545
}
546+
_applyArchiveExecutablePermissions(
547+
dep: dep,
548+
extensionName: extensionName,
549+
extractedDir: extractedDir,
550+
fs: fs,
551+
os: os,
552+
);
534553
if (artifactDir.existsSync()) {
535554
artifactDir.deleteSync(recursive: true);
536555
}
537556
extractedDir.renameSync(artifactDir.path);
538557
} else {
558+
if (dep.isExecutable) {
559+
os.chmod(stagingFile, _executableChmodMode);
560+
}
539561
if (artifactFile.existsSync()) {
540562
artifactFile.deleteSync();
541563
}
@@ -545,4 +567,51 @@ Actual: $actualDigest''');
545567
..writeAsStringSync(expectedSha);
546568
stagingStamp.renameSync(stampFile.path);
547569
}
570+
571+
/// Validates every entry in [RemoteArtifactDependency.executables] within
572+
/// [extractedDir] (rejecting out-of-tree paths, missing files, and symbolic
573+
/// links along any directory or file segment) and applies `chmod a+x`.
574+
void _applyArchiveExecutablePermissions({
575+
required RemoteArtifactDependency dep,
576+
required String extensionName,
577+
required Directory extractedDir,
578+
required FileSystem fs,
579+
required OperatingSystemUtils os,
580+
}) {
581+
for (final String relativePath in dep.executables) {
582+
assert(ArtifactDependency.isValidExecutableSubpath(relativePath));
583+
final List<String> segments = relativePath.split('/');
584+
String currentPath = extractedDir.path;
585+
for (var i = 0; i < segments.length - 1; i++) {
586+
currentPath = fs.path.join(currentPath, segments[i]);
587+
if (fs.isLinkSync(currentPath)) {
588+
throwToolExit(
589+
'Executable "$relativePath" in artifact "${dep.name}" for extension '
590+
'"$extensionName" traverses a symbolic link.',
591+
);
592+
}
593+
if (!fs.directory(currentPath).existsSync()) {
594+
throwToolExit(
595+
'Executable "$relativePath" declared by artifact "${dep.name}" for extension '
596+
'"$extensionName" was not found in the extracted archive.',
597+
);
598+
}
599+
}
600+
final String execPath = fs.path.join(currentPath, segments.last);
601+
if (fs.isLinkSync(execPath)) {
602+
throwToolExit(
603+
'Executable "$relativePath" in artifact "${dep.name}" for extension '
604+
'"$extensionName" must not be a symbolic link.',
605+
);
606+
}
607+
final File execFile = fs.file(execPath);
608+
if (!execFile.existsSync()) {
609+
throwToolExit(
610+
'Executable "$relativePath" declared by artifact "${dep.name}" for extension '
611+
'"$extensionName" was not found in the extracted archive.',
612+
);
613+
}
614+
os.chmod(execFile, _executableChmodMode);
615+
}
616+
}
548617
}

‎packages/flutter_tools/lib/src/experimental/extension_build_manager.dart‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ base class ExtensionBuildManager {
3939
List<ExtensionAssembleTarget> get cachedAssembleTargets =>
4040
_cachedAssembleTargets ?? const <ExtensionAssembleTarget>[];
4141

42+
/// Returns the name of the extension that registered the custom `flutter build`
43+
/// target [targetName], or `null` if no extension registered it.
44+
String? tryExtensionNameForBuildTarget(String targetName) =>
45+
_buildTargetToConnection[targetName]?.extensionName;
46+
4247
/// Returns the name of the extension that registered the custom `flutter build`
4348
/// target [targetName], or [ExtensionConnection.defaultExtensionName] if no
4449
/// extension registered it.
4550
String extensionNameForBuildTarget(String targetName) =>
46-
_buildTargetToConnection[targetName]?.extensionName ??
47-
ExtensionConnection.defaultExtensionName;
51+
tryExtensionNameForBuildTarget(targetName) ?? ExtensionConnection.defaultExtensionName;
4852

4953
/// Returns the name of the extension that registered the custom `flutter assemble`
5054
/// target [targetName], or [ExtensionConnection.defaultExtensionName] if no

0 commit comments

Comments
 (0)