fix(editor): normalize the guest watch's disk read

The external guest's watch read disk raw while the session's text and
lastFlushedText came through readDocument's normalizing external
branch, so diskTextChanged saw a difference on every BOM/CRLF file and
raised conflictAlert on unchanged content — re-arming after each
sibling directory event and after either alert answer, blocking
departures and quit until the user's own flush rewrote the file
normalized (finding museion/external-watch-compares-unnormalized-text,
argus cycle-6; unticketed, brief-32).

Extract the external branch (BOM strip + CRLF->LF) into
EditorSession.readExternalDocument(at:) — one shared normalizer, two
callers: the open read (readDocument) and the watch's onEvent. The #67
compare is now normalized-to-normalized; what readDocument loads is
unchanged.
This commit is contained in:
AlexKucera committed 2026-11-30 22:17:13 +01:00
1 parent b69ddd3963
commit 70dd47ed8c
2 files changed
+23 -9

No files matched your search

+17 -6
View File
@@ -157,16 +157,27 @@ 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):
guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil } readExternalDocument(at: url)
if text.hasPrefix("\u{FEFF}") {
text.removeFirst()
}
return text.replacingOccurrences(of: "\r\n", with: "\n")
} }
} }
/// #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 }
if text.hasPrefix("\u{FEFF}") {
text.removeFirst()
}
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).
private func writeDocument() throws { private func writeDocument() throws {
+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()])