Skip to content

Commit b60eca1

Browse files
authored
fix(filesystem): ensure bare Windows drive letters normalize to root (#3434)
fix(filesystem): ensure bare Windows drive letters normalize to root Appends path.sep to bare drive letters (e.g. "C:") before calling path.normalize(), preventing them from normalizing to "C:." (current directory on drive) instead of "C:\" (drive root). Includes test coverage with platform mocking. Fixes #3418
1 parent 1cdf806 commit b60eca1

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/filesystem/__tests__/path-utils.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ describe('Path Utilities', () => {
354354
expect(result).not.toContain('\\');
355355
});
356356

357+
it('normalizes bare Windows drive letters to the drive root on Windows', () => {
358+
Object.defineProperty(process, 'platform', {
359+
value: 'win32',
360+
writable: true,
361+
configurable: true
362+
});
363+
364+
expect(normalizePath('C:')).toBe('C:\\');
365+
expect(normalizePath('d:')).toBe('D:\\');
366+
});
367+
357368
it('should handle relative path slash conversion based on platform', () => {
358369
// This test verifies platform-specific behavior naturally without mocking
359370
// On Windows: forward slashes converted to backslashes

src/filesystem/path-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ export function normalizePath(p: string): string {
7777
p = p.replace(/\\\\/g, '\\');
7878
}
7979

80+
// On Windows, if we have a bare drive letter (e.g. "C:"), append a separator
81+
// so path.normalize doesn't return "C:." which can break path validation.
82+
if (process.platform === 'win32' && /^[a-zA-Z]:$/.test(p)) {
83+
p = p + path.sep;
84+
}
85+
8086
// Use Node's path normalization, which handles . and .. segments
8187
let normalized = path.normalize(p);
8288

@@ -116,3 +122,4 @@ export function expandHome(filepath: string): string {
116122
}
117123
return filepath;
118124
}
125+

0 commit comments

Comments
 (0)