fix(reference): preserve physical failures across late cancellation (#10101)

* fix(reference): preserve physical root admission failures

* fix(reference): retain physical Git read origins during cancellation

* fix(reference): preserve Git directory lookup failures
This commit is contained in:
morluto authored and GitHub committed 2026-11-11 21:39:09 +08:00
1 parent 437109e574
commit 4dedf3e865
5 files changed
+312 -79

No files matched your search

+50 -22
View File
@@ -1,6 +1,9 @@
import fs from "node:fs";
import { join } from "node:path";
import { locateReferenceSourceGitDirectory } from "./ReferenceSourceGitDirectory.js";
import {
locateReferenceSourceGitDirectory,
ReferenceSourceGitDirectoryCancelled,
} from "./ReferenceSourceGitDirectory.js";
import { err, ok, type Result } from "../domain/result.js";
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
@@ -72,9 +75,19 @@ const resolveSourceVcs = async (
const cleanupFailures: ArtifactReaderFailure[] = [];
const readMetadataBytes = async (path: string): Promise<Buffer> => {
try {
return await readRegularFile(path, { signal });
return await readRegularFile(path, {
signal,
cancellationFailure: (reason) =>
new ArtifactReaderFailure(
"cancelled",
"Git metadata read cancelled",
{
cause: reason,
},
),
});
} catch (cause: unknown) {
if (signal?.aborted === true && cause === signal.reason) throw cause;
if (cause instanceof ArtifactReaderFailure) throw cause;
if (!(cause instanceof RegularFileCleanupFailure)) {
if (
signal?.aborted === true &&
@@ -92,11 +105,15 @@ const resolveSourceVcs = async (
{ cause: physical },
);
}
throw cause;
throw cause instanceof RegularFileReadFailure
? cause.operationCause
: cause;
}
const cancellation = regularFileCancellation(cause);
const retry = await retryRegularFileCleanup(cause, resources, signal);
if (
retry.outcome.kind === "failed" &&
cancellation === undefined &&
errorCode(retry.outcome.cause) !== "ENOENT"
)
failedRead ??= { cause: retry.outcome.cause };
@@ -111,13 +128,10 @@ const resolveSourceVcs = async (
? retry.outcome.cause
: cause.cleanupCause;
throw new ArtifactReaderFailure(
regularFileCancellation(cause) !== undefined ||
(signal?.aborted === true && primary === signal.reason)
? "cancelled"
: "io",
cancellation === undefined ? "io" : "cancelled",
`Git metadata could not be read safely: ${errorMessage(primary)}`,
{
cause: primary,
cause: cancellation === undefined ? primary : cancellation.reason,
...(retry.cleanup === undefined ? {} : { cleanup: retry.cleanup }),
},
);
@@ -130,9 +144,7 @@ const resolveSourceVcs = async (
failure.cleanup === undefined ? [] : [failure.cleanup],
);
throw new ArtifactReaderFailure(
(primary instanceof ArtifactReaderFailure &&
primary.reason === "cancelled") ||
(signal?.aborted === true && primary === signal.reason)
primary instanceof ArtifactReaderFailure && primary.reason === "cancelled"
? "cancelled"
: "io",
`Git metadata could not be read safely: ${errorMessage(primary)}`,
@@ -161,9 +173,9 @@ const resolveSourceVcs = async (
...fs,
promises: {
...fs.promises,
// Let isomorphic-git continue from a failed loose ref to packed-refs,
// then report the original read failure after resolution. This preserves
// both observations when packed metadata also fails cleanup.
// Let isomorphic-git finish probing refs, then report original failures.
// A later resolved ref must not discard an earlier physical read or
// retained cleanup failure.
readFile: async (
path: string | undefined,
options:
@@ -209,22 +221,38 @@ const resolveSourceVcs = async (
head = await resolveRef({ fs: referenceFs, dir: root, ...options });
} catch (cause: unknown) {
if (cleanupFailures.length > 0)
throwCleanupFailures(failedRead?.cause ?? cause);
throwCleanupFailures(failedRead?.cause ?? cleanupFailures[0] ?? cause);
if (failedRead !== undefined) throw failedRead.cause;
throw cause;
}
if (cleanupFailures.length > 0)
throwCleanupFailures(failedRead?.cause ?? cleanupFailures[0]?.cause);
throwCleanupFailures(failedRead?.cause ?? cleanupFailures[0]);
signal?.throwIfAborted();
if (failedRead !== undefined) throw failedRead.cause;
return head;
};
const locations = await locateReferenceSourceGitDirectory(
root,
readMetadataText,
signal,
);
let locations;
try {
locations = await locateReferenceSourceGitDirectory(
root,
readMetadataText,
signal,
);
} catch (cause: unknown) {
if (
signal?.aborted === true &&
!(cause instanceof ReferenceSourceGitDirectoryCancelled) &&
!(cause instanceof ArtifactReaderFailure)
)
throw new ArtifactReaderFailure(
"io",
`Git directory could not be resolved: ${errorMessage(cause)}`,
{ cause },
);
throw cause;
}
if (locations === undefined) return { kind: "none", head: null, dirty: null };
if (isAborted(signal)) return unknownVcs();
const { gitDirectory, commonDirectory } = locations;
if (gitDirectory === commonDirectory) {
const head = await readRef({ gitdir: gitDirectory, ref: "HEAD" });