Skip to content

fix: handle UNC image paths and watchers - #4778

Merged
Jocs merged 2 commits into
marktext:developfrom
Renakoni:investigate/issues-4577-4563-unc-paths
Sep 3, 2026
Merged

Jocs merged 2 commits into
marktext:developfrom
Renakoni:investigate/issues-4577-4563-unc-paths

Conversation

@Renakoni

Copy link
Copy Markdown
Contributor

Closes #4563

Summary

Fix UNC path handling for local images and project-tree watching.

Relative images opened from UNC-backed documents could lose the UNC host during path resolution, producing invalid URLs such as file:///wsl.localhost/... instead of host-based file URLs such as file://wsl.localhost/.... This PR normalizes local image paths into valid file:// URLs for UNC paths, Windows drive paths, and POSIX paths.

UNC roots also need polling for reliable directory watching. On WSL UNC paths, chokidar can fail to watch or enumerate files cleanly without polling, which can leave the project tree empty or produce watcher errors. This PR detects UNC roots and forces usePolling: true for those watchers.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added for UNC file URL normalization, WSL UNC relative image paths, and UNC watcher polling.
  • Manually tested on: Windows with a real WSL UNC path.

The following checks passed:

  • pnpm --filter @muyajs/core exec vitest run src/utils/__tests__/image.spec.ts
  • pnpm --filter marktext exec vitest run test/unit/specs/printService-image.spec.ts test/unit/specs/exportHtml.spec.ts test/unit/specs/watcher-await-write-finish.spec.ts
  • pnpm --filter @muyajs/core lint:types
  • pnpm --filter marktext typecheck
  • pnpm --filter @muyajs/core exec vitest run --testTimeout 30000
  • git diff --check

Manual WSL UNC verification:

  • Created a real WSL-backed test folder at \\wsl.localhost\HermesUbuntu\tmp\marktext-unc-real-test.
  • Placed doc-relative.md and img/desktop-image.png inside that WSL filesystem.
  • Opened the markdown file through the Windows UNC path.
  • Confirmed the relative image ![desktop image](img/desktop-image.png) renders correctly.
  • Confirmed opening the WSL UNC folder shows the project contents instead of an empty project.

Real SMB / Windows network share verification was not performed because no actual \\server\share environment was available. The same UNC-path fix is expected to address #4577 as well, and the ordinary UNC share case is covered by the same detection and file URL normalization logic with unit tests for both \\server\share\... and //server/share/... path forms. However, because #4577 was not verified against a real SMB share, this PR only closes #4563.

Notes for reviewers [optional]

The core issue is not WSL-specific. WSL UNC paths and ordinary SMB share paths both use the same UNC shape:

\\host\share\...
//host/share/...

The image fix preserves the host/share portion when resolving local image paths and converts local paths to valid file URLs:

\\host\share\path  -> file://host/share/path
C:\path\file.png   -> file:///C:/path/file.png
/tmp/file.png      -> file:///tmp/file.png

For watcher behavior, UNC roots force polling because native watching on WSL UNC paths was unreliable during verification. A direct chokidar probe on the WSL UNC test folder showed non-polling watchers emitted EISDIR errors, while polling discovered the same files without watcher errors.


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Copilot AI review requested due to automatic review settings June 28, 2026 10:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Jocs
Jocs merged commit 1e5ccd7 into marktext:develop Sep 3, 2026
16 checks passed
@hui6900

hui6900 commented Sep 13, 2026

Copy link
Copy Markdown

Following up on this PR — it looks like it introduced a regression on real SMB shares.

On 0.20.0-rc.2, saving a file opened from \\server\share\... fails with EPERM at the
rename step of the atomic save. Confirmed A/B on the same file, machine and preferences:
rc.2 failed on every save, while rc.1 (after downgrading) saved fine every time.

Since this PR makes usePolling unconditionally true for UNC paths,
watcherUsePolling: false no longer has any effect for them.

Full details, logs and what I ruled out: #5322

(中文)本 PR 在真实 SMB 共享上引入了回归:rc.2 保存 \\server\share\... 上的文件时,原子
保存的 rename 步骤报 EPERM;同一文件/机器/配置降级到 rc.1 后完全正常。由于本 PR 对 UNC 路径
无条件强制 usePolling: true,watcherUsePolling: false 已失效。详见 #5322。

🤖 Investigated with Claude Code

Jocs added a commit that referenced this pull request Sep 23, 2026
…5520)

* fix(desktop): retry a save whose rename an SMB share refused (#5322)

On a Windows SMB share, MoveFileEx(REPLACE_EXISTING) — what fs.rename
compiles to, and the last step of the atomic save — is refused with
ACCESS_DENIED, surfaced as EPERM, while any handle is open on the target.
MarkText's own file watcher stats the open document, so a save races its
own watcher: the temp file is written, the rename over the document is
rejected, and the edit is lost.

Measured on a real Windows SMB share (loopback share, windows-latest):

  one open read handle held across every save   50/50 renames refused
  watcher stat storm at 1ms (~12000 stats)     102/300 refused
  watcher polling as the app configures it       1/200 refused
  no handle open at all                          0/50  refused

The same probes against Samba from a Linux client never fail, so this is
the Windows SMB redirector's share-mode check, not a server-side one.

This is a 0.19 -> 0.20 regression, and not from #4778 as the issue
assumed. 0.19.x wrote the document in place with fs-extra's outputFile
and never renamed, so it could not hit this. #4852 introduced the
temp+rename atomic save (for #3786/#3828), which is what races; #4778
then forced usePolling for UNC paths, which raised the stat rate enough
to turn an occasional lost save into a reliable one. Reverting #4778
would only push the failure back below the reporter's notice — it would
not fix it, and it would leave mapped network drives (Z:\, not a UNC
path, so never force-polled) failing occasionally either way.

The handle is released within milliseconds, so back off and try the save
again instead of dropping the edit. Retrying re-runs the whole atomic
write, which is safe: write-file-atomic cleans up its temp file on
failure. Three retries at 50/150/300ms take the 1ms-stat-storm failure
rate from 102/300 to 3/300; at the app's real stat rate the remaining
window is far smaller still. A process that holds a handle open
continuously (an antivirus scanner, a sync client) is out of reach of
any retry — that save still reports mt::tab-save-failure and the tab
stays dirty, rather than silently losing the edit.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* docs: correct what the rename refusal actually depends on (#5322)

A follow-up probe showed the earlier explanation was wrong on two counts:
MoveFileEx(REPLACE_EXISTING) is refused for an open target on local NTFS
just as on a share (50/50 either way), and asking for FILE_SHARE_DELETE
makes no difference (err=5 with and without it). What separates the two
is how long the handle stays open - microseconds for a local stat, a
network round trip for one over SMB - so only shares lose the race in
practice. The retry itself is unchanged; only the reasoning it records.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* fix(desktop): pick retry delays that land on a different phase (#5322)

The watcher stats on a fixed period, so a retry only helps if its
cumulative offset lands on a phase the stat is not holding. 50/150/300
accumulates to 50/200/500 - the last two are multiples of chokidar's
100ms interval, i.e. exactly the phase that just failed, so only the
first retry was ever doing anything. Simulated against both periods the
app uses (100ms watchFile, 150ms awaitWriteFinish), that loses 33/57/75%
of first failures once a stat holds the file for 60/70/80ms.

50/75/50 accumulates to 50/125/175 - three distinct phases - and clears
every first failure up to a ~70ms stat, while finishing in 175ms instead
of 500ms.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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] WSL : Failed to load resource: net::ERR_FILE_NOT_FOUND

4 participants