use libs for path resolution

This commit is contained in:
Patrick Erichsen 2025-10-17 14:29:50 -07:00
parent ee439faa38
commit c38a5bbd9d
3 changed files with 40 additions and 60 deletions

20
core/package-lock.json generated
View file

@ -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",

View file

@ -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",

View file

@ -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<boolean> {
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<ResolvedPath | null> {
// 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;
}