refactor(exec): dedupe executable candidate resolution

This commit is contained in:
Peter Steinberger
2026-04-03 01:57:20 +09:00
parent 48279dca84
commit e36c563775
4 changed files with 51 additions and 30 deletions

View File

@@ -3,8 +3,10 @@ import path from "node:path";
import { matchesExecAllowlistPattern } from "./exec-allowlist-pattern.js";
import type { ExecAllowlistEntry } from "./exec-approvals.js";
import { resolveExecWrapperTrustPlan } from "./exec-wrapper-trust-plan.js";
import { resolveExecutablePath as resolveExecutableCandidatePath } from "./executable-path.js";
import { expandHomePrefix } from "./home-dir.js";
import {
resolveExecutablePath as resolveExecutableCandidatePath,
resolveExecutablePathCandidate,
} from "./executable-path.js";
export type ExecutableResolution = {
rawExecutable: string;
@@ -45,10 +47,6 @@ function parseFirstToken(command: string): string | null {
return match ? match[0] : null;
}
function isDriveLessWindowsRootedPath(value: string): boolean {
return process.platform === "win32" && /^:[\\/]/.test(value);
}
function tryResolveRealpath(filePath: string | undefined): string | undefined {
if (!filePath) {
return undefined;
@@ -179,18 +177,10 @@ function resolveExecutableCandidatePathFromResolution(
if (!raw) {
return undefined;
}
const expanded = raw.startsWith("~") ? expandHomePrefix(raw) : raw;
if (isDriveLessWindowsRootedPath(expanded)) {
return undefined;
}
if (!expanded.includes("/") && !expanded.includes("\\")) {
return undefined;
}
if (path.isAbsolute(expanded)) {
return expanded;
}
const base = cwd && cwd.trim() ? cwd.trim() : process.cwd();
return path.resolve(base, expanded);
return resolveExecutablePathCandidate(raw, {
cwd,
requirePathSeparator: true,
});
}
export function resolveExecutionTargetResolution(