From 45f9f37d0f2ec4fb75a291218b0d986a97cd75f9 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Wed, 14 May 2025 22:14:34 +0100 Subject: [PATCH] Make startup deletion check more robust --- .../src/obsidian-file-system.ts | 22 ++++++++++++++++++- .../sync-client/src/sync-operations/syncer.ts | 14 +++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/frontend/obsidian-plugin/src/obsidian-file-system.ts b/frontend/obsidian-plugin/src/obsidian-file-system.ts index 2cccacd3..1a4d7099 100644 --- a/frontend/obsidian-plugin/src/obsidian-file-system.ts +++ b/frontend/obsidian-plugin/src/obsidian-file-system.ts @@ -15,7 +15,27 @@ export class ObsidianFileSystemOperations implements FileSystemOperations { ) {} public async listAllFiles(): Promise { - return this.vault.getFiles().map((file) => file.path); + // Let's implement this by hand because vault.adapter.listAllFiles doesn't always return all files. + const allFiles = []; + const remainingFolders = [this.vault.getRoot().path]; + + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + while (true) { + const folder = remainingFolders.pop(); + if (folder == undefined) { + break; + } + + if (folder.includes(".obsidian")) { + continue; + } + + const files = await this.vault.adapter.list(normalizePath(folder)); + allFiles.push(...files.files); + remainingFolders.push(...files.folders); + } + + return allFiles; } public async read(path: RelativePath): Promise { diff --git a/frontend/sync-client/src/sync-operations/syncer.ts b/frontend/sync-client/src/sync-operations/syncer.ts index 5aafb08c..ca608314 100644 --- a/frontend/sync-client/src/sync-operations/syncer.ts +++ b/frontend/sync-client/src/sync-operations/syncer.ts @@ -1,6 +1,7 @@ import type { Database, DocumentId, + DocumentRecord, RelativePath } from "../persistence/database"; import type { SyncService } from "../services/sync-service"; @@ -434,9 +435,16 @@ export class Syncer { const allLocalFiles = await this.operations.listAllFiles(); - let locallyPossiblyDeletedFiles = [ - ...this.database.resolvedDocuments - ].filter(({ relativePath }) => !allLocalFiles.includes(relativePath)); + let locallyPossiblyDeletedFiles: DocumentRecord[] = []; + + for (const document of this.database.resolvedDocuments) { + if ( + !document.isDeleted && + !(await this.operations.exists(document.relativePath)) + ) { + locallyPossiblyDeletedFiles.push(document); + } + } const updates = Promise.all( allLocalFiles.map(async (relativePath) => {