Yardlet博客

macOS 15 缺少一个 Logger 符号,导致我的 Mac 应用连续四天启动即闪退

连续四天里,Yardlet 在 macOS 15 上一启动就会闪退。SDK 标明 Logger.isEnabled(type:) 自 macOS 11 起可用,但 macOS 15.6.1 的 libswiftos 实际并不导出这个符号。

我开发了 Yardlet,一款免费开源的 Mac 应用,可以在同一个窗口里并行运行 Codex、Claude Code 等编程代理。产品说明写着支持 macOS 15 及以上版本。但在整整四天里,它在 macOS 15 上一启动就会闪退,而我对此一无所知。

我是怎么发现的

10 月 31 日,我正准备把公司的 MacBook 拿来做一次性能对比测试。那台 Mac 还停留在 macOS 15.6.1。结果 Yardlet 一打开就立刻退出,连窗口都没弹出来,只留下一份崩溃报告。我第一反应还以为是公司这台 Mac 太老了。

Crash report
Termination Reason: DYLD, Symbol missing
Symbol not found: _$s2os6LoggerV9isEnabled4typeSbSo0a5_log_E2_ta_tF
Expected in: /usr/lib/swift/libswiftos.dylib

缺失的符号是 os 模块里的 Logger.isEnabled(type:)。我只在代码里用过它一次,用来在没人查看调试日志时跳过拼接文本选区相关的调试信息。

我把公开发布的 0.11.0、0.11.4 和 0.11.5 的磁盘镜像都下载下来,用 nm -u 检查了一遍:三个版本全导入了这个符号。首个公开发布版 0.11.0 是在 10 月 27 日放出的,而我自己的 Mac 运行的是 macOS 27,所以一直没撞上这个问题。

为什么编译时没有报错

项目的部署目标(deployment target)设的是 macOS 15,所以我原本以为只要调用了更高版本的 API,编译器就会报可用性错误。但在 Xcode 27.1 的 SDK 里,Logger 标注的是自 macOS 11 起可用,而 isEnabled(type:) 本身没有单独的可用性标注(此处做了精简):

Swift
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
public struct Logger : @unchecked Sendable {
  // ...
  public func isEnabled(type: OSLogType) -> Bool

于是编译器就默认这个符号从 macOS 11 开始一直存在。但 Command Line Tools 自带的 macOS 15.5 SDK 的 libswiftos.tbd 里根本没有它,macOS 27 SDK 里才有,而 macOS 15.6.1 上的 libswiftos.dylib 也确实没有导出这个符号。macOS 26 我当时还没来得及测。

修复方法

OSLog 自身也有一个 isEnabled(type:),底层调用的是自 macOS 10.12 就存在的 C 函数 os_log_type_enabled。所以我改为保留一个 OSLog 句柄,再由它构造 Logger,需要判断时直接问这个句柄:

Swift
static let logHandle = OSLog(subsystem: bundleID, category: "conversation-selection")
static let log = Logger(logHandle)

guard logHandle.isEnabled(type: .debug) else { return }

排查其余所有导入符号

我不想下一次再以同样的方式踩坑。于是我在那台 macOS 15.6.1 的 Mac 上,列出了可执行文件里所有非弱链接(non-weak)的导入符号,并逐一比对目标系统库在当地实际导出的符号。下面是一份简略版脚本(未检查进程退出码):

check-imports.py
import re, subprocess, sys

def run(*args):
    return subprocess.run(args, capture_output=True, text=True).stdout

def short(path):
    name = re.sub(r"\.dylib$", "", path.split("/")[-1])
    return re.sub(r"(\.[A-Z0-9]+)+$", "", name)

binary = sys.argv[1]
wanted = {}
for line in run("dyld_info", "-imports", binary).splitlines():
    m = re.match(r"\s+0x[0-9A-F]+\s+(_\S+)\s+(\[weak-import\]\s+)?\(from (\S+)\)", line)
    if m and not m.group(2):
        wanted.setdefault(m.group(3), set()).add(m.group(1))

for line in run("dyld_info", "-dependents", binary).splitlines():
    path = line.strip().split()[-1] if line.strip() else ""
    if not path.startswith(("/usr/lib", "/System")) or short(path) not in wanted:
        continue
    libraries = [path] + [l.split()[-1] for l in run("dyld_info", "-dependents", path).splitlines() if "re-export" in l]
    exported = set()
    for library in libraries:
        exported |= set(re.findall(r"(_\S+)", run("dyld_info", "-exports", library)))
    for symbol in sorted(wanted[short(path)] - exported):
        print(short(path), run("xcrun", "swift-demangle", "--compact", symbol).strip())

脚本会跟踪直接 re-export 的库,因为 SwiftUI 在 macOS 15 上会 re-export SwiftUICore。用它扫描 0.11.5 的 4,261 个强导入符号,结果只打印出一行:libswiftos os.Logger.isEnabled(type: __C.os_log_type_t) -> Swift.Bool。

当天晚上我就发布了 0.11.6。发布前,我把公证后的磁盘镜像拷贝到那台 macOS 15.6.1 的 Mac 上验证了一遍:脚本检查了全部 4,291 个导入符号,没有任何输出;在空白主目录下启动应用也能稳定运行。

后续改动

现在每个版本发布前,都会先在那台 macOS 15 的 Mac 上跑一遍这个符号检查并实机启动。后续也计划补上一条在最低支持版本 macOS 上自动拉起应用的 CI 任务,目前尚未搭建。

以前我总觉得只要设好 deployment target,编译器就能替我兜底。其实编译器能替你守住的,只是 SDK 声明里的版本边界——而这一次,SDK 写的是 macOS 11。