From 7754a7c3664693aeab58f71c8fc4bb42e83f6c45 Mon Sep 17 00:00:00 2001 From: Eljees <3.14hell@gmail.com> Date: Thu, 27 Aug 2026 13:35:41 +0300 Subject: [PATCH] Preserve the leading slashes of Windows UNC paths `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> --- index.js | 7 +++++++ test.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/index.js b/index.js index 6fac553..a02ed71 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,13 @@ module.exports = function(path, stripTrailing) { } } + // preserve the leading slashes of a UNC path (`\\server\share\...`), + // consistent with the win32 namespace prefixes handled above + if (prefix === '' && len > 2 && path[0] === '\\' && path[1] === '\\' && path[2] !== '\\' && path[2] !== '/') { + path = path.slice(2); + prefix = '//'; + } + var segs = path.split(/[/\\]+/); if (stripTrailing !== false && segs[segs.length - 1] === '') { segs.pop(); diff --git a/test.js b/test.js index 55b3bda..bedac48 100644 --- a/test.js +++ b/test.js @@ -84,4 +84,22 @@ describe('normalize-path', function() { }); }); }); + describe('windows UNC paths', function() { + var units = [ + ['\\\\Server01\\user\\docs\\Letter.txt', '//Server01/user/docs/Letter.txt'], + ['\\\\server\\share\\file.css', '//server/share/file.css'], + ['\\\\localhost\\c$\\temp\\file.txt', '//localhost/c$/temp/file.txt'], + ['\\\\server\\share\\', '//server/share'], + ]; + + units.forEach(function(unit) { + it('should preserve the leading slashes of ' + unit[0], function() { + assert.equal(normalize(unit[0]), unit[1]); + }); + }); + + it('should keep a trailing slash when stripTrailing is false', function() { + assert.equal(normalize('\\\\server\\share\\', false), '//server/share/'); + }); + }); });