Skip to content

Commit

Permalink
[macOS] Set correct Platform.executable (argv[0]) (flutter#33128)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbracken authored May 8, 2022
1 parent b89e093 commit 2d1f34c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions shell/platform/darwin/macos/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,9 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {

// 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<const char*> argv = {[self.executableName UTF8String]};
std::vector<std::string> switches = _project.switches;
std::vector<const char*> argv = {"placeholder"};
std::transform(switches.begin(), switches.end(), std::back_inserter(argv),
[](const std::string& arg) -> const char* { return arg.c_str(); });

Expand Down Expand Up @@ -517,6 +515,10 @@ - (void)updateDisplayConfig {
return _bridge;
}

- (nonnull NSString*)executableName {
return [[[NSProcessInfo processInfo] arguments] firstObject] ?: @"Flutter";
}

- (void)updateWindowMetrics {
if (!_engine || !_viewController.viewLoaded) {
return;
Expand Down
17 changes: 17 additions & 0 deletions shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ @interface FlutterEngine (Test)
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<std::string>::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"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@
// 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';

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.
Expand Down

0 comments on commit 2d1f34c

Please sign in to comment.