Skip to content

Preserve the leading slashes of Windows UNC paths - #17

Open
Eljees wants to merge 1 commit into
jonschlinkert:masterfrom
Eljees:unc-preserve-leading-slashes
Open

Preserve the leading slashes of Windows UNC paths#17
Eljees wants to merge 1 commit into
jonschlinkert:masterfrom
Eljees:unc-preserve-leading-slashes

Conversation

@Eljees

@Eljees Eljees commented Aug 27, 2026

Copy link
Copy Markdown

Problem

A plain Windows UNC path loses its leading double slash:

normalize('\\\\server\\share\\file.css');
// actual:   '/server/share/file.css'
// expected: '//server/share/file.css'

The leading \\ is treated as a repeated separator and collapsed, so the
result no longer refers to a network path. The Win32 namespace prefixes are
already special-cased (\\?\C:\... -> //?/C:/..., \\.\CdRomX ->
//./CdRomX); a plain UNC path is the one Windows form that loses its
doubled leading slash.

Change

After the existing namespace-prefix handling, keep the // prefix when the
input starts with exactly two backslashes followed by a path character. Two
notes on scope:

  • Forward-slash inputs (//foo/bar) are intentionally NOT changed and still
    collapse to /foo/bar, exactly as the existing tests specify - a doubled
    leading slash is only unambiguous on backslash-spelled Windows paths.
  • Three or more leading backslashes are not a valid UNC form and keep the
    current collapsing behaviour.

Tests

5 new assertions (plain UNC, admin share c$, trailing-slash stripping and
stripTrailing: false). Full suite: 34 passing before, 39 passing after,
0 failing; all pre-existing expectations untouched.

Downstream context

This is the root cause of stylelint/stylelint#3045 (linting a file addressed
via a UNC path silently reports no results): stylelint normalizes the entry
before handing it to fast-glob, and the collapsed /server/share/... no
longer matches the file. A stylelint maintainer asked whether this could be
fixed upstream here rather than worked around there
(stylelint/stylelint#3045 (comment)).

Verified end to end on a real Windows host: with this patch dropped into
stylelint's node_modules, stylelint's UNC reproducer
(\\localhost\<drive>$\...) lints correctly with no changes on the
stylelint side; with the unpatched file it fails.

`normalize()` collapses the leading double backslash of a plain UNC path,
so `\\server\share\file.css` becomes `/server/share/file.css` and the
path loses its network-path identity. The Win32 namespace prefixes
(`\\?\`, `\\.\`) are already special-cased to keep their doubled leading
slash; this applies the same treatment to plain UNC paths.

Forward-slash inputs like `//foo/bar` are intentionally left unchanged
(still collapse to `/foo/bar`), matching the existing test expectations,
since a leading double slash is only meaningful on Windows input paths
spelled with backslashes.

Downstream context: stylelint/stylelint#3045 (files addressed via a UNC
path silently produce no lint results, because the collapsed path no
longer matches anything when handed to fast-glob).

Signed-off-by: Eljees <3.14hell@gmail.com>
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.

1 participant