Skip to content

Commit 29cd236

Browse files
committed
fix(cli-core): show the cursor again when an exit cuts a spinner short
1 parent f1f6868 commit 29cd236

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎packages/cli-core/src/startup/frame.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,27 @@ export function createIndicator(
130130
frame++;
131131
};
132132

133+
// An exit with the spinner still drawing (`process.exit`, an uncaught error)
134+
// never reaches `end`. A signal death skips this too: the signal handler
135+
// shows the cursor itself.
136+
const showCursorOnExit = () => write(SHOW_CURSOR);
137+
133138
const end = (line: string | undefined) => {
134139
if (timer === undefined) return;
135140
clearInterval(timer);
136141
timer = undefined;
137142
erase();
138143
if (line !== undefined) write(`${line}\n`);
139144
write(SHOW_CURSOR);
145+
process.off("exit", showCursorOnExit);
140146
};
141147

142148
return {
143149
start(message) {
144150
if (timer !== undefined) return;
145151
setText(message);
146152
write(HIDE_CURSOR);
153+
process.on("exit", showCursorOnExit);
147154
if (options.guide ?? true) write(`${dim(S_BAR)}\n`);
148155
// The first frame comes on the first tick, so a step that ends at once
149156
// leaves only its done line.

‎packages/cli-core/src/startup/signals.subprocess.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,50 @@ describe.skipIf(process.platform === "win32")("a signal with a child process run
124124
);
125125
});
126126

127+
/**
128+
* Leave the process by `path` while a spinner is drawing on a terminal, and
129+
* return what it wrote there.
130+
*/
131+
async function leaveMidSpinner(path: string): Promise<string> {
132+
const source = `
133+
const { registerSignalHandlers } = await import(${JSON.stringify(SIGNALS_MODULE)});
134+
const { createIndicator } = await import(${JSON.stringify(`${import.meta.dirname}/frame.ts`)});
135+
process.stderr.isTTY = true;
136+
registerSignalHandlers();
137+
createIndicator((chunk) => process.stderr.write(chunk)).start("Working");
138+
setTimeout(() => { ${path}; }, 150);
139+
setTimeout(() => process.exit(99), 10_000); // guard against a hang
140+
`;
141+
const { CLERK_CLI_NO_SIGNAL_RERAISE: _suppressed, ...cleanEnv } = process.env;
142+
const proc = Bun.spawn(["bun", "-e", source], {
143+
stdout: "ignore",
144+
stderr: "pipe",
145+
env: { ...cleanEnv, CLERK_TELEMETRY_DISABLED: "1" },
146+
});
147+
const stderr = await new Response(proc.stderr).text();
148+
await proc.exited;
149+
return stderr;
150+
}
151+
152+
describe.skipIf(process.platform === "win32")("the cursor a spinner hid", () => {
153+
test.each([
154+
'process.kill(process.pid, "SIGINT")',
155+
'process.kill(process.pid, "SIGTERM")',
156+
'process.kill(process.pid, "SIGHUP")',
157+
"process.exit(1)",
158+
'throw new Error("boom")',
159+
])(
160+
"is shown again after %s",
161+
async (path) => {
162+
const stderr = await leaveMidSpinner(path);
163+
164+
expect(stderr).toContain("\x1b[?25l");
165+
expect(stderr.lastIndexOf("\x1b[?25h")).toBeGreaterThan(stderr.lastIndexOf("\x1b[?25l"));
166+
},
167+
15_000,
168+
);
169+
});
170+
127171
describe("Ctrl-C as the shell sees it", () => {
128172
test("an interrupted operation dies from SIGINT rather than exiting 130", async () => {
129173
const { signalCode } = await runInterrupted("work");

0 commit comments

Comments
 (0)