Skip to content

Commit d29b7a3

Browse files
test: intercept the commit rename by identity, not by spelling
The windows leg at a06845b came back 13 failed instead of 20, and the two clusters split apart cleanly. job 128591015715 (run 42434722070) shows zero StagingPathError: the eight staging-guard failures are gone, so the guard fix works on the runner. What is left is one file and one payload AssertionError: expected 'WRITE_COMPLETED\n' to contain 'EXIT_AT_REPLACEMENT_RENAME' at safeWriteJson.interruption.test.ts:215 for the twelve recovery cases and :328 for the junction case. WRITE_COMPLETED means the child's fault hook never recognised the commit rename at all, so the write finished. ubuntu passed the same file, and it passes locally here, because neither box presents the condition; the runner does. The hook decided by text plus fs.realpath: compare the resolved destination against the parent's path and against realpathSync(target), and otherwise compare realpathSync(destination) with realpathSync(target). That cannot work where one directory answers under two spellings. Measured on a real 8.3 alias pair on this box, feeding the shipped matcher both spellings: target spelled LONG, destination spelled through the alias: committed matcher false target spelled SHORT, destination spelled the long way: committed matcher false a file in another directory, either spelling: false (correct) and the runner's own data says the same thing from the other side: the staging-guard message that used to appear there expected the long spelling of the temp directory and got RUNNER~1 for the same directory, which is what its canonicaliser handed back. Two canonicalisations that do not agree, so no amount of canonicalising on one side matches the other. fs.stat reports one identity for both spellings, so the interception decision is now taken on identity: same path text, or same case-insensitive path text on win32, or the same dev+ino, or - when the destination is not on disk yet and has no identity of its own - the same directory identity plus the same basename. The negative case is unchanged: a staging file under another directory's alias is still not the commit. Pinned by a test instead of by a platform that cannot show the condition. The child now answers a matcher self-check (INTERRUPTION_MODE=matcher) without building or writing anything, and 'recognises the commit rename through a short-name alias, and refuses another directory' creates two long-named directories, asks cmd for their 8.3 spellings, and asserts the decisions from both spellings. Against the shipped matcher it fails with 'alias: false' (S268: 1 failed | 13 passed); after the change all 14 pass (S270, S272). It returns early on non-win32 and when a volume generates no short names, so it cannot fail the way the alias spec did on ubuntu. Gates (pinned v22.23.4): vitest.misc.config.ts on this file 14 passed; src tsc --noEmit exit 0; eslint --prune-suppressions --max-warnings=0 on the file exit 0 with no count delta (265 entries both sides) and eslint-suppressions.json left at blob 42be144; prettier clean; knip exit 0. Note for whoever reads the next windows run: this leg aborted at test:misc, so the services shard - and with it safeWriteText.alias.spec.ts - has not executed on windows yet at any head. Mutation preflight skipped and said out loud: the unit is far over the advisory cap. Refs: #1911
1 parent a06845b commit d29b7a3

1 file changed

Lines changed: 141 additions & 14 deletions

File tree

‎src/utils/__tests__/safeWriteJson.interruption.test.ts‎

Lines changed: 141 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawn, type ChildProcess } from "child_process"
1+
import { execFileSync, spawn, type ChildProcess } from "child_process"
22
import { createRequire } from "module"
33
import { fileURLToPath } from "url"
44
import * as fs from "fs/promises"
@@ -38,25 +38,54 @@ const targetPath = path.resolve(process.env.INTERRUPTION_TARGET_PATH)
3838
// comparison would then let the commit land unnoticed. Canonicalise this side the same way,
3939
// and fall back to the textual form for a target that does not exist yet and cannot be
4040
// canonicalised.
41-
let canonicalTargetPath = targetPath
42-
try {
43-
canonicalTargetPath = fs.realpathSync(targetPath)
44-
} catch {
45-
// A target that is not on disk yet has no canonical spelling; the textual form is then
46-
// the only comparison available, and it is the one the writer would use anyway.
41+
const identityOf = (entry) => {
42+
try {
43+
const stat = fs.statSync(entry, { bigint: true })
44+
return stat.dev + ":" + stat.ino
45+
} catch {
46+
return null
47+
}
4748
}
48-
const isCommitRename = (newPath) => {
49-
const resolved = path.resolve(String(newPath))
50-
if (resolved === targetPath || resolved === canonicalTargetPath) {
49+
const sameEntry = (candidate, expected) => {
50+
const resolved = path.resolve(String(candidate))
51+
const wanted = path.resolve(String(expected))
52+
if (resolved === wanted) {
5153
return true
5254
}
53-
try {
54-
return fs.realpathSync(resolved) === canonicalTargetPath
55-
} catch {
56-
// A destination that cannot be canonicalised is not the canonical target.
55+
if (process.platform === "win32" && resolved.toLowerCase() === wanted.toLowerCase()) {
56+
return true
57+
}
58+
const candidateIdentity = identityOf(resolved)
59+
const wantedIdentity = identityOf(wanted)
60+
if (candidateIdentity !== null && wantedIdentity !== null && candidateIdentity === wantedIdentity) {
61+
return true
62+
}
63+
// A destination that is not on disk yet has no identity of its own, so compare the directory it
64+
// would land in and the name it would land under: same directory identity and same basename is
65+
// the same file, whatever spelling either side used.
66+
const candidateName = path.basename(resolved)
67+
const wantedName = path.basename(wanted)
68+
if (
69+
process.platform === "win32"
70+
? candidateName.toLowerCase() !== wantedName.toLowerCase()
71+
: candidateName !== wantedName
72+
) {
5773
return false
5874
}
75+
const candidateDirIdentity = identityOf(path.dirname(resolved))
76+
const wantedDirIdentity = identityOf(path.dirname(wanted))
77+
return (
78+
candidateDirIdentity !== null && wantedDirIdentity !== null && candidateDirIdentity === wantedDirIdentity
79+
)
5980
}
81+
// Text and fs.realpath cannot decide this one. On the hosted Windows runner one directory answers
82+
// under two spellings and the canonicaliser hands back the short form for the profile component
83+
// (RUNNER~1), so the writer's canonical destination and the parent's canonical target are different
84+
// text for one file. Measured here: realpath returns whichever spelling it is handed while fs.stat
85+
// reports a single identity for both; measured on the runner: the staging guard reported the long
86+
// spelling expected against a short got for the same directory. Identity is the only spelling both
87+
// sides agree on, so the interception decision is taken on identity.
88+
const isCommitRename = (newPath) => sameEntry(newPath, targetPath)
6089
const fsPromises = require("fs/promises")
6190
const actualRename = fsPromises.rename
6291
@@ -79,6 +108,17 @@ fsPromises.rename = async function interruptionRename(oldPath, newPath) {
79108
return actualRename.apply(fsPromises, arguments)
80109
}
81110
111+
// Matcher self-check mode: answer the interception decision for candidates handed in from the
112+
// parent, without building or writing anything. This is how the alias decisions are pinned by a
113+
// test on a real second spelling instead of being trusted to a platform that cannot show one.
114+
if (process.env.INTERRUPTION_MODE === "matcher") {
115+
const cases = JSON.parse(process.env.INTERRUPTION_MATCHER_CASES || "[]")
116+
for (const testCase of cases) {
117+
fs.writeSync(1, "MATCH " + testCase.label + "=" + isCommitRename(testCase.candidate) + String.fromCharCode(10))
118+
}
119+
process.exit(0)
120+
}
121+
82122
require(process.env.ESBUILD_PATH).buildSync({
83123
entryPoints: [process.env.SAFE_WRITE_JSON_ENTRY],
84124
bundle: true,
@@ -346,4 +386,91 @@ describe("safeWriteJson subprocess interruption fault injection", () => {
346386
},
347387
45_000,
348388
)
389+
390+
// The interception decision, taken on a directory that answers to two spellings. The
391+
// writer hands the child hook the spelling its own canonicalisation produced, and on the hosted
392+
// Windows runner that spelling is the 8.3 alias of the directory the parent created, so the hook
393+
// has to recognise the commit rename from the file identity rather than from the text it is
394+
// handed. Windows only: there is no short-name alias elsewhere, and a volume that does not
395+
// generate them cannot present one here either.
396+
const shortSpellingOf = (directory: string): string | null => {
397+
if (process.platform !== "win32") {
398+
return null
399+
}
400+
try {
401+
const out = execFileSync("cmd.exe", ["/c", "for %A in (" + directory + ") do @echo %~sA"], {
402+
encoding: "utf8",
403+
}).trim()
404+
return out.includes("~1") ? out : null
405+
} catch {
406+
return null
407+
}
408+
}
409+
410+
it("recognises the commit rename through a short-name alias, and refuses another directory", async () => {
411+
const base = await fs.mkdtemp(path.join(os.tmpdir(), "interruption-matcher-"))
412+
const longDir = path.join(base, "LONGMATCHERDIRECTORYNAME")
413+
const otherDir = path.join(base, "OTHERMATCHERDIRECTORYNAME")
414+
await fs.mkdir(longDir, { recursive: true })
415+
await fs.mkdir(otherDir, { recursive: true })
416+
const shortDir = shortSpellingOf(longDir)
417+
const shortOther = shortSpellingOf(otherDir)
418+
if (shortDir === null || shortOther === null) {
419+
return
420+
}
421+
const fileName = "target.json"
422+
await fs.writeFile(path.join(longDir, fileName), "committed", "utf8")
423+
424+
const runMatcher = async (
425+
targetPath: string,
426+
candidates: Array<[string, string]>,
427+
): Promise<Record<string, string>> => {
428+
const child = spawn(process.execPath, [childScriptPath], {
429+
env: {
430+
...process.env,
431+
INTERRUPTION_TARGET_PATH: targetPath,
432+
INTERRUPTION_MODE: "matcher",
433+
INTERRUPTION_MATCHER_CASES: JSON.stringify(
434+
candidates.map(([label, candidate]) => ({ label, candidate })),
435+
),
436+
ESBUILD_PATH: esbuildPath,
437+
SAFE_WRITE_JSON_ENTRY: safeWriteJsonEntryPath,
438+
INTERRUPTION_BUNDLE_PATH: childBundlePath,
439+
},
440+
stdio: ["ignore", "pipe", "pipe"],
441+
})
442+
const exit = await waitForChildExit(child)
443+
const decisions: Record<string, string> = {}
444+
for (const line of exit.stdout.split(String.fromCharCode(10))) {
445+
const match = line.match(/^MATCH ([^=]+)=(true|false)$/)
446+
if (match) {
447+
decisions[match[1]] = match[2]
448+
}
449+
}
450+
return decisions
451+
}
452+
453+
// Same file, spelled through the alias: the decision must be yes from either side's spelling.
454+
const fromLong = await runMatcher(path.join(longDir, fileName), [
455+
["self", path.join(longDir, fileName)],
456+
["alias", path.join(shortDir, fileName)],
457+
["other-directory-file", path.join(otherDir, fileName)],
458+
["other-directory-alias", path.join(shortOther ?? otherDir, fileName)],
459+
])
460+
expect(fromLong).toEqual({
461+
self: "true",
462+
alias: "true",
463+
"other-directory-file": "false",
464+
"other-directory-alias": "false",
465+
})
466+
467+
const fromShort = await runMatcher(path.join(shortDir, fileName), [
468+
["self", path.join(shortDir, fileName)],
469+
["alias", path.join(longDir, fileName)],
470+
["other-directory-file", path.join(otherDir, fileName)],
471+
])
472+
expect(fromShort).toEqual({ self: "true", alias: "true", "other-directory-file": "false" })
473+
474+
await fs.rm(base, { recursive: true, force: true })
475+
}, 30_000)
349476
})

0 commit comments

Comments
 (0)