godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.72k stars 20.12k forks source link

Second instances of running exported macOS projects do not get opened file paths in launch arguments #94788

Open lostminds opened 1 month ago

lostminds commented 1 month ago

Tested versions

Reproducible in 4.3bX and 4.3rc1

System information

macOS 14.5

Issue description

When launching an exported Godot project by opening a file in the OS (or dragging it onto the Dock/taskbar icon of the application) you can access this file path as the first launch argument in OS.get_cmdline_args(). There is currently no other way to get access to files passed from the OS as discussed in https://github.com/godotengine/godot-proposals/issues/10195 . If the process is already running when the file is sent from the OS, the current implementation is to start a new duplicate process and pass the file path as a launch argument for that.

This works as expected on Windows, but not on macOS:

Steps to reproduce

If you want to be more advanced you can add something like this:

<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSItemContentTypes</key>
<array>
<string>public.text</string>
</array>
</dict>
</array>

In the Additional Plist Content field of your macOS export, which should enable your exported application to open .txt files from macOS with it once it's installed.

Minimal reproduction project (MRP)

Since this is related to exported builds and exporting (at least successfully) to macOS requires inputting personal codesigning details I don't want to submit a MRP. But the steps to reproduce above should hopefully be minimal enough to be easy to reproduce.

lostminds commented 1 month ago

While testing this further I discovered that the Godot Engine Editor has the same behavior of starting a separate process for additional .godot project files opened on macOS. However, in this case the second project file is opened as expected, so the editor must be getting the correct launch argument or accessing the sent file in some other way specific to the editor. So perhaps looking at how the editor handles secondary processes being launched as compared to other exported godot project could be useful.

lostminds commented 1 month ago

This seems to be a macOS sandboxing issue. If you export the project with sandboxing off the secondary processes get the expected launch argument.

bruvzg commented 1 month ago

This seems to be a macOS sandboxing issue.

Sandboxed apps can't set command line arguments when starting a new instance. Current logic was implemented for the editor (which is not running in the sandbox and designed to run in multiple instances).

https://github.com/godotengine/godot/blob/88d9325065a3e00e9e168ffad4ff93e12455a357/platform/macos/godot_application_delegate.mm#L155-L189

We probably should add an option to override this behavior in exported projects and instead of starting new instances pass filenames to a notification or user defined callback function.