museion/external-watch-compares-unnormalized-text: guest watch reads through the ONE shared external normalizer — false #67 conflict gone #203

Merged
AlexKucera merged 4 commits from fix/external-watch-compares-unnormalized-text into main 2026-12-03 07:36:53 +01:00
2 changed files with 19 additions and 5 deletions
Showing only changes of commit 70dd47ed8c - Show all commits

No files matched your search

+13 -2
View File
@@ -157,15 +157,26 @@ final class EditorSession {
private static func readDocument(_ target: SessionTarget) -> String? { private static func readDocument(_ target: SessionTarget) -> String? {
switch target { switch target {
case let .post(id, home): case let .post(id, home):
return PostStore.readBody(id: id, home: home) PostStore.readBody(id: id, home: home)
case let .externalFile(url): case let .externalFile(url):
readExternalDocument(at: url)
}
}
/// #67/brief-32: the ONE external-target disk read — raw UTF-8, BOM
/// stripped, CRLF normalized to LF. Every reader of an external file's
/// bytes goes through here (the open read above, the guest watch in
/// PostsModelImports, Yes-reload, keep-local), so the #67 compare is
/// always normalized-to-normalized: the watch used to read raw and
/// raised a false conflict on every directory event over an unchanged
/// BOM/CRLF file.
static func readExternalDocument(at url: URL) -> String? {
guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil } guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil }
if text.hasPrefix("\u{FEFF}") { if text.hasPrefix("\u{FEFF}") {
text.removeFirst() text.removeFirst()
} }
return text.replacingOccurrences(of: "\r\n", with: "\n") return text.replacingOccurrences(of: "\r\n", with: "\n")
} }
}
/// Target-aware write: posts → `PostStore.overwrite` (+ sidecar); /// Target-aware write: posts → `PostStore.overwrite` (+ sidecar);
/// external → the whole text, in place, no sidecar (#93). /// external → the whole text, in place, no sidecar (#93).
+6 -3
View File
@@ -62,11 +62,14 @@ extension PostsModel {
} }
/// #93: watch the open external file — its rescan is the #67 disk-change /// #93: watch the open external file — its rescan is the #67 disk-change
/// check, reused verbatim for foreign files. PostsWatcher fires on the /// check, reused verbatim for foreign files. The disk read goes through
/// main actor already; no re-hop needed. /// the shared external normalizer (brief-32): the session's text is
/// normalized on open, so a raw read raised a false conflict on every
/// directory event over an unchanged BOM/CRLF file. PostsWatcher fires
/// on the main actor already; no re-hop needed.
private func startExternalWatch(url: URL) { private func startExternalWatch(url: URL) {
externalWatcher.onEvent = { [weak self] in externalWatcher.onEvent = { [weak self] in
guard let self, let session = externalSession, let disk = try? String(contentsOf: url, encoding: .utf8) else { return } guard let self, let session = externalSession, let disk = EditorSession.readExternalDocument(at: url) else { return }
session.diskDidChange(diskText: disk) session.diskDidChange(diskText: disk)
} }
externalWatcher.start(urls: [url.deletingLastPathComponent()]) externalWatcher.start(urls: [url.deletingLastPathComponent()])