Skip to content

fix(desktop): allow saving directly to a drive root (#5150) - #5188

Open
Panisnbb wants to merge 1 commit into
marktext:developfrom
Panisnbb:fix/desktop-save-drive-root
Open

fix(desktop): allow saving directly to a drive root (#5150)#5188
Panisnbb wants to merge 1 commit into
marktext:developfrom
Panisnbb:fix/desktop-save-drive-root

Conversation

@Panisnbb

Copy link
Copy Markdown

Fixes #5150

Problem

Saving a markdown file directly to a disk drive root (e.g. E:\ or D:\) on Windows fails and blocks the save entirely, as reported in #5150.

Root cause

writeFile() in packages/desktop/src/main/filesystem/index.ts unconditionally called ensureDir() on the target's parent directory before writing. fs-extra's ensureDir() maps to fs.mkdir(dir, { recursive: true }). On Windows, fs.mkdir() throws EPERM (not the usual EEXIST) when the path is an existing volume root such as E:\. Node handles a filesystem-root path specially, so an existing parent — a drive root included — failed the save with:

EPERM: operation not permitted, mkdir 'E:\'

This is independent of the drive's ACL / write permission: even a writable drive root fails, because the error occurs at the mkdir step before any file write is attempted. That is why it reproduced on every drive root on Windows.

Fix

Only call ensureDir() when the parent directory does not already exist:

const dir = path.dirname(pathname)
if (!isDirectory(dir)) {
  await ensureDir(dir)
}
  • An existing parent (drive roots included) is now a harmless no-op.
  • The autosave re-creates files and folders when they are moved away #3509 behaviour — recreating a moved/deleted parent folder on save, which is intentional and matches VS Code — is preserved, because isDirectory() returns false for a missing parent and ensureDir() still runs.
  • isDirectory() uses lstatSync (does not follow symlinks), so symlinked parents still fall through to ensureDir(), keeping prior behaviour unchanged.

Cross-platform safety

The change is platform-neutral and only uses lstatSync. On macOS/Linux an existing parent directory is likewise a no-op (recursive mkdir on an existing directory succeeds silently), so the only behavioural change is skipping a redundant — and on Windows, failing — call on a drive root. Symlink and deleted-parent handling are unchanged.

Verification

  • Reproduced the original failure with the exact fs-extra (11.3.5) and write-file-atomic (7.0.1) versions the desktop uses; confirmed EPERM: operation not permitted, mkdir 'E:\'.
  • Confirmed the fix writes successfully to E:\ directly, to an existing directory, to a recreated (deleted) parent, and to a normal new file.
  • pnpm typecheck passes; the changed file is lint-clean.
  • Existing write-file-missing-dir.spec.ts (autosave re-creates files and folders when they are moved away #3509, 2 tests) still passes, confirming the recreate-missing-parent behaviour is unchanged.

writeFile() unconditionally ensured the target's parent directory existed. On Windows, fs.mkdir() returns EPERM (not EEXIST) for an existing volume root (e.g. "E:\\"), so saving directly to a drive root failed with "operation not permitted".
@Panisnbb
Panisnbb force-pushed the fix/desktop-save-drive-root branch from 92f2960 to 5885fcf Compare August 26, 2026 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Can't save in Disk root dir

1 participant