From 57af5f31067dedd8aff07cb576b30044da14e22f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Jan 2025 11:50:12 -0800 Subject: [PATCH] crash: prefer XDG cache dir if available --- src/crash/sentry.zig | 21 +++++++++++++++++---- src/os/macos.zig | 20 +++++++++++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/crash/sentry.zig b/src/crash/sentry.zig index f381a8840..9e05b427d 100644 --- a/src/crash/sentry.zig +++ b/src/crash/sentry.zig @@ -101,10 +101,23 @@ fn initThread(gpa: Allocator) !void { sentry.c.sentry_options_set_before_send(opts, beforeSend, null); // Determine the Sentry cache directory. - const cache_dir = if (builtin.os.tag == .macos) - try internal_os.macos.cacheDir(alloc, "sentry") - else - try internal_os.xdg.cache(alloc, .{ .subdir = "ghostty/sentry" }); + const cache_dir = cache_dir: { + // On macOS, we prefer to use the NSCachesDirectory value to be + // a more idiomatic macOS application. But if XDG env vars are set + // we will respect them. + if (comptime builtin.os.tag == .macos) macos: { + if (std.posix.getenv("XDG_CACHE_HOME") != null) break :macos; + break :cache_dir try internal_os.macos.cacheDir( + alloc, + "sentry", + ); + } + + break :cache_dir try internal_os.xdg.cache( + alloc, + .{ .subdir = "ghostty/sentry" }, + ); + }; sentry.c.sentry_options_set_database_path_n( opts, cache_dir.ptr, diff --git a/src/os/macos.zig b/src/os/macos.zig index 918dde9af..a956d25e2 100644 --- a/src/os/macos.zig +++ b/src/os/macos.zig @@ -25,10 +25,11 @@ pub fn appSupportDir( alloc: Allocator, sub_path: []const u8, ) AppSupportDirError![]const u8 { - return try makeCommonPath(alloc, .NSApplicationSupportDirectory, &.{ - build_config.bundle_id, - sub_path, - }); + return try commonDir( + alloc, + .NSApplicationSupportDirectory, + &.{ build_config.bundle_id, sub_path }, + ); } pub const CacheDirError = Allocator.Error || error{AppleAPIFailed}; @@ -39,10 +40,11 @@ pub fn cacheDir( alloc: Allocator, sub_path: []const u8, ) CacheDirError![]const u8 { - return try makeCommonPath(alloc, .NSCachesDirectory, &.{ - build_config.bundle_id, - sub_path, - }); + return try commonDir( + alloc, + .NSCachesDirectory, + &.{ build_config.bundle_id, sub_path }, + ); } pub const SetQosClassError = error{ @@ -101,7 +103,7 @@ pub const NSSearchPathDomainMask = enum(c_ulong) { NSUserDomainMask = 1, }; -fn makeCommonPath( +fn commonDir( alloc: Allocator, directory: NSSearchPathDirectory, sub_paths: []const []const u8,