diff --git a/core/package-lock.json b/core/package-lock.json index 1c830c623..dcd7fd2c1 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -56,6 +56,7 @@ "mac-ca": "^3.1.0", "node-fetch": "^3.3.2", "node-html-markdown": "^1.3.0", + "normalize-path": "^3.0.0", "ollama": "^0.4.6", "onnxruntime-node": "1.14.0", "openai": "^5.13.1", @@ -76,6 +77,7 @@ "system-ca": "^1.0.3", "tar": "^7.4.3", "tree-sitter-wasms": "^0.1.11", + "untildify": "^6.0.0", "uuid": "^9.0.1", "vectordb": "^0.4.20", "web-tree-sitter": "^0.21.0", @@ -101,6 +103,7 @@ "@types/mozilla-readability": "^0.2.1", "@types/mustache": "^4.2.5", "@types/node-fetch": "^2.6.11", + "@types/normalize-path": "^3.0.2", "@types/pg": "^8.11.6", "@types/plist": "^3.0.5", "@types/request": "^2.48.12", @@ -6858,6 +6861,12 @@ "@types/node": "*" } }, + "node_modules/@types/normalize-path": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/normalize-path/-/normalize-path-3.0.2.tgz", + "integrity": "sha512-DO++toKYPaFn0Z8hQ7Tx+3iT9t77IJo/nDiqTXilgEP+kPNIYdpS9kh3fXuc53ugqwp9pxC1PVjCpV1tQDyqMA==", + "dev": true + }, "node_modules/@types/pad-left": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/@types/pad-left/-/pad-left-2.1.1.tgz", @@ -19207,6 +19216,17 @@ "webpack-virtual-modules": "^0.5.0" } }, + "node_modules/untildify": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/untildify/-/untildify-6.0.0.tgz", + "integrity": "sha512-sA2YTBvW2F463GvSbiZtso+dpuQV+B7xX9saX30SGrR5Fyx4AUcvA/zN+ShAkABKUKVyDaHECsJrHv5ToTuHsQ==", + "engines": { + "node": ">=20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/update-browserslist-db": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.1.1.tgz", diff --git a/core/package.json b/core/package.json index f20ba384b..752353da9 100644 --- a/core/package.json +++ b/core/package.json @@ -31,6 +31,7 @@ "@types/mozilla-readability": "^0.2.1", "@types/mustache": "^4.2.5", "@types/node-fetch": "^2.6.11", + "@types/normalize-path": "^3.0.2", "@types/pg": "^8.11.6", "@types/plist": "^3.0.5", "@types/request": "^2.48.12", @@ -101,6 +102,7 @@ "mac-ca": "^3.1.0", "node-fetch": "^3.3.2", "node-html-markdown": "^1.3.0", + "normalize-path": "^3.0.0", "ollama": "^0.4.6", "onnxruntime-node": "1.14.0", "openai": "^5.13.1", @@ -121,6 +123,7 @@ "system-ca": "^1.0.3", "tar": "^7.4.3", "tree-sitter-wasms": "^0.1.11", + "untildify": "^6.0.0", "uuid": "^9.0.1", "vectordb": "^0.4.20", "web-tree-sitter": "^0.21.0", diff --git a/core/util/pathResolver.ts b/core/util/pathResolver.ts index 235deb3ce..b5cfd8a2d 100644 --- a/core/util/pathResolver.ts +++ b/core/util/pathResolver.ts @@ -1,8 +1,9 @@ -import * as os from "os"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import normalizePath from "normalize-path"; import * as path from "path"; +import untildify from "untildify"; import { IDE } from ".."; import { resolveRelativePathInDir } from "./ideUtils"; -import { localPathToUri } from "./pathToUri"; import { findUriInDirs } from "./uri"; export interface ResolvedPath { @@ -21,72 +22,41 @@ async function isUriWithinWorkspace(ide: IDE, uri: string): Promise { return foundInDir !== null; } -/** - * Resolves user-provided paths that may be: - * - Relative to workspace directories - * - Absolute paths (Unix/Windows) - * - Tilde paths (~/ or ~username/) - * - File URIs (file://) - * - * Returns both the URI and a normalized display path. - */ export async function resolveInputPath( ide: IDE, inputPath: string, ): Promise { - // Trim whitespace const trimmedPath = inputPath.trim(); - // Check for file:// URI + // Handle file:// URIs if (trimmedPath.startsWith("file://")) { - const uri = trimmedPath; - // Extract path from URI for display - const displayPath = decodeURIComponent(uri.slice(7)); - const isWithinWorkspace = await isUriWithinWorkspace(ide, uri); + const displayPath = fileURLToPath(trimmedPath); + const isWithinWorkspace = await isUriWithinWorkspace(ide, trimmedPath); return { - uri, + uri: trimmedPath, displayPath, isAbsolute: true, isWithinWorkspace, }; } - // Expand tilde paths - let expandedPath = trimmedPath; - if (trimmedPath.startsWith("~/")) { - expandedPath = path.join(os.homedir(), trimmedPath.slice(2)); - } else if (trimmedPath === "~") { - expandedPath = os.homedir(); - } else if (trimmedPath.startsWith("~") && trimmedPath.includes("/")) { - // Handle ~username/ format (Unix-like systems) - // For now, we'll just return null as this requires more complex parsing - // and platform-specific handling - return null; - } + // Expand tilde paths (handles ~/ and ~username/) + const expandedPath = untildify(trimmedPath); - // Check if it's an absolute path + // Check if it's an absolute path (including Windows paths) const isAbsolute = path.isAbsolute(expandedPath) || // Windows network paths expandedPath.startsWith("\\\\") || - // Windows drive letters (C:, D:, etc.) + // Windows drive letters /^[a-zA-Z]:/.test(expandedPath); if (isAbsolute) { - // For Windows network paths, handle specially - if (expandedPath.startsWith("\\\\")) { - const networkPath = expandedPath.replace(/\\/g, "/"); - const uri = "file:" + networkPath; // file://server/share format - const isWithinWorkspace = await isUriWithinWorkspace(ide, uri); - return { - uri, - displayPath: expandedPath, - isAbsolute: true, - isWithinWorkspace, - }; - } - // Convert absolute path to URI - const uri = localPathToUri(expandedPath); + // Normalize for cross-platform compatibility (converts \ to /) + const normalizedPath = normalizePath(expandedPath); + + // Convert to file:// URI format + const uri = pathToFileURL(expandedPath).href; const isWithinWorkspace = await isUriWithinWorkspace(ide, uri); return { uri, @@ -96,10 +66,9 @@ export async function resolveInputPath( }; } - // Fall back to relative path resolution within workspace + // Handle relative paths... const workspaceUri = await resolveRelativePathInDir(expandedPath, ide); if (workspaceUri) { - // Relative paths resolved within workspace are always within workspace return { uri: workspaceUri, displayPath: expandedPath, @@ -110,15 +79,3 @@ export async function resolveInputPath( return null; } - -/** - * Normalizes a path for display purposes. - * Contracts home directory to ~ on Unix-like systems. - */ -export function normalizeDisplayPath(fullPath: string): string { - const home = os.homedir(); - if (fullPath.startsWith(home)) { - return "~" + fullPath.slice(home.length); - } - return fullPath; -}