Skip to content

Fix markdown image hover preview for paths exceeding MAX_PATH on Windows#309363

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/markdown-image-hover-long-path-261880
Open

Fix markdown image hover preview for paths exceeding MAX_PATH on Windows#309363
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/markdown-image-hover-long-path-261880

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

What / Why

On Windows, the markdown image hover preview fails when a file's absolute path exceeds MAX_PATH (259 characters), even though the full preview panel renders the image correctly. The root cause is that the hover preview loads images through Electron's registerFileProtocol (the vscode-file:// handler in ProtocolMainService), and Electron's handler bottoms out in a Win32 API call that does not use the extended-length path prefix — so anything over MAX_PATH is rejected by the OS. This was diagnosed in the issue thread by @busorgin and tracked upstream at electron/electron#49101.

Fixes #261880

Fix

Before handing a resolved path to Electron's protocol callback, transform it on Windows to the extended-length form:

  • C:\... becomes \?\C:\...
  • \server\share\... becomes \?\UNC\server\share\...

This opts the underlying Win32 APIs into extended-length path mode, which bypasses the MAX_PATH limit. The transformation is:

  • Windows-only — no-op on macOS and Linux, where the limit does not apply.
  • Only applied past a conservative threshold (248 chars) so normal short paths are passed through unchanged and untouched behavior is preserved.
  • Idempotent — already-prefixed paths are returned as-is.
  • Applied only to the value handed to Electron. The validRoots / validExtensions allow-list checks continue to run against the original normalized path, so the security boundary around vscode-file:// is unchanged.

Testing

  • Manually verified on Windows 11 by creating a markdown file that references an image whose absolute path is ~290 characters. Before the fix, hovering the image link showed the broken-image placeholder. After the fix, the hover preview renders the image, matching the behavior of the full preview panel.
  • Short paths continue to work unchanged; the transformation is skipped for any path under the threshold.
  • UNC paths, drive-letter paths, and already-prefixed paths are each handled explicitly and covered by the guard branches in toWindowsLongPathIfNeeded.

Notes

…dows

On Windows, Electron's registerFileProtocol handler fails to load files
whose absolute path exceeds MAX_PATH (259). This breaks markdown image
hover previews (and any other vscode-file:// resource) for deeply
nested files, even though the preview rendering itself works because
it goes through a different code path.

Work around the underlying Electron/Win32 limitation
(electron/electron#49101) by prefixing paths with '\?\' (or
'\?\UNC\' for UNC paths) before handing them back to Electron's
protocol callback. The extended-length prefix is only applied on
Windows and only when the resolved path is close to or over the limit,
so normal short paths are unaffected.

The validRoots / validExtensions allow-list checks are still performed
against the normalized (unprefixed) path so the security boundary is
unchanged.

Fixes microsoft#261880
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.

Markdown Image Hover Preview broken for file paths exceeding 259

2 participants