Files
openchamber/packages/ui/src/lib/pathNormalization.ts
T
9624d4b6f6 fix(ui,server): normalize Windows drive letter casing for consistent path resolution (#2154)
* fix(ui,server): normalize Windows drive letter casing for consistent path resolution

Provider settings were failing to persist for specific projects on Windows
because path normalization was inconsistent across the codebase. Some
normalizePath functions uppercased the Windows drive letter (c:\ -> C:\)
and others did not, causing:
  - directoryScoped cache misses (different keys for the same directory)
  - broken model selection in the affected project
  - lost conversation history (sessions could not match their project)
  - false cache hits in resolveConfigDirectory on undefined inputs

This change extracts a single shared normalizePath utility and uses it
from the 5 client sites that were missing the drive letter normalization.
The server-side normalizePathForPersistence is updated to uppercase the
drive letter both before and after safeRealpathSync, so the persisted
path is consistent even when realpath returns a symlink/junction with
a lowercase drive letter on some Windows environments.

Fixes #2109

* test(ui,server): add coverage for Windows path normalization

Address review feedback on #2154:
- Add Windows-platform test for normalizePathForPersistence covering
  drive letter uppercase on input and after realpath resolution
- Add dedicated test suite for the shared normalizePath utility
- Defensive fix: normalizePath now returns null for paths that consist
  only of slashes (\\, ///), matching the documented contract

Refs #2154

* fix(server): scope drive casing normalization to Windows

---------

Co-authored-by: bashrusakh <bashrusakh@users.noreply.github.com>
Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
2026-07-13 01:43:35 +03:00

30 lines
1.1 KiB
TypeScript

/**
* Normalize a directory path for consistent comparison.
*
* Handles Windows-specific path quirks:
* - Converts backslashes to forward slashes
* - Uppercases lowercase Windows drive letters (e.g., "c:\\" → "C:\\")
* - Trims trailing slashes (except for the root "/")
*
* Returns null for non-string inputs, null/undefined, empty strings,
* whitespace-only strings, and paths that consist only of slashes
* (e.g. "\\", "\\\\", "///").
*
* The drive letter regex is anchored (^([a-z]):) and matches only a
* single lowercase letter, so it never affects multi-character tokens
* (e.g., "abc:def"), URLs, or Windows `\\?\` device paths.
*/
export const normalizePath = (value?: string | null): string | null => {
if (typeof value !== "string") return null;
const trimmed = value.trim();
if (!trimmed) return null;
const replaced = trimmed
.replace(/\\/g, "/")
.replace(/^([a-z]):/, (_, letter: string) => letter.toUpperCase() + ":");
if (replaced === "/") return "/";
const stripped = replaced.length > 1 ? replaced.replace(/\/+$/, "") : replaced;
return stripped || null;
};