fix(git): canonicalize paths the same way @airship/core does

The windows-latest leg failed the two Codex-path tests with a null baseline,
and the cause was a second implementation of the same rule.

@airship/git had its own realpathSafe built on the JS realpathSync, while
core's canonicalPath used realpathSync.native for the Windows case-folding.
Those disagree on more than case: the JS version leaves an 8.3 short name
alone, so a repo under `C:\Users\RUNNER~1\…` stayed short here while
DiffCapture's keys came back as `C:\Users\runneradmin\…`. fileAtHead computes
`relative(root, path)` from the two, got a `..` path, and returned null for
every file — which on the Codex path means no baseline at all, so each edit
would render as a whole-file diff against nothing.

This is exactly the drift the ./paths module was added to end; it unified
core's four copies and left this fifth one. The rule now lives here, in the
lowest package that touches the filesystem, and core imports it. Deliberately
not re-exported from ./paths — one implementation, one import path, so there
is nowhere for a second copy to reappear.
This commit is contained in:
Nayan
2026-08-11 23:27:58 +05:30
parent ff9472776b
commit 3f193ac4f2
5 changed files with 41 additions and 42 deletions
+2 -1
View File
@@ -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 = /^/;
+1 -6
View File
@@ -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";
+2 -1
View File
@@ -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;
+9 -27
View File
@@ -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.
+27 -7
View File
@@ -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);
}
}
}