diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 9e5f1e50df..c33cd93dc9 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -264,11 +264,9 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi // TODO(stuartmorgan): Move internal channel registration from FlutterViewController to here. - // FlutterProjectArgs is expecting a full argv, so when processing it for - // flags the first item is treated as the executable and ignored. Add a dummy - // value so that all provided arguments are used. + // The first argument of argv is required to be the executable name. + std::vector argv = {[self.executableName UTF8String]}; std::vector switches = _project.switches; - std::vector argv = {"placeholder"}; std::transform(switches.begin(), switches.end(), std::back_inserter(argv), [](const std::string& arg) -> const char* { return arg.c_str(); }); @@ -517,6 +515,10 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi return _bridge; } +- (nonnull NSString*)executableName { + return [[[NSProcessInfo processInfo] arguments] firstObject] ?: @"Flutter"; +} + - (void)updateWindowMetrics { if (!_engine || !_viewController.viewLoaded) { return; diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm index feeb64f7f2..db5e1e09c1 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm @@ -39,6 +39,23 @@ TEST_F(FlutterEngineTest, CanLaunch) { EXPECT_TRUE(engine.running); } +TEST_F(FlutterEngineTest, HasNonNullExecutableName) { + // Launch the test entrypoint. + FlutterEngine* engine = GetFlutterEngine(); + std::string executable_name = [[engine executableName] UTF8String]; + EXPECT_TRUE([engine runWithEntrypoint:@"executableNameNotNull"]); + + // Block until notified by the Dart test of the value of Platform.executable. + fml::AutoResetWaitableEvent latch; + AddNativeCallback("NotifyStringValue", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { + const auto dart_string = tonic::DartConverter::FromDart( + Dart_GetNativeArgument(args, 0)); + EXPECT_EQ(executable_name, dart_string); + latch.Signal(); + })); + latch.Wait(); +} + TEST_F(FlutterEngineTest, MessengerSend) { FlutterEngine* engine = GetFlutterEngine(); EXPECT_TRUE([engine runWithEntrypoint:@"main"]); diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h index 0629ba3abc..89c05e91ae 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h @@ -39,6 +39,11 @@ */ @property(nonatomic) BOOL semanticsEnabled; +/** + * The executable name for the current process. + */ +@property(nonatomic, readonly, nonnull) NSString* executableName; + /** * Informs the engine that the associated view controller's view size has changed. */ diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart index 6621c3fd88..dfb2e2ff9c 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:io'; import 'dart:ui'; void signalNativeTest() native 'SignalNativeTest'; @@ -9,6 +10,17 @@ void signalNativeTest() native 'SignalNativeTest'; void main() { } +/// Notifies the test of a string value. +/// +/// This is used to notify the native side of the test of a string value from +/// the Dart fixture under test. +void notifyStringValue(String s) native 'NotifyStringValue'; + +@pragma('vm:entry-point') +void executableNameNotNull() { + notifyStringValue(Platform.executable); +} + @pragma('vm:entry-point') void canLogToStdout() { // Emit hello world message to output then signal the test.