Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions openless-all/app/scripts/ci-cargo-fixture-cleanup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { rmSync } from 'node:fs';

export function withCargoFixtureCleanup(directory, run, remove = rmSync) {
let failed = false;
let failure;
try {
return run();
} catch (error) {
failed = true;
failure = error;
throw error;
} finally {
try {
// Retry the observed Windows busy removals only in this Cargo fixture.
// Use Node's bounded retry budget; exhaustion still throws.
remove(directory, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} catch (cleanupError) {
if (failed) {
throw new AggregateError(
[failure, cleanupError],
'Cargo fixture failed and its temporary directory could not be removed',
{ cause: failure },
);
}
throw cleanupError;
}
}
}
7 changes: 3 additions & 4 deletions openless-all/app/scripts/ci-performance.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { load } from 'js-yaml';
import { withCargoFixtureCleanup } from './ci-cargo-fixture-cleanup.mjs';

const workflow = (name) =>
load(readFileSync(new URL(`../../../.github/workflows/${name}`, import.meta.url), 'utf8'));
Expand Down Expand Up @@ -99,7 +100,7 @@ assert.equal(
// macOS exposes /private/var through /var. Use one canonical path for relative
// and absolute manifests so Cargo does not fingerprint two names for one source.
const cargoScratch = realpathSync(mkdtempSync(join(tmpdir(), 'openless-cargo-reuse-')));
try {
withCargoFixtureCleanup(cargoScratch, () => {
for (const path of ['host/src', 'shared/src', 'backend/src']) {
mkdirSync(join(cargoScratch, path), { recursive: true });
}
Expand Down Expand Up @@ -205,9 +206,7 @@ ci-shared = { path = "../shared" }
assert.ifError(broken.error);
assert.notEqual(broken.status, 0);
assert.match(broken.stderr, /production diagnostics checked/);
} finally {
rmSync(cargoScratch, { recursive: true, force: true });
}
});

// Run the real Windows build shell with only npm stubbed. Check artifact-flow
// decisions, not the spelling of the commands: one compile, optional rebundle,
Expand Down
66 changes: 66 additions & 0 deletions openless-all/app/scripts/windows-cargo-fixture-cleanup.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import assert from 'node:assert/strict';
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { withCargoFixtureCleanup } from './ci-cargo-fixture-cleanup.mjs';

// This regression never starts Cargo, a compiler, or any other child process.
const root = mkdtempSync(join(tmpdir(), 'openless-cargo-cleanup-contract-'));
const fixture = (name) => {
const directory = join(root, name);
mkdirSync(directory);
writeFileSync(join(directory, 'fixture.txt'), 'fixture');
return directory;
};
try {
const success = fixture('success');
assert.equal(withCargoFixtureCleanup(success, () => 42), 42);
assert.equal(existsSync(success), false);

const primary = new Error('fixture compilation or assertion failed');
const failed = fixture('primary-failure');
assert.throws(
() =>
withCargoFixtureCleanup(failed, () => {
throw primary;
}),
(error) => error === primary,
);
assert.equal(existsSync(failed), false, 'the original failure must still clean its fixture');

const cleanupError = Object.assign(new Error('fixture removal busy'), { code: 'EBUSY' });
const cannotRemove = (directory, options) => {
assert.equal(directory.startsWith(root), true);
assert.deepEqual(options, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
// Simulate rmSync exhausting its own retry budget; do not emulate Windows locks.
throw cleanupError;
};
const cleanupOnly = fixture('cleanup-failure');
assert.throws(
() => withCargoFixtureCleanup(cleanupOnly, () => 42, cannotRemove),
(error) => error === cleanupError,
);
assert.equal(existsSync(cleanupOnly), true);

const both = fixture('both-failures');
assert.throws(
() =>
withCargoFixtureCleanup(
both,
() => {
throw primary;
},
cannotRemove,
),
(error) => {
assert.ok(error instanceof AggregateError);
assert.equal(error.cause, primary);
assert.deepEqual(error.errors, [primary, cleanupError]);
return true;
},
);
assert.equal(existsSync(both), true);
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
console.log('Windows Cargo fixture cleanup contract passed (no compilation)');
Loading