platform/sdl3: chdir to .app bundle on macOS so CWD-relative assets resolve

A macOS .app launched with CWD=/ (Finder/open) could not find CWD-relative
assets (read_file_bytes("assets/...")) and crashed in stbtt with a null font.
SdlPlatform.init now chdirs to SDL_GetBasePath() when running from inside a
.app bundle (detected by ".app" in the base path), mirroring uikit.sx s iOS
chdir_to_bundle. Gated so the sx run dev flow (binary not bundled) keeps the
project CWD. Verified: direct-exec with CWD=/ now stays alive (was: instant
stbtt segfault). Filed issue 0051 with the analysis.

Note: launching via Finder/open additionally triggers Gatekeeper App
Translocation for the dev-signed bundle (separate code-signing concern, not
the asset path).
This commit is contained in:
agra
2026-05-30 01:10:13 +03:00
parent 3731a200c3
commit b31fbae757
2 changed files with 139 additions and 0 deletions

View File

@@ -11,6 +11,33 @@
g_sdl_plat : *SdlPlatform = null;
chdir :: (path: [*]u8) -> s32 #foreign;
SDL_GetBasePath :: () -> [*]u8 #foreign;
// A macOS `.app` launched via Finder / `open` starts with CWD=`/`, so a
// game's CWD-relative asset loads (`read_file_bytes("assets/...")`) miss and
// the app crashes loading fonts/textures. SDL's base path is the directory
// holding the executable — inside the `.app`, where the bundler copied
// `assets/`. chdir there, but ONLY when actually running from within a `.app`
// bundle, so the `sx run` dev flow (binary not bundled, assets in the project
// CWD) is left alone. Mirrors uikit.sx's iOS `chdir_to_bundle`.
sdl_chdir_to_bundle :: () {
inline if OS != .macos { return; }
bp := SDL_GetBasePath();
if bp == null { return; }
// Reorient only when the base path lives inside a `.app` bundle.
i : s64 = 0;
found := false;
while bp[i] != 0 {
if bp[i] == 46 and bp[i + 1] == 97 and bp[i + 2] == 112 and bp[i + 3] == 112 {
found = true;
break;
}
i = i + 1;
}
if found { chdir(bp); }
}
SdlPlatform :: struct {
window: *void = null;
gl_ctx: *void = null;
@@ -38,6 +65,8 @@ impl Platform for SdlPlatform {
self.delta_time = 0.008;
self.dpi_scale = 1.0;
SDL_Init(SDL_INIT_VIDEO);
// Find bundled assets when launched as a `.app` (CWD=/ under Finder).
sdl_chdir_to_bundle();
inline if OS == {
case .wasm: {