From aaae3275636cc1c9eca42dbfe73790c601e97a44 Mon Sep 17 00:00:00 2001 From: Roshan Singh Date: Wed, 14 Jan 2026 16:06:26 +0000 Subject: [PATCH] macOS: ensure /tmp/clawdbot exists for launchd logs --- apps/macos/Sources/Clawdbot/LogLocator.swift | 11 ++++++++-- .../ClawdbotIPCTests/LogLocatorTests.swift | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 apps/macos/Tests/ClawdbotIPCTests/LogLocatorTests.swift diff --git a/apps/macos/Sources/Clawdbot/LogLocator.swift b/apps/macos/Sources/Clawdbot/LogLocator.swift index b49e58f9c..1444223ea 100644 --- a/apps/macos/Sources/Clawdbot/LogLocator.swift +++ b/apps/macos/Sources/Clawdbot/LogLocator.swift @@ -5,12 +5,17 @@ enum LogLocator { private static let stdoutLog = logDir.appendingPathComponent("clawdbot-stdout.log") private static let gatewayLog = logDir.appendingPathComponent("clawdbot-gateway.log") + private static func ensureLogDirExists() { + try? FileManager.default.createDirectory(at: self.logDir, withIntermediateDirectories: true) + } + private static func modificationDate(for url: URL) -> Date { (try? url.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate) ?? .distantPast } /// Returns the newest log file under /tmp/clawdbot/ (rolling or stdout), or nil if none exist. static func bestLogFile() -> URL? { + self.ensureLogDirExists() let fm = FileManager.default let files = (try? fm.contentsOfDirectory( at: self.logDir, @@ -26,11 +31,13 @@ enum LogLocator { /// Path to use for launchd stdout/err. static var launchdLogPath: String { - stdoutLog.path + self.ensureLogDirExists() + return stdoutLog.path } /// Path to use for the Gateway launchd job stdout/err. static var launchdGatewayLogPath: String { - gatewayLog.path + self.ensureLogDirExists() + return gatewayLog.path } } diff --git a/apps/macos/Tests/ClawdbotIPCTests/LogLocatorTests.swift b/apps/macos/Tests/ClawdbotIPCTests/LogLocatorTests.swift new file mode 100644 index 000000000..61db98c93 --- /dev/null +++ b/apps/macos/Tests/ClawdbotIPCTests/LogLocatorTests.swift @@ -0,0 +1,21 @@ +import Foundation +import Testing +@testable import Clawdbot + +@Suite struct LogLocatorTests { + @Test func launchdGatewayLogPathEnsuresTmpDirExists() throws { + let dirPath = "/tmp/clawdbot" + let fm = FileManager.default + + // Simulate a clean machine state where /tmp/clawdbot does not exist. + if fm.fileExists(atPath: dirPath) { + try? fm.removeItem(atPath: dirPath) + } + + _ = LogLocator.launchdGatewayLogPath + + var isDir: ObjCBool = false + #expect(fm.fileExists(atPath: dirPath, isDirectory: &isDir)) + #expect(isDir.boolValue == true) + } +}