预计阅读

命令行工具版本冲突导致 brew 管理失效





MacOS Tahoe 是一次 UI 上大改变的版本更新,流动的玻璃质感,我之前忍不住更新了一下。后来,通过系统设置检查更新,发现新的系统和CLT(命令行开发工具),又更新过系统,也更新过 CLT 。不知怎的,发现 brew 安装的 APP 不能用 brew 卸载,而且 brew 安装 app 的时候,有时候还出现错误。

最近,Apple 发布操作系统 26.4 和 CLT 26.4,我想升级一下,同时解决一下 brew 的问题。在升级完 MacOS 26.4 后,brew 还是有问题,不是系统的问题。我搜索了一下,发现问题的根源是 SDK 和编译器版本冲突。最简单的验证,是执行如下一行命令。

swift $(brew --repo)/Library/Homebrew/cask/utils/quarantine.swift
/Library/Developer/CommandLineTools/usr/include/swift/module.modulemap:13:8: error: redefinition of module 'SwiftBridging'
11 | //===----------------------------------------------------------------------===//
12 | 
13 | module SwiftBridging {
   |        `- error: redefinition of module 'SwiftBridging'
14 |     header "bridging"
15 | 

/Library/Developer/CommandLineTools/usr/include/swift/bridging.modulemap:13:8: note: previously defined here
11 | //===----------------------------------------------------------------------===//
12 | 
13 | module SwiftBridging {
   |        `- note: previously defined here
14 |     header "bridging"
15 | 

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Modules/Foundation.swiftmodule/arm64e-apple-macos.swiftinterface:7:8: error: failed to build module 'CoreFoundation'; this SDK is not supported by the compiler (the SDK is built with 'Apple Swift version 6.2 effective-5.10 (swiftlang-6.2.3.3.2 clang-1700.6.3.2)', while this compiler is 'Apple Swift version 6.2.4 effective-5.10 (swiftlang-6.2.4.1.4 clang-1700.6.4.2)'). Please select a toolchain which matches the SDK.
    5 | import Builtin
    6 | import Combine
    7 | import CoreFoundation
      |        `- error: failed to build module 'CoreFoundation'; this SDK is not supported by the compiler (the SDK is built with 'Apple Swift version 6.2 effective-5.10 (swiftlang-6.2.3.3.2 clang-1700.6.3.2)', while this compiler is 'Apple Swift version 6.2.4 effective-5.10 (swiftlang-6.2.4.1.4 clang-1700.6.4.2)'). Please select a toolchain which matches the SDK.
    8 | import Darwin
    9 | import Dispatch

/Library/Developer/CommandLineTools/usr/include/swift/module.modulemap: 0:
/Library/Developer/CommandLineTools/usr/include/swift/bridging.modulemap: 0:
/opt/homebrew/Library/Homebrew/cask/utils/quarantine.swift:3:8: error: failed to build module 'Foundation'; this SDK is not supported by the compiler (the SDK is built with 'Apple Swift version 6.2 effective-5.10 (swiftlang-6.2.3.3.2 clang-1700.6.3.2)', while this compiler is 'Apple Swift version 6.2.4 effective-5.10 (swiftlang-6.2.4.1.4 clang-1700.6.4.2)'). Please select a toolchain which matches the SDK.
 1 | #!/usr/bin/swift
 2 | 
 3 | import Foundation
   |        `- error: failed to build module 'Foundation'; this SDK is not supported by the compiler (the SDK is built with 'Apple Swift version 6.2 effective-5.10 (swiftlang-6.2.3.3.2 clang-1700.6.3.2)', while this compiler is 'Apple Swift version 6.2.4 effective-5.10 (swiftlang-6.2.4.1.4 clang-1700.6.4.2)'). Please select a toolchain which matches the SDK.
 4 | 
 5 | struct SwiftErr: TextOutputStream {

进入到 CLT 的安装目录,可以看到多个 SDK 版本,按照网上的办法,我删除已安装的 CLT 。

sudo rm -rf /Library/Developer/CommandLineTools/

进入 Developer 目录,会弹出来一个窗口,提示你安装 CLT ,我就直接在系统设置的检查更新里安装了最新的 CLT。

cd  /Library/Developer/

重新安装 CLT ,再执行 brew doctor

swift $(brew --repo)/Library/Homebrew/cask/utils/quarantine.swift
# 没有消息就是好消息
brew doctor
Your system is ready to brew.

问题解决。在应用程序目录下找到并删掉几个 brew 安装的 app,重新用 brew 安装。

MacOS 的软件包管理器 brew 是通过命令行的方式安装和卸载 App。brew 是依赖 CLT 的,如果 CLT 的 SDK 和编译器不匹配就会出问题。这次更新完,我的编译器版本升级到 21 了。

clang -v
Apple clang version 21.0.0 (clang-2100.0.123.102)
Target: arm64-apple-darwin25.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

这下 Swift 版本与 Clang 的版本配套了。

swift --version
swift-driver version: 1.148.6 Apple Swift version 6.3 (swiftlang-6.3.0.123.5 clang-2100.0.123.102)
Target: arm64-apple-macosx26.0

Homebrew 也更新到最新的版本 5.1.1 。

brew --version
Homebrew 5.1.1