Skip to content

Commit 19aec00

Browse files
author
easonLiangWorldedtech
committed
fix: fold a lock key from the deepest existing ancestor, not the way strings compare
Port of #1915's 5131bc0 to this unit's shape. platform-unit-test (windows-latest) failed here with four safeWriteJson.test.ts assertions reporting 'expected [Function] to throw error including Primary rename failed but got Lock file is already being held', and the two directory-creation names are the diagnosis: when a component of the target is missing, the previous fold could not reach a canonical form. This unit compared the two lock identities by case alone. On Windows the filesystem folds two spellings of one directory entry in two ways - case anywhere, and short (8.3) names inside a component - and a CI agent hands out its runner profile directory as RUNNER~1, so os.tmpdir() below it is spelled two ways at once. resolveLockKey canonicalises through the highest ancestor it can reach, so a case-only comparison leaves the canonical referent key unequal to the requested short spelling: one .lock directory is asked for twice and the second acquisition collides with the first one's own lock. _lockIdentityKey now canonicalises the deepest EXISTING ancestor and appends the segments below it, case-folded on Windows. The walk starts at the PARENT, not at the file: the lock is the entry <path>.lock beside the file, so the final component must never be resolved through a symlink, or a link and its referent would fold into one key and the two distinct .lock entries this call takes on purpose would collapse into one. That boundary was established on the unit this is ported from, where starting the walk at the file turned three existing tests red; the port is not byte-identical because this unit computed sameIdentity inline. Verification and its limit, stated plainly: the four CI failures do not reproduce on this host, because the local os.tmpdir() has no short-name ancestor - only a CI agent (or a host where 8.3 names are in play) exercises that spelling, so the four are verified by CI, not locally. What is verified locally is that the fold does not disturb anything else: the safeWriteJson specs pass 37 / 4 skipped, and safeWriteText.integration.spec.ts fails the same single test with and without this change (identical by name, checked by stashing the change and re-running), which is the known environment limit - that test runs the real icacls, which cannot run in this sandbox. prettier --write then --check with the repo config clean; tsc --noEmit with the local paths override 0 errors; eslint . --ext=ts --max-warnings=0 exits 0; eslint-suppressions.json unchanged. Still owed by this port, recorded rather than assumed: the mixed-ancestor acceptance test (existing ancestor spelled short with a missing tail) has not been ported yet, because in this unit sameIdentity is only consulted when publishing over a link, so the test must be re-derived against that shape rather than copied.
1 parent 5e99a1a commit 19aec00

1 file changed

Lines changed: 47 additions & 7 deletions

File tree

‎src/utils/safeWriteJson.ts‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,44 @@ function _scopeErrorCode(error: unknown): string | undefined {
133133
* @param {SafeWriteJsonOptions} options - Optional configuration for JSON formatting.
134134
* @returns {Promise<void>}
135135
*/
136+
/**
137+
* Fold a path into the identity its advisory lock is actually placed under.
138+
*
139+
* acquireFileLock locks `<absolute path>.lock` with realpath:false (see fileLock.ts), so the
140+
* lock's identity is the directory ENTRY the path names, not the string. On Windows the
141+
* filesystem folds two spellings of one entry in two ways: case anywhere, and short (8.3) names
142+
* inside a component - the runner profile directory arrives as RUNNER~1 on a CI agent, which is
143+
* where this was first observed. A case-only comparison folds only the first, so the second
144+
* leaves two keys that look different, one .lock directory, and a second acquisition that
145+
* collides with the first one's own lock: 'Lock file is already being held' once the retries are
146+
* spent.
147+
*
148+
* So fold the way the lock is placed: canonicalise the deepest EXISTING ancestor and append the
149+
* segments below it, case-folded on Windows. Canonicalising an existing ancestor is what folds a
150+
* short name, because that folding belongs to the filesystem rather than to any string rule; a
151+
* tail that does not exist yet can only be folded by the string rule. The walk starts at the
152+
* PARENT, not at the file: the lock is the entry beside the file, so the final component must
153+
* never be resolved through a symlink, or a link and its referent would fold into one key and
154+
* the two distinct .lock entries this call takes on purpose would collapse into one.
155+
*/
156+
async function _lockIdentityKey(absoluteFilePath: string): Promise<string> {
157+
const missing: string[] = [path.basename(absoluteFilePath)]
158+
let current = path.dirname(absoluteFilePath)
159+
for (;;) {
160+
let canonical: string | undefined
161+
try {
162+
canonical = await fs.realpath(current)
163+
} catch {
164+
// This component is not there yet; an ancestor of it may be.
165+
}
166+
if (canonical !== undefined || path.dirname(current) === current) {
167+
const folded = path.join(canonical ?? current, ...missing)
168+
return process.platform === "win32" ? folded.toLowerCase() : folded
169+
}
170+
missing.unshift(path.basename(current))
171+
current = path.dirname(current)
172+
}
173+
}
136174
async function safeWriteJson(filePath: string, data: any, options?: SafeWriteJsonOptions): Promise<void> {
137175
const absoluteFilePath = path.resolve(filePath)
138176
// One release per lock acquired, kept in acquisition order and released in reverse below.
@@ -164,14 +202,16 @@ async function safeWriteJson(filePath: string, data: any, options?: SafeWriteJso
164202
// only the link-path lock lets those two writers overlap, and their merge reads overwrite each
165203
// other. Resolving the referent here costs one more realpath on a default write; it is a read of
166204
// the link target for locking purposes, not a decision to publish through it.
167-
// resolveLockKey canonicalizes and Windows paths are case-insensitive, so the two keys are
168-
// compared the same way the filesystem would: byte-for-byte they can name one file twice, and
169-
// locking a file that this call already locked would stall on its own stale timeout.
205+
// The two keys are compared the way the filesystem would, not the way strings compare:
206+
// byte-for-byte they can name one file twice, and locking a file this call already locked
207+
// would stall on its own stale timeout. Folding is per _lockIdentityKey, because case folding
208+
// alone leaves a short-name spelling unequal to the canonical one resolveLockKey returns.
170209
const linkPathLockKey = absoluteFilePath
171-
const sameIdentity =
172-
process.platform === "win32"
173-
? referentLockKey.toLowerCase() === linkPathLockKey.toLowerCase()
174-
: referentLockKey === linkPathLockKey
210+
const [referentLockIdentity, linkPathLockIdentity] = await Promise.all([
211+
_lockIdentityKey(referentLockKey),
212+
_lockIdentityKey(linkPathLockKey),
213+
])
214+
const sameIdentity = referentLockIdentity === linkPathLockIdentity
175215
const lockKeys = publishOverLink
176216
? sameIdentity
177217
? [linkPathLockKey]

0 commit comments

Comments
 (0)