From 485509a273d40fcab7436ccd9e4bd36b398dab64 Mon Sep 17 00:00:00 2001 From: che cheng Date: Fri, 6 Nov 2026 16:59:24 +0800 Subject: [PATCH] fix: read a store marker with a BOM the same way on every macOS (#770) CI reached its tests for the first time and two failed, both passing locally. The reader relied on Foundation dropping a BOM, which the macOS 15 runner does not do, so a store opened on one machine was malformed on another. The reader now removes it. The offline regeneration test needs SwiftPM's global cache, which CI did not keep; ci.yml now caches it, under a key of its own. --- .github/workflows/ci.yml | 13 +++++++++---- Sources/AkashicStoreIO/StoreVersion.swift | 5 ++++- Tests/AkashicKitTests/StoreMarkerMatrixTests.swift | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2310c52e..10738185 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,12 +69,17 @@ jobs: - name: Swift 版本 run: swift --version - # .build 快取。key 綁 Package.resolved——依賴沒變就重用,變了就重建。 + # .build 與 SwiftPM 全域快取。key 綁 Package.resolved——依賴沒變就重用,變了就重建。 + # 全域快取也要存:`testUnicodeV1TrustChainAndOfficialConformanceArePinned` 以新的 scratch path 離線重建產生器, + # 依賴從這裡來。只存 .build 時快取命中就不會 fetch,全域快取是空的,那支測試連 swift-crypto 都拿不到(#770)。 + # key 與 census-parity.yml 分開:兩邊的 path 不同,共用 key 會互相擋住。 - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 with: - path: .build - key: ${{ runner.os }}-spm-${{ hashFiles('Package.resolved', 'Package.swift') }} - restore-keys: ${{ runner.os }}-spm- + path: | + .build + ~/Library/Caches/org.swift.swiftpm + key: ${{ runner.os }}-spm-global-${{ hashFiles('Package.resolved', 'Package.swift') }} + restore-keys: ${{ runner.os }}-spm-global- # `-warnings-as-errors`(#62)。擋的是「只有 warning、沒有測試失敗」這一類—— # attribute 靜默轉移的第二次事故(PR #55)正是這個形狀:`writePerson` 失去 diff --git a/Sources/AkashicStoreIO/StoreVersion.swift b/Sources/AkashicStoreIO/StoreVersion.swift index e3aeae50..a5232e88 100644 --- a/Sources/AkashicStoreIO/StoreVersion.swift +++ b/Sources/AkashicStoreIO/StoreVersion.swift @@ -276,7 +276,10 @@ public enum StoreVersion { /// 不得為了解析 marker 再回頭讀磁碟。 static func read(data: Data?, path: String) throws -> Int { guard let data else { return 1 } - guard let text = String(data: data, encoding: .utf8) else { + // BOM 由這裡去掉,不靠 Foundation:`String(data:encoding:)` 留不留 BOM 依系統版本而異(CI 的 macOS 15 留著、判 malformed, + // 本機的 macOS 27 吃掉、判 accept,#770)。同一個 store 不能因為換了一台機器就打不開。 + let body = data.starts(with: [0xEF, 0xBB, 0xBF]) ? data.dropFirst(3) : data + guard let text = String(data: body, encoding: .utf8) else { throw StoreVersionError.malformed(path: displaySafeInvisible(path, max: 300), line: "(標記檔不是 UTF-8)") } var found: Int? diff --git a/Tests/AkashicKitTests/StoreMarkerMatrixTests.swift b/Tests/AkashicKitTests/StoreMarkerMatrixTests.swift index 4766b5b5..3811590d 100644 --- a/Tests/AkashicKitTests/StoreMarkerMatrixTests.swift +++ b/Tests/AkashicKitTests/StoreMarkerMatrixTests.swift @@ -15,7 +15,7 @@ import Foundation /// 所以它同時是讀端 grammar 的一份可否證規格——特別是那些反直覺的格子: /// 全形/天城體/阿拉伯數字是 malformed(Swift 的 `Int(String)` 只吃 ASCII)、`#` 後緊跟 combining mark 或 ZWJ 時 /// 那一行**不是**註解(`hasPrefix("#")` 是 grapheme 層比較)、U+200B 被 `CharacterSet.whitespaces` trim 掉 -/// (Foundation 的空白集含 ZWSP,而 ZWSP 是 Cf 不是 Zs)、BOM 被 Foundation 吃掉所以是合法的。 +/// (Foundation 的空白集含 ZWSP,而 ZWSP 是 Cf 不是 Zs)、BOM 由讀端自己去掉所以是合法的(Foundation 留不留它依系統版本而異,#770)。 /// **這些行為取決於 Swift/Unicode 的版本**:Swift 或作業系統更新讓某一格的答案變了,這裡會紅——那是這個 /// 矩陣被留下來的理由(以前由 hash-table-drift 那條路徑守著「表過期是安靜的」,現在是這裡)。 ///