Enhance convert_path_with_tilde_expansion to handle Windows (#4390)

This commit is contained in:
Max Novich
2025-08-28 14:32:39 -07:00
committed by GitHub
parent 74ce62d13d
commit 1d0c08b3d3
3 changed files with 59 additions and 27 deletions
+11
View File
@@ -119,7 +119,18 @@ const addPaths = (
* @returns The expanded path with tilde replaced by home directory
*/
export function expandTilde(filePath: string): string {
if (!filePath || typeof filePath !== 'string') return filePath;
// Support "~", "~/..." and "~\\..." on Windows
if (filePath === '~') {
return os.homedir();
}
if (filePath.startsWith('~/') || (process.platform === 'win32' && filePath.startsWith('~\\'))) {
// Remove the leading "~" and any separator that follows, then join
const remainder = filePath.slice(2);
return path.join(os.homedir(), remainder);
}
if (filePath.startsWith('~')) {
// Generic fallback: replace only the first leading tilde
return path.join(os.homedir(), filePath.slice(1));
}
return filePath;