Skip to content

Commit 9dd94fd

Browse files
committed
Adjust path-utils
1 parent 96e251f commit 9dd94fd

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

src/filesystem/path-utils.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,26 @@ export function normalizePath(p: string): string {
5353
// Convert WSL or Unix-style Windows paths to Windows format
5454
p = convertToWindowsPath(p);
5555

56-
// Check if this is a UNC path before normalization
57-
const isUNCPath = p.startsWith('\\\\');
58-
59-
// For non-UNC paths, normalize double backslashes first
60-
if (!isUNCPath) {
56+
// Handle double backslashes, preserving leading UNC \\
57+
if (p.startsWith('\\\\')) {
58+
// For UNC paths, normalize double backslashes *after* the leading marker
59+
const restOfPath = p.substring(2).replace(/\\\\/g, '\\');
60+
p = '\\\\' + restOfPath;
61+
} else {
62+
// For non-UNC paths, normalize all double backslashes
6163
p = p.replace(/\\\\/g, '\\');
6264
}
6365

6466
// Use Node's path normalization, which handles . and .. segments
6567
let normalized = path.normalize(p);
6668

67-
// Handle UNC paths after normalization to preserve the leading \\
68-
if (isUNCPath) {
69-
// Ensure UNC path starts with exactly two backslashes
70-
if (normalized.startsWith('\\') && !normalized.startsWith('\\\\')) {
71-
normalized = '\\' + normalized;
72-
}
73-
// Normalize any remaining double backslashes in the rest of the path
74-
normalized = normalized.replace(/^(\\\\)(.*)/, (match, leading, rest) => {
75-
return leading + rest.replace(/\\\\/g, '\\');
76-
});
69+
// Fix UNC paths after normalization (path.normalize can remove a leading backslash)
70+
if (p.startsWith('\\\\') && !normalized.startsWith('\\\\')) {
71+
normalized = '\\' + normalized;
7772
}
7873

7974
// Handle Windows paths: convert slashes and ensure drive letter is capitalized
80-
if (normalized.match(/^[a-zA-Z]:|^\/mnt\/[a-z]\/|^\/[a-z]\//i)) {
75+
if (normalized.match(/^[a-zA-Z]:/)) {
8176
let result = normalized.replace(/\//g, '\\');
8277
// Capitalize drive letter if present
8378
if (/^[a-z]:/.test(result)) {
@@ -86,8 +81,9 @@ export function normalizePath(p: string): string {
8681
return result;
8782
}
8883

89-
// Leave other paths unchanged
90-
return normalized;
84+
// For all other paths (including relative paths), convert forward slashes to backslashes
85+
// This ensures relative paths like "some/relative/path" become "some\\relative\\path"
86+
return normalized.replace(/\//g, '\\');
9187
}
9288

9389
/**

0 commit comments

Comments
 (0)