fix(providers): preserve observations and primary cleanup failures
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { lstat, realpath } from "node:fs/promises";
|
||||
|
||||
import { compareCodePoints } from "../../domain/canonicalOrdering.js";
|
||||
import { canonicalDigest } from "../../domain/comparisonSemantics.js";
|
||||
import { AsarArtifactReader } from "../AsarArtifactReader.js";
|
||||
import {
|
||||
@@ -136,10 +137,12 @@ const materializeSelection = async ({
|
||||
selected.map((item) => [item.occurrence.logical_path, item]),
|
||||
);
|
||||
const reader = await createReader(sourcePath, input);
|
||||
const output = await SafeOutputTree.create(input.outputRoot);
|
||||
let output: SafeOutputTree | undefined;
|
||||
let readerCloseAttempted = false;
|
||||
let readerClosed = false;
|
||||
const extracted: ExtractedOccurrence[] = [];
|
||||
try {
|
||||
output = await SafeOutputTree.create(input.outputRoot);
|
||||
const materialized: SelectedOccurrence[] = [];
|
||||
const registry = new ArtifactPathRegistry();
|
||||
for await (const entry of reader.entries(signal)) {
|
||||
@@ -171,10 +174,11 @@ const materializeSelection = async ({
|
||||
});
|
||||
materialized.push(selectedItem);
|
||||
}
|
||||
readerCloseAttempted = true;
|
||||
await reader.close();
|
||||
readerClosed = true;
|
||||
extracted.sort((left, right) =>
|
||||
left.relative_path.localeCompare(right.relative_path, "en"),
|
||||
compareCodePoints(left.relative_path, right.relative_path),
|
||||
);
|
||||
const result = createExtractionResult(
|
||||
input,
|
||||
@@ -185,12 +189,48 @@ const materializeSelection = async ({
|
||||
await output.commit();
|
||||
return result;
|
||||
} catch (cause: unknown) {
|
||||
if (!readerClosed)
|
||||
await reader.close().catch((cause: unknown) => {
|
||||
// best-effort cleanup: reader close must not mask the extraction failure.
|
||||
void cause;
|
||||
const cleanupFailures: {
|
||||
readonly cause: unknown;
|
||||
readonly resource: string;
|
||||
}[] = [];
|
||||
if (readerCloseAttempted && !readerClosed)
|
||||
cleanupFailures.push({
|
||||
cause,
|
||||
resource: `artifact reader for ${sourcePath}`,
|
||||
});
|
||||
await output.rollback();
|
||||
if (!readerCloseAttempted) {
|
||||
try {
|
||||
readerCloseAttempted = true;
|
||||
await reader.close();
|
||||
} catch (cleanupCause: unknown) {
|
||||
cleanupFailures.push({
|
||||
cause: cleanupCause,
|
||||
resource: `artifact reader for ${sourcePath}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (output !== undefined) {
|
||||
try {
|
||||
await output.rollback();
|
||||
} catch (cleanupCause: unknown) {
|
||||
cleanupFailures.push({
|
||||
cause: cleanupCause,
|
||||
resource: input.outputRoot,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (cleanupFailures.length > 0) {
|
||||
const observations = cleanupFailures.map(
|
||||
({ cause: cleanupCause, resource }) =>
|
||||
ArtifactReaderFailure.cleanupObservation(cleanupCause, resource),
|
||||
);
|
||||
throw ArtifactReaderFailure.withCleanup(cause, {
|
||||
reason: observations.map(({ reason }) => reason).join("; "),
|
||||
resources: [
|
||||
...new Set(observations.flatMap(({ resources }) => resources)),
|
||||
],
|
||||
});
|
||||
}
|
||||
throw cause;
|
||||
}
|
||||
};
|
||||
@@ -205,7 +245,7 @@ const createExtractionResult = (
|
||||
source_manifest_id: inventory.manifest.manifest_id,
|
||||
selected_occurrence_ids: selected
|
||||
.map(({ occurrence }) => occurrence.occurrence_id)
|
||||
.sort((left, right) => left.localeCompare(right)),
|
||||
.sort(compareCodePoints),
|
||||
files_sha256: canonicalDigest(extracted, "Artifact"),
|
||||
output_root_alias: "$OUTPUT_ROOT" as const,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user