Unverified Commit aa394331 authored by Yegor's avatar Yegor Committed by GitHub

Fix dartDefines in daemon mode (#45317)

* de-null dartDefines in daemon mode
* remove daemonCommand; pipe through dartDefines into Daemon
* pass dartDefiles in attach; add test for --machine mode
parent a60bf8e2
...@@ -198,8 +198,13 @@ class AttachCommand extends FlutterCommand { ...@@ -198,8 +198,13 @@ class AttachCommand extends FlutterCommand {
final int devicePort = await getDevicePort(); final int devicePort = await getDevicePort();
final Daemon daemon = boolArg('machine') final Daemon daemon = boolArg('machine')
? Daemon(stdinCommandStream, stdoutCommandResponse, ? Daemon(
notifyingLogger: NotifyingLogger(), logToStdout: true) stdinCommandStream,
stdoutCommandResponse,
notifyingLogger: NotifyingLogger(),
logToStdout: true,
dartDefines: dartDefines,
)
: null; : null;
Stream<Uri> observatoryUri; Stream<Uri> observatoryUri;
......
...@@ -35,7 +35,9 @@ const String protocolVersion = '0.5.3'; ...@@ -35,7 +35,9 @@ const String protocolVersion = '0.5.3';
/// It can be shutdown with a `daemon.shutdown` command (or by killing the /// It can be shutdown with a `daemon.shutdown` command (or by killing the
/// process). /// process).
class DaemonCommand extends FlutterCommand { class DaemonCommand extends FlutterCommand {
DaemonCommand({ this.hidden = false }); DaemonCommand({ this.hidden = false }) {
usesDartDefines();
}
@override @override
final String name = 'daemon'; final String name = 'daemon';
...@@ -58,8 +60,10 @@ class DaemonCommand extends FlutterCommand { ...@@ -58,8 +60,10 @@ class DaemonCommand extends FlutterCommand {
await context.run<void>( await context.run<void>(
body: () async { body: () async {
final Daemon daemon = Daemon( final Daemon daemon = Daemon(
stdinCommandStream, stdoutCommandResponse, stdinCommandStream, stdoutCommandResponse,
daemonCommand: this, notifyingLogger: notifyingLogger); notifyingLogger: notifyingLogger,
dartDefines: dartDefines,
);
final int code = await daemon.onExit; final int code = await daemon.onExit;
if (code != 0) { if (code != 0) {
...@@ -82,10 +86,17 @@ class Daemon { ...@@ -82,10 +86,17 @@ class Daemon {
Daemon( Daemon(
Stream<Map<String, dynamic>> commandStream, Stream<Map<String, dynamic>> commandStream,
this.sendCommand, { this.sendCommand, {
this.daemonCommand,
this.notifyingLogger, this.notifyingLogger,
this.logToStdout = false, this.logToStdout = false,
@required this.dartDefines,
}) { }) {
if (dartDefines == null) {
throw Exception(
'dartDefines must not be null. This is a bug in Flutter.\n'
'Please file an issue at https://github.com/flutter/flutter/issues/new/choose',
);
}
// Set up domains. // Set up domains.
_registerDomain(daemonDomain = DaemonDomain(this)); _registerDomain(daemonDomain = DaemonDomain(this));
_registerDomain(appDomain = AppDomain(this)); _registerDomain(appDomain = AppDomain(this));
...@@ -110,9 +121,9 @@ class Daemon { ...@@ -110,9 +121,9 @@ class Daemon {
StreamSubscription<Map<String, dynamic>> _commandSubscription; StreamSubscription<Map<String, dynamic>> _commandSubscription;
final DispatchCommand sendCommand; final DispatchCommand sendCommand;
final DaemonCommand daemonCommand;
final NotifyingLogger notifyingLogger; final NotifyingLogger notifyingLogger;
final bool logToStdout; final bool logToStdout;
final List<String> dartDefines;
final Completer<int> _onExitCompleter = Completer<int>(); final Completer<int> _onExitCompleter = Completer<int>();
final Map<String, Domain> _domainMap = <String, Domain>{}; final Map<String, Domain> _domainMap = <String, Domain>{};
...@@ -184,8 +195,6 @@ abstract class Domain { ...@@ -184,8 +195,6 @@ abstract class Domain {
_handlers[name] = handler; _handlers[name] = handler;
} }
FlutterCommand get command => daemon.daemonCommand;
@override @override
String toString() => name; String toString() => name;
...@@ -424,7 +433,7 @@ class AppDomain extends Domain { ...@@ -424,7 +433,7 @@ class AppDomain extends Domain {
viewFilter: isolateFilter, viewFilter: isolateFilter,
target: target, target: target,
buildMode: options.buildInfo.mode, buildMode: options.buildInfo.mode,
dartDefines: command?.dartDefines, dartDefines: daemon.dartDefines,
); );
ResidentRunner runner; ResidentRunner runner;
...@@ -437,7 +446,7 @@ class AppDomain extends Domain { ...@@ -437,7 +446,7 @@ class AppDomain extends Domain {
debuggingOptions: options, debuggingOptions: options,
ipv6: ipv6, ipv6: ipv6,
stayResident: true, stayResident: true,
dartDefines: command?.dartDefines, dartDefines: daemon.dartDefines,
); );
} else if (enableHotReload) { } else if (enableHotReload) {
runner = HotRunner( runner = HotRunner(
......
...@@ -340,8 +340,13 @@ class RunCommand extends RunCommandBase { ...@@ -340,8 +340,13 @@ class RunCommand extends RunCommandBase {
if (devices.length > 1) { if (devices.length > 1) {
throwToolExit('--machine does not support -d all.'); throwToolExit('--machine does not support -d all.');
} }
final Daemon daemon = Daemon(stdinCommandStream, stdoutCommandResponse, final Daemon daemon = Daemon(
notifyingLogger: NotifyingLogger(), logToStdout: true); stdinCommandStream,
stdoutCommandResponse,
notifyingLogger: NotifyingLogger(),
logToStdout: true,
dartDefines: dartDefines,
);
AppInstance app; AppInstance app;
try { try {
final String applicationBinaryPath = stringArg('use-application-binary'); final String applicationBinaryPath = stringArg('use-application-binary');
......
...@@ -39,6 +39,7 @@ void main() { ...@@ -39,6 +39,7 @@ void main() {
commands.stream, commands.stream,
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
commands.add(<String, dynamic>{'id': 0, 'method': 'daemon.version'}); commands.add(<String, dynamic>{'id': 0, 'method': 'daemon.version'});
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent); final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
...@@ -56,6 +57,7 @@ void main() { ...@@ -56,6 +57,7 @@ void main() {
commands.stream, commands.stream,
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
printError('daemon.logMessage test'); printError('daemon.logMessage test');
final Map<String, dynamic> response = await responses.stream.firstWhere((Map<String, dynamic> map) { final Map<String, dynamic> response = await responses.stream.firstWhere((Map<String, dynamic> map) {
...@@ -83,6 +85,7 @@ void main() { ...@@ -83,6 +85,7 @@ void main() {
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
logToStdout: true, logToStdout: true,
dartDefines: const <String>[],
); );
printStatus('daemon.logMessage test'); printStatus('daemon.logMessage test');
// Service the event loop. // Service the event loop.
...@@ -103,6 +106,7 @@ void main() { ...@@ -103,6 +106,7 @@ void main() {
commands.stream, commands.stream,
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
commands.add(<String, dynamic>{'id': 0, 'method': 'daemon.shutdown'}); commands.add(<String, dynamic>{'id': 0, 'method': 'daemon.shutdown'});
return daemon.onExit.then<void>((int code) async { return daemon.onExit.then<void>((int code) async {
...@@ -112,16 +116,13 @@ void main() { ...@@ -112,16 +116,13 @@ void main() {
}); });
testUsingContext('app.restart without an appId should report an error', () async { testUsingContext('app.restart without an appId should report an error', () async {
final DaemonCommand command = DaemonCommand();
applyMocksToCommand(command);
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon( daemon = Daemon(
commands.stream, commands.stream,
responses.add, responses.add,
daemonCommand: command,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
commands.add(<String, dynamic>{'id': 0, 'method': 'app.restart'}); commands.add(<String, dynamic>{'id': 0, 'method': 'app.restart'});
...@@ -133,16 +134,13 @@ void main() { ...@@ -133,16 +134,13 @@ void main() {
}); });
testUsingContext('ext.flutter.debugPaint via service extension without an appId should report an error', () async { testUsingContext('ext.flutter.debugPaint via service extension without an appId should report an error', () async {
final DaemonCommand command = DaemonCommand();
applyMocksToCommand(command);
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon( daemon = Daemon(
commands.stream, commands.stream,
responses.add, responses.add,
daemonCommand: command,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
commands.add(<String, dynamic>{ commands.add(<String, dynamic>{
...@@ -160,16 +158,13 @@ void main() { ...@@ -160,16 +158,13 @@ void main() {
}); });
testUsingContext('app.stop without appId should report an error', () async { testUsingContext('app.stop without appId should report an error', () async {
final DaemonCommand command = DaemonCommand();
applyMocksToCommand(command);
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon( daemon = Daemon(
commands.stream, commands.stream,
responses.add, responses.add,
daemonCommand: command,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
commands.add(<String, dynamic>{'id': 0, 'method': 'app.stop'}); commands.add(<String, dynamic>{'id': 0, 'method': 'app.stop'});
...@@ -187,6 +182,7 @@ void main() { ...@@ -187,6 +182,7 @@ void main() {
commands.stream, commands.stream,
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
commands.add(<String, dynamic>{'id': 0, 'method': 'device.getDevices'}); commands.add(<String, dynamic>{'id': 0, 'method': 'device.getDevices'});
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent); final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
...@@ -203,6 +199,7 @@ void main() { ...@@ -203,6 +199,7 @@ void main() {
commands.stream, commands.stream,
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery(); final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery();
daemon.deviceDomain.addDeviceDiscoverer(discoverer); daemon.deviceDomain.addDeviceDiscoverer(discoverer);
...@@ -221,9 +218,10 @@ void main() { ...@@ -221,9 +218,10 @@ void main() {
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon( daemon = Daemon(
commands.stream, commands.stream,
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery(); final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery();
...@@ -247,16 +245,13 @@ void main() { ...@@ -247,16 +245,13 @@ void main() {
}); });
testUsingContext('emulator.launch without an emulatorId should report an error', () async { testUsingContext('emulator.launch without an emulatorId should report an error', () async {
final DaemonCommand command = DaemonCommand();
applyMocksToCommand(command);
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon( daemon = Daemon(
commands.stream, commands.stream,
responses.add, responses.add,
daemonCommand: command,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
commands.add(<String, dynamic>{'id': 0, 'method': 'emulator.launch'}); commands.add(<String, dynamic>{'id': 0, 'method': 'emulator.launch'});
...@@ -274,6 +269,7 @@ void main() { ...@@ -274,6 +269,7 @@ void main() {
commands.stream, commands.stream,
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
); );
commands.add(<String, dynamic>{'id': 0, 'method': 'emulator.getEmulators'}); commands.add(<String, dynamic>{'id': 0, 'method': 'emulator.getEmulators'});
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent); final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
......
...@@ -244,6 +244,37 @@ void main() { ...@@ -244,6 +244,37 @@ void main() {
FlutterVersion: () => mockStableFlutterVersion, FlutterVersion: () => mockStableFlutterVersion,
WebRunnerFactory: () => mockWebRunnerFactory, WebRunnerFactory: () => mockWebRunnerFactory,
}); });
testUsingContext('populates dartDefines in --machine mode', () async {
final Directory tempDir = fs.systemTempDirectory.createTempSync('flutter_run_test.');
fs.currentDirectory = tempDir;
final Directory libDir = tempDir.childDirectory('lib');
libDir.createSync();
final File mainFile = libDir.childFile('main.dart');
mainFile.writeAsStringSync('void main() {}');
final Directory webDir = tempDir.childDirectory('web');
webDir.createSync();
final File indexFile = libDir.childFile('index.html');
indexFile.writeAsStringSync('<h1>Hello</h1>');
when(mockDeviceManager.deviceDiscoverers).thenReturn(<DeviceDiscovery>[]);
args.add('--machine');
await createTestCommandRunner(command).run(args);
expect(mockWebRunnerFactory._dartDefines, <String>['FOO=bar']);
}, overrides: <Type, Generator>{
DeviceManager: () => mockDeviceManager,
FeatureFlags: () => TestFeatureFlags(
isWebEnabled: true,
),
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
DeviceManager: () => mockDeviceManager,
FlutterVersion: () => mockStableFlutterVersion,
WebRunnerFactory: () => mockWebRunnerFactory,
});
}); });
}); });
} }
...@@ -276,6 +307,9 @@ class FakeDevice extends Fake implements Device { ...@@ -276,6 +307,9 @@ class FakeDevice extends Fake implements Device {
static const int kFailure = -1; static const int kFailure = -1;
TargetPlatform _targetPlatform = TargetPlatform.ios; TargetPlatform _targetPlatform = TargetPlatform.ios;
@override
String get id => 'fake_device';
void _throwToolExit(int code) => throwToolExit(null, exitCode: code); void _throwToolExit(int code) => throwToolExit(null, exitCode: code);
@override @override
...@@ -350,6 +384,9 @@ class MockWebRunnerFactory extends Mock implements WebRunnerFactory { ...@@ -350,6 +384,9 @@ class MockWebRunnerFactory extends Mock implements WebRunnerFactory {
} }
class MockWebRunner extends Mock implements ResidentRunner { class MockWebRunner extends Mock implements ResidentRunner {
@override
bool get debuggingEnabled => false;
@override @override
Future<int> run({ Future<int> run({
Completer<DebugConnectionInfo> connectionInfoCompleter, Completer<DebugConnectionInfo> connectionInfoCompleter,
...@@ -358,4 +395,7 @@ class MockWebRunner extends Mock implements ResidentRunner { ...@@ -358,4 +395,7 @@ class MockWebRunner extends Mock implements ResidentRunner {
}) async { }) async {
return 0; return 0;
} }
@override
Future<int> waitForAppToFinish() async => 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment