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 <noreply@anthropic.com>
This commit is contained in:
Wannes Salomé
2026-02-17 00:24:30 +01:00
parent b1bba7685e
commit 48b0d22da6

View File

@@ -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);