Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix(cli): release the cache lock when a failed Windows build is already gone

youxi79816d79b6698f0
diff --git a/bin/utils/shell.ts b/bin/utils/shell.ts
index 9c14f51c..7e1e0d80 100644
--- a/bin/utils/shell.ts
+++ b/bin/utils/shell.ts
@@ -15,10 +15,17 @@ export interface ShellCommand {
async function terminateBuildTree(pid: number): Promise<void> {
if (process.platform === 'win32') {
- await execa('taskkill', ['/pid', String(pid), '/T', '/F'], {
- timeout: 5000,
- windowsHide: true,
- });
+ try {
+ await execa('taskkill', ['/pid', String(pid), '/T', '/F'], {
+ timeout: 5000,
+ windowsHide: true,
+ });
+ } catch (error) {
+ // 128 is "process not found": a failed build has usually exited before
+ // it rejects. Treating that as unconfirmed kept the cache lock, and
+ // every later build refused to start until it was deleted by hand.
+ if ((error as { exitCode?: number }).exitCode !== 128) throw error;
+ }
return;
}
diff --git a/dist/cli.js b/dist/cli.js
index 16299672..8dc208da 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -586,10 +586,19 @@ async function enterBuildWorkspace() {
async function terminateBuildTree(pid) {
if (process.platform === 'win32') {
- await execa('taskkill', ['/pid', String(pid), '/T', '/F'], {
- timeout: 5000,
- windowsHide: true,
- });
+ try {
+ await execa('taskkill', ['/pid', String(pid), '/T', '/F'], {
+ timeout: 5000,
+ windowsHide: true,
+ });
+ }
+ catch (error) {
+ // 128 is "process not found": a failed build has usually exited before
+ // it rejects. Treating that as unconfirmed kept the cache lock, and
+ // every later build refused to start until it was deleted by hand.
+ if (error.exitCode !== 128)
+ throw error;
+ }
return;
}
const killGroup = (signal) => {
diff --git a/tests/unit/shell.test.ts b/tests/unit/shell.test.ts
index fd559873..2ba0cad7 100644
--- a/tests/unit/shell.test.ts
+++ b/tests/unit/shell.test.ts
@@ -10,10 +10,22 @@ vi.mock('execa', async (importOriginal) => {
return { ...actual, execa: vi.fn(actual.execa) };
});
+vi.mock('@/utils/build-workspace', async (importOriginal) => {
+ const actual =
+ await importOriginal<typeof import('@/utils/build-workspace')>();
+ return {
+ ...actual,
+ preventBuildWorkspaceCleanup: vi.fn(actual.preventBuildWorkspaceCleanup),
+ };
+});
+
import { execa } from 'execa';
import { isMachineMode } from '@/utils/output';
import { shellExec } from '@/utils/shell';
-import { beginBuildCancellation } from '@/utils/build-workspace';
+import {
+ beginBuildCancellation,
+ preventBuildWorkspaceCleanup,
+} from '@/utils/build-workspace';
const tempDirs: string[] = [];
afterEach(async () => {
@@ -98,6 +110,43 @@ describe('structured process execution', () => {
).rejects.toThrow('Command timed out after 50ms');
});
+ it.each([
+ [128, 0],
+ [1, 1],
+ ])(
+ 'a failed Windows build whose taskkill exits %i keeps the workspace %i times',
+ async (taskkillExit, kept) => {
+ const actual = await vi.importActual<typeof import('execa')>('execa');
+ vi.mocked(execa).mockImplementation(((file: string, ...rest: any[]) =>
+ file === 'taskkill'
+ ? Promise.reject(
+ Object.assign(new Error('taskkill failed'), {
+ exitCode: taskkillExit,
+ }),
+ )
+ : (actual.execa as any)(file, ...rest)) as any);
+ const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
+ Object.defineProperty(process, 'platform', { value: 'win32' });
+ const endCancellation = beginBuildCancellation();
+ try {
+ await expect(
+ shellExec({
+ executable: process.execPath,
+ args: ['-e', 'process.exit(7)'],
+ }),
+ ).rejects.toThrow('Exit code: 7');
+ expect(vi.mocked(execa).mock.calls.map((call) => call[0])).toContain(
+ 'taskkill',
+ );
+ expect(preventBuildWorkspaceCleanup).toHaveBeenCalledTimes(kept);
+ } finally {
+ endCancellation();
+ Object.defineProperty(process, 'platform', platform);
+ vi.mocked(execa).mockImplementation(actual.execa as any);
+ }
+ },
+ );
+
it.skipIf(process.platform === 'win32')(
'stops compiler descendants before rejecting a timed-out build',
async () => {