diff --git a/packages/core/src/diff-capture.ts b/packages/core/src/diff-capture.ts index c7599fa..5da5c09 100644 --- a/packages/core/src/diff-capture.ts +++ b/packages/core/src/diff-capture.ts @@ -15,9 +15,10 @@ */ import { existsSync, readFileSync } from "node:fs"; import { relative, resolve } from "node:path"; +import { canonicalPath } from "@airship/git"; import type { FileDiff } from "@airship/protocol"; import { createPatch } from "diff"; -import { canonicalPath, toPosixPath } from "./paths"; +import { toPosixPath } from "./paths"; const CRLF = /\r\n/g; const BOM = /^/; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 1e3e62e..6f545a8 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -7,12 +7,7 @@ export type { export { getAdapter } from "./agent"; export { DiffCapture } from "./diff-capture"; /** Path identity, shared so containment, diff keys and history keys agree. */ -export { - canonicalPath, - isPathInside, - pathKey, - toPosixPath, -} from "./paths"; +export { isPathInside, pathKey, toPosixPath } from "./paths"; export type { EditPromptInput } from "./prompt"; export { buildEditPrompt, PIKA_SYSTEM_PROMPT, systemPrompt } from "./prompt"; export type { CodexConfigValue, CodexSettings } from "./providers/codex"; diff --git a/packages/core/src/paths.test.ts b/packages/core/src/paths.test.ts index 70dfcc2..ca03fcb 100644 --- a/packages/core/src/paths.test.ts +++ b/packages/core/src/paths.test.ts @@ -25,8 +25,9 @@ import { } from "node:fs"; import { tmpdir } from "node:os"; import { join, parse, sep } from "node:path"; +import { canonicalPath } from "@airship/git"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { canonicalPath, isPathInside, pathKey, toPosixPath } from "./paths"; +import { isPathInside, pathKey, toPosixPath } from "./paths"; let root: string; diff --git a/packages/core/src/paths.ts b/packages/core/src/paths.ts index 4c4d594..be3dc95 100644 --- a/packages/core/src/paths.ts +++ b/packages/core/src/paths.ts @@ -26,36 +26,18 @@ * and undoes it with the turn, and the history store splits in two so undo and * PR-from-session silently find nothing. */ -import { realpathSync } from "node:fs"; -import { basename, dirname, resolve, sep } from "node:path"; +import { resolve, sep } from "node:path"; +import { canonicalPath } from "@airship/git"; const WIN32 = process.platform === "win32"; -/** - * `.native` on Windows for the case-folding above. Elsewhere the JS version is - * the better choice — it is faster and does not go through the OS handle API. - */ -const realpath = WIN32 ? realpathSync.native : realpathSync; - -/** - * Resolve symlinks (and, on Windows, case) so two spellings of one path compare - * equal. - * - * Falls back to the containing directory for a file that does not exist yet — - * that is the create case, and it is the common one — and to the raw resolved - * path when even the parent is missing. - */ -export function canonicalPath(path: string): string { - try { - return realpath(path); - } catch { - try { - return resolve(realpath(dirname(path)), basename(path)); - } catch { - return resolve(path); - } - } -} +// canonicalPath comes from @airship/git and is deliberately NOT re-exported +// here: one implementation, one import path. That package needs the identical +// rule for `fileAtHead`, which derives a repo-relative path from keys this +// module produced, and when a second copy lived here the two drifted on +// Windows — the JS realpathSync leaves an 8.3 short name (`RUNNER~1`) alone +// while the native one expands it (`runneradmin`) — so `relative` returned a +// `..` path and every file read as absent from HEAD. /** * A stable comparison key for a path. diff --git a/packages/git/src/index.ts b/packages/git/src/index.ts index 3601ab6..d637532 100644 --- a/packages/git/src/index.ts +++ b/packages/git/src/index.ts @@ -128,8 +128,8 @@ export function fileAtHead(cwd: string, path: string): string | null { // from mixed sources — some resolved through git, some as the user typed // them — and on macOS `/tmp` and `/var` are symlinks, so an uncanonicalized // comparison reads a perfectly normal path as escaping the project. - const root = realpathSafe(cwd); - const rel = relative(root, realpathSafe(resolve(cwd, path))); + const root = canonicalPath(cwd); + const rel = relative(root, canonicalPath(resolve(cwd, path))); // `..` means the path really does escape the working directory; `HEAD:./..` // is not valid git syntax and there is nothing sensible to return. if (!rel || rel.startsWith("..")) { @@ -138,15 +138,35 @@ export function fileAtHead(cwd: string, path: string): string | null { return tryGitRaw(cwd, ["show", `HEAD:./${rel.split(sep).join("/")}`]); } -/** Resolve symlinks where possible, falling back for paths that don't exist. */ -function realpathSafe(path: string): string { +/** + * Resolve symlinks — and, on Windows, case and 8.3 short names — where + * possible, falling back for paths that don't exist. + * + * `.native` on Windows is not a detail. The JS implementation resolves links + * while preserving whatever spelling the caller passed, so `C:\Users\RUNNER~1` + * stays short while anything that went through `GetFinalPathNameByHandle` + * reads `C:\Users\runneradmin`. Two spellings of one directory make `relative` + * below return a `..` path, and `fileAtHead` then reports every file as absent + * from HEAD — which on the Codex path silently turns each edit into a + * whole-file diff with no baseline. + * + * Exported, and re-exported by @airship/core's ./paths rather than + * reimplemented there: core canonicalizes DiffCapture's map keys and hands the + * results straight to `fileAtHead`, so a second implementation is a second + * chance for the two to disagree. This package is the lowest one that touches + * the filesystem, which makes it the place that owns the rule. + */ +const realpath = + process.platform === "win32" ? realpathSync.native : realpathSync; + +export function canonicalPath(path: string): string { try { - return realpathSync(path); + return realpath(path); } catch { try { - return resolve(realpathSync(dirname(path)), basename(path)); + return resolve(realpath(dirname(path)), basename(path)); } catch { - return path; + return resolve(path); } } }