|
1 | | -import { App, TFile } from "obsidian"; |
| 1 | +import { App, TFile, TFolder } from "obsidian"; |
2 | 2 | import { format } from "date-fns"; |
3 | 3 | import { escapeBody } from "./escapeUtils"; |
4 | 4 | import { NoticeManager } from "../notice-manager"; |
@@ -50,19 +50,54 @@ export class FileHelpers { |
50 | 50 | return; |
51 | 51 | } |
52 | 52 |
|
53 | | - const folder = this.app.vault.getAbstractFileByPath(path); |
54 | | - if (!folder) { |
55 | | - try { |
56 | | - await this.app.vault.createFolder(path); |
57 | | - this.noticeManager.debug(`Created folder: ${path}`); |
58 | | - } catch (error) { |
59 | | - // Folder may have been created concurrently or vault cache was stale - verify it exists now |
60 | | - const existsNow = this.app.vault.getAbstractFileByPath(path); |
61 | | - if (!existsNow) { |
62 | | - // Folder truly doesn't exist and creation failed - rethrow |
63 | | - throw error; |
| 53 | + // Normalize path separators to forward slashes for consistency |
| 54 | + const normalizedPath = path.replace(/\\/g, "/"); |
| 55 | + let existing = this.app.vault.getAbstractFileByPath(normalizedPath); |
| 56 | + |
| 57 | + // Check if folder already exists |
| 58 | + if (existing instanceof TFolder) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + try { |
| 65 | + await this.app.vault.createFolder(normalizedPath); |
| 66 | + this.noticeManager.debug(`Created folder: ${normalizedPath}`); |
| 67 | + } catch (error: unknown) { |
| 68 | + const errorMsg = error instanceof Error ? error.message : String(error); |
| 69 | + |
| 70 | + // Handle "Folder already exists" or other folder creation errors |
| 71 | + // Retry vault check with slight delay to allow cache to update |
| 72 | + const existsNow = this.app.vault.getAbstractFileByPath(normalizedPath); |
| 73 | + |
| 74 | + if (existsNow instanceof TFolder) { |
| 75 | + // Folder exists now, which is fine (concurrent creation or cache stale) |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if ( |
| 80 | + error instanceof Error && |
| 81 | + error.message.includes("Folder already exists") |
| 82 | + ) { |
| 83 | + // Expected case - folder was created successfully but Obsidian threw error anyway |
| 84 | + // This commonly happens when the vault cache is out of sync |
| 85 | + // Try one more time with a tiny delay to let cache update |
| 86 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 87 | + const retryCheck = this.app.vault.getAbstractFileByPath(normalizedPath); |
| 88 | + if (retryCheck instanceof TFolder) { |
| 89 | + this.noticeManager.debug( |
| 90 | + `Folder created successfully: ${normalizedPath}`, |
| 91 | + ); |
| 92 | + return; |
64 | 93 | } |
| 94 | + // Even though cache says it doesn't exist, the folder was likely created |
| 95 | + // Continue anyway - subsequent operations will work since the folder actually exists |
| 96 | + return; |
65 | 97 | } |
| 98 | + |
| 99 | + // Folder creation genuinely failed - rethrow |
| 100 | + throw error; |
66 | 101 | } |
67 | 102 | } |
68 | 103 |
|
|
0 commit comments