// Plausible bug: `..` at the root is silently clamped, and relativeTo compares string prefixes instead of segments.export type PathErrorInfo = | { kind: "invalid_cwd" } | { kind: "escapes_root"; input: string } | { kind: "not_absolute"; path: string };export class PathError extends Error { declare readonly kind: PathErrorInfo["kind"]; declare readonly input?: string; declare readonly path?: string; constructor(info: PathErrorInfo) { super(info.kind); Object.assign(this, info); }}const normalize = (p: string): string => { const out: string[] = []; for (const seg of p.split("/")) { if (!seg || seg === ".") continue; if (seg === "..") out.pop(); else out.push(seg); } return "/" + out.join("/");};export function resolvePath(cwd: string, input: string): string { if (!cwd.startsWith("/")) throw new PathError({ kind: "invalid_cwd" }); return normalize(input.startsWith("/") ? input : `${cwd}/${input}`);}export function relativeTo(from: string, to: string): string { if (!from.startsWith("/")) throw new PathError({ kind: "not_absolute", path: from }); if (!to.startsWith("/")) throw new PathError({ kind: "not_absolute", path: to }); const a = normalize(from); const b = normalize(to); if (a === b) return "."; if (b.startsWith(a)) return b.slice(a.length).replace(/^\//, ""); const ups: string[] = []; let base = a; while (!b.startsWith(base)) { base = base.slice(0, base.lastIndexOf("/")) || "/"; ups.push(".."); } const rest = b.slice(base.length).replace(/^\//, ""); return [...ups, ...(rest ? [rest] : [])].join("/");}