diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 261e1a06b7..1bda14808b 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -169,17 +169,49 @@ class DeviceManager { /// Find and return a list of devices based on the current project and environment. /// - /// Returns a list of deviecs specified by the user. If the user has not specified - /// all devices and has multiple connected then filter the list by those supported - /// in the current project and remove non-ephemeral device types. + /// Returns a list of deviecs specified by the user. + /// + /// * If the user specified '-d all', then return all connected devices which + /// support the current project, except for fuchsia and web. + /// + /// * If the user specified a device id, then do nothing as the list is already + /// filtered by [getDevices]. + /// + /// * If the user did not specify a device id and there is more than one + /// device connected, then filter out unsupported devices and prioritize + /// ephemeral devices. Future> findTargetDevices(FlutterProject flutterProject) async { List devices = await getDevices().toList(); - if (devices.length > 1 && !deviceManager.hasSpecifiedAllDevices && !deviceManager.hasSpecifiedDeviceId) { - devices = devices - .where((Device device) => isDeviceSupportedForProject(device, flutterProject)) - .toList(); + // Always remove web and fuchsia devices from `--all`. This setting + // currently requires devices to share a frontend_server and resident + // runnner instance. Both web and fuchsia require differently configured + // compilers, and web requires an entirely different resident runner. + if (hasSpecifiedAllDevices) { + devices = [ + for (Device device in devices) + if (await device.targetPlatform != TargetPlatform.fuchsia && + await device.targetPlatform != TargetPlatform.web_javascript) + device + ]; + } + // If there is no specified device, the remove all devices which are not + // supported by the current application. For example, if there was no + // 'android' folder then don't attempt to launch with an Android device. + if (devices.length > 1 && !hasSpecifiedDeviceId) { + devices = [ + for (Device device in devices) + if (device.isSupportedForProject(flutterProject)) + device + ]; + } + + // If there are still multiple devices and the user did not specify to run + // all, then attempt to prioritize ephemeral devices. For example, if the + // use only typed 'flutter run' and both an Android device and desktop + // device are availible, choose the Android device. + if (devices.length > 1 && !hasSpecifiedAllDevices) { // Note: ephemeral is nullable for device types where this is not well // defined. if (devices.any((Device device) => device.ephemeral == true)) { diff --git a/packages/flutter_tools/test/device_test.dart b/packages/flutter_tools/test/device_test.dart index 1b49ff9310..ea8bc0abcf 100644 --- a/packages/flutter_tools/test/device_test.dart +++ b/packages/flutter_tools/test/device_test.dart @@ -4,6 +4,7 @@ import 'dart:async'; +import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/project.dart'; @@ -43,12 +44,18 @@ void main() { _MockDevice nonEphemeralOne; _MockDevice nonEphemeralTwo; _MockDevice unsupported; + _MockDevice webDevice; + _MockDevice fuchsiaDevice; setUp(() { ephemeral = _MockDevice('ephemeral', 'ephemeral', true); nonEphemeralOne = _MockDevice('nonEphemeralOne', 'nonEphemeralOne', false); nonEphemeralTwo = _MockDevice('nonEphemeralTwo', 'nonEphemeralTwo', false); unsupported = _MockDevice('unsupported', 'unsupported', true, false); + webDevice = _MockDevice('webby', 'webby') + ..targetPlatform = Future.value(TargetPlatform.web_javascript); + fuchsiaDevice = _MockDevice('fuchsiay', 'fuchsiay') + ..targetPlatform = Future.value(TargetPlatform.fuchsia); }); testUsingContext('chooses ephemeral device', () async { @@ -79,6 +86,36 @@ void main() { nonEphemeralTwo, ]); }); + + testUsingContext('Removes web and fuchsia from --all', () async { + final List devices = [ + webDevice, + fuchsiaDevice, + ]; + final DeviceManager deviceManager = TestDeviceManager(devices); + deviceManager.specifiedDeviceId = 'all'; + + final List filtered = await deviceManager.findTargetDevices(FlutterProject.current()); + + expect(filtered, []); + }); + + testUsingContext('Removes unsupported devices from --all', () async { + final List devices = [ + nonEphemeralOne, + nonEphemeralTwo, + unsupported, + ]; + final DeviceManager deviceManager = TestDeviceManager(devices); + deviceManager.specifiedDeviceId = 'all'; + + final List filtered = await deviceManager.findTargetDevices(FlutterProject.current()); + + expect(filtered, [ + nonEphemeralOne, + nonEphemeralTwo, + ]); + }); }); } @@ -106,6 +143,9 @@ class _MockDevice extends Device { @override final String name; + @override + Future targetPlatform = Future.value(TargetPlatform.android_arm); + @override void noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);