From 48b0d22da6d4e0a48e0f0a270b516545fbfa47d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wannes=20Salom=C3=A9?= Date: Tue, 17 Feb 2026 00:24:30 +0100 Subject: [PATCH] fix: handle "File already exists" for .md files in writeFileAuto During concurrent initialisation (UPDATE STORAGE runs up to 10 ops in parallel), getAbstractFileByPath can return null for .md files whose vault index entry hasn't been populated yet, even though the file already exists on disk. This causes vault.create() to throw "File already exists." The same root cause (stale in-memory index) was already identified for non-.md files (see comment above) and handled via adapterWrite. Extend that workaround to .md files by catching the "File already exists" error and falling back to adapterWrite, consistent with the existing approach. Co-Authored-By: Claude Sonnet 4.5 --- .../coreObsidian/ModuleFileAccessObsidian.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/modules/coreObsidian/ModuleFileAccessObsidian.ts b/src/modules/coreObsidian/ModuleFileAccessObsidian.ts index a7e58ed..935754d 100644 --- a/src/modules/coreObsidian/ModuleFileAccessObsidian.ts +++ b/src/modules/coreObsidian/ModuleFileAccessObsidian.ts @@ -108,7 +108,22 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements // For safety, check existence return await this.vaultAccess.adapterExists(path); } else { - return (await this.vaultAccess.vaultCreate(path, data, opt)) instanceof TFile; + // The same stale-index issue described above can also happen for .md files during + // concurrent initialisation (UPDATE STORAGE runs up to 10 ops in parallel). + // getAbstractFileByPath returns null because Obsidian's in-memory index hasn't + // caught up yet, but the file already exists on disk — causing vault.create() to + // throw "File already exists." + // Fall back to adapterWrite (same approach used for non-md files above) so the + // file is written correctly without an error. + try { + return (await this.vaultAccess.vaultCreate(path, data, opt)) instanceof TFile; + } catch (ex) { + if (ex instanceof Error && ex.message === "File already exists.") { + await this.vaultAccess.adapterWrite(path, data, opt); + return await this.vaultAccess.adapterExists(path); + } + throw ex; + } } } else { this._log(`Could not write file (Possibly already exists as a folder): ${path}`, LOG_LEVEL_VERBOSE);