From d70fe1dd5a839211d01705fc178c15d170aa27c5 Mon Sep 17 00:00:00 2001 From: Aditya Jain Date: Tue, 25 Aug 2026 01:19:30 -0700 Subject: [PATCH] fix(cli): --yes mode requires a TTY when a request's default is falsy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CliIoHost.resolveRequest's auto-respond path checked `if (msg.defaultResponse)` before returning the default in --yes/autoRespond mode. A falsy-but-defined default (empty string '', or 0) fails that truthy check, so the code falls through past the auto-respond block to the isTTY check and throws TtyNotAttached instead of returning the default — even though --yes was passed specifically to avoid needing a TTY. This is user-visible today: the resource-import prompt (importer.ts) and the SDK MFA-token prompt (awscli-compatible.ts) both pass an empty-string defaultResponse, so `cdk import --yes` (or any non-interactive/CI run hitting these prompts) aborts with TtyNotAttached instead of silently taking the default, unlike NonInteractiveIoHost's equivalent codepath which returns the default unconditionally. Fixes #1891 --- .../aws-cdk/lib/cli/io-host/cli-io-host.ts | 2 +- .../test/cli/io-host/cli-io-host.test.ts | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk/lib/cli/io-host/cli-io-host.ts b/packages/aws-cdk/lib/cli/io-host/cli-io-host.ts index 768c76c7d..90c13b796 100644 --- a/packages/aws-cdk/lib/cli/io-host/cli-io-host.ts +++ b/packages/aws-cdk/lib/cli/io-host/cli-io-host.ts @@ -1003,7 +1003,7 @@ export class CliIoHost implements IIoHost, ObservableIoHost { } // respond with the default for all other messages - if (msg.defaultResponse) { + if (msg.defaultResponse !== undefined) { await this.writeMessage({ ...msg, message: `${chalk.cyan(msg.message)} (auto-responded with default: ${util.format(msg.defaultResponse)})`, diff --git a/packages/aws-cdk/test/cli/io-host/cli-io-host.test.ts b/packages/aws-cdk/test/cli/io-host/cli-io-host.test.ts index d03d1af2a..087468280 100644 --- a/packages/aws-cdk/test/cli/io-host/cli-io-host.test.ts +++ b/packages/aws-cdk/test/cli/io-host/cli-io-host.test.ts @@ -1336,6 +1336,37 @@ describe('CliIoHost', () => { })); expect(response).toBe('foobar'); }); + + test('falsy defaults (empty string, zero) are auto-responded instead of requiring a TTY', async () => { + const nonTtyAutoRespondingIoHost = CliIoHost.instance({ + logLevel: 'trace', + autoRespond: true, + isCI: false, + isTTY: false, + }, true); + + // empty string default + const stringResponse = await nonTtyAutoRespondingIoHost.requestResponse(plainMessage({ + time: new Date(), + level: 'info', + action: 'synth', + code: 'CDK_TOOLKIT_I5060', + message: 'test message', + defaultResponse: '', + })); + expect(stringResponse).toBe(''); + + // zero default + const numberResponse = await nonTtyAutoRespondingIoHost.requestResponse(plainMessage({ + time: new Date(), + level: 'info', + action: 'synth', + code: 'CDK_TOOLKIT_I0001', + message: 'test message', + defaultResponse: 0, + })); + expect(numberResponse).toBe(0); + }); }); describe('non-promptable data', () => {