Skip to content

Commit b5afa98

Browse files
authored
Pass --web-define through to web runner when using --machine mode (flutter#183228)
This passes `--web-define`s through to the machine daemon used when `flutter run` is invoked with `--machine` (which is the case for IDEs). Fixes flutter#183170 Fixes Dart-Code/Dart-Code#5942 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 6ec6dd1 commit b5afa98

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

packages/flutter_tools/lib/src/commands/daemon.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ class AppDomain extends Domain {
665665
String? route,
666666
DebuggingOptions options,
667667
bool enableHotReload, {
668+
Map<String, String> webDefines = const <String, String>{},
668669
File? applicationBinary,
669670
required bool trackWidgetCreation,
670671
String? projectRootPath,
@@ -712,6 +713,7 @@ class AppDomain extends Domain {
712713
platform: globals.platform,
713714
outputPreferences: globals.outputPreferences,
714715
fileSystem: globals.fs,
716+
webDefines: webDefines,
715717
);
716718
} else if (enableHotReload) {
717719
runner = HotRunner(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ class RunCommand extends RunCommandBase {
841841
route,
842842
debuggingOptions,
843843
hotMode,
844+
webDefines: extractWebDefines(),
844845
applicationBinary: applicationBinaryPath == null
845846
? null
846847
: globals.fs.file(applicationBinaryPath),

packages/flutter_tools/test/commands.shard/hermetic/run_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,50 @@ void main() {
530530
},
531531
);
532532

533+
group('web', () {
534+
late FakeWebRunnerFactory fakeWebRunnerFactory;
535+
536+
setUp(() {
537+
fakeWebRunnerFactory = FakeWebRunnerFactory();
538+
});
539+
540+
testUsingContext(
541+
'can pass --web-define',
542+
() async {
543+
final command = RunCommand();
544+
final device = FakeDevice(
545+
platformType: PlatformType.web,
546+
targetPlatform: TargetPlatform.web_javascript,
547+
);
548+
testDeviceManager.devices = <Device>[device];
549+
550+
await expectLater(
551+
() => createTestCommandRunner(command).run(<String>[
552+
'run',
553+
'--no-pub',
554+
'--machine',
555+
'-d',
556+
device.id,
557+
'--web-define=NAME=VAL',
558+
]),
559+
throwsToolExit(),
560+
);
561+
expect(fakeWebRunnerFactory.lastWebDefines, <String, String>{'NAME': 'VAL'});
562+
},
563+
overrides: <Type, Generator>{
564+
Artifacts: () => artifacts,
565+
Cache: () => Cache.test(processManager: FakeProcessManager.any()),
566+
DeviceManager: () => testDeviceManager,
567+
FeatureFlags: () => FakeFeatureFlags(),
568+
FileSystem: () => fs,
569+
ProcessManager: () => FakeProcessManager.any(),
570+
Stdio: () => FakeStdio(),
571+
Logger: () => MachineOutputLogger(parent: logger),
572+
WebRunnerFactory: () => fakeWebRunnerFactory,
573+
},
574+
);
575+
});
576+
533577
testUsingContext(
534578
'can disable devtools with --no-devtools',
535579
() async {
@@ -1990,6 +2034,11 @@ class FakeResidentRunner extends Fake implements ResidentRunner {
19902034
}
19912035
return 0;
19922036
}
2037+
2038+
@override
2039+
DebuggingOptions get debuggingOptions {
2040+
throwToolExit('');
2041+
}
19932042
}
19942043

19952044
class DaemonCapturingRunCommand extends RunCommand {
@@ -2019,6 +2068,7 @@ class CapturingAppDomain extends AppDomain {
20192068
String? route,
20202069
DebuggingOptions options,
20212070
bool enableHotReload, {
2071+
Map<String, String> webDefines = const <String, String>{},
20222072
File? applicationBinary,
20232073
required bool trackWidgetCreation,
20242074
String? projectRootPath,
@@ -2074,6 +2124,7 @@ class FakeFeatureFlags extends Fake implements FeatureFlags {
20742124
/// A Fake WebRunnerFactory that CAPTURES the debugging options passed to it.
20752125
class FakeWebRunnerFactory extends Fake implements WebRunnerFactory {
20762126
DebuggingOptions? lastOptions;
2127+
Map<String, String>? lastWebDefines;
20772128

20782129
@override
20792130
ResidentRunner createWebRunner(
@@ -2094,6 +2145,7 @@ class FakeWebRunnerFactory extends Fake implements WebRunnerFactory {
20942145
Map<String, String> webDefines = const <String, String>{},
20952146
}) {
20962147
lastOptions = debuggingOptions;
2148+
lastWebDefines = webDefines;
20972149
return FakeResidentRunner();
20982150
}
20992151
}

0 commit comments

Comments
 (0)