fix(coding-agent): disambiguate resource paths

This commit is contained in:
Armin Ronacher
2026-05-08 00:16:28 +02:00
parent 783e96a144
commit 3421726e86
5 changed files with 51 additions and 14 deletions

View File

@@ -1,4 +1,5 @@
import { realpathSync } from "node:fs";
import { isAbsolute, relative, resolve as resolvePath, sep } from "node:path";
/**
* Resolve a path to its canonical (real) form, following symlinks.
@@ -34,3 +35,23 @@ export function isLocalPath(value: string): boolean {
}
return true;
}
function resolveAgainstCwd(filePath: string, cwd: string): string {
return isAbsolute(filePath) ? resolvePath(filePath) : resolvePath(cwd, filePath);
}
export function getCwdRelativePath(filePath: string, cwd: string): string | undefined {
const resolvedCwd = resolvePath(cwd);
const resolvedPath = resolveAgainstCwd(filePath, resolvedCwd);
const relativePath = relative(resolvedCwd, resolvedPath);
const isInsideCwd =
relativePath === "" ||
(relativePath !== ".." && !relativePath.startsWith(`..${sep}`) && !isAbsolute(relativePath));
return isInsideCwd ? relativePath || "." : undefined;
}
export function formatPathRelativeToCwdOrAbsolute(filePath: string, cwd: string): string {
const absolutePath = resolveAgainstCwd(filePath, cwd);
return (getCwdRelativePath(absolutePath, cwd) ?? absolutePath).split(sep).join("/");
}