diff --git a/src/utils/terminalShellEnvironment.test.ts b/src/utils/terminalShellEnvironment.test.ts index c7b52b61..fdf39e27 100644 --- a/src/utils/terminalShellEnvironment.test.ts +++ b/src/utils/terminalShellEnvironment.test.ts @@ -101,4 +101,42 @@ describe('terminal shell environment', () => { expect(env).toBeNull() }) + + it('does not capture shell env when the current process owns an interactive TTY', async () => { + const stdinDescriptor = Object.getOwnPropertyDescriptor(process.stdin, 'isTTY') + const stdoutDescriptor = Object.getOwnPropertyDescriptor(process.stdout, 'isTTY') + + Object.defineProperty(process.stdin, 'isTTY', { + configurable: true, + value: true, + }) + Object.defineProperty(process.stdout, 'isTTY', { + configurable: true, + value: true, + }) + + try { + const shellPath = path.join(tmpDir, 'zsh') + await writeFakeZsh(shellPath) + + const env = await getTerminalShellEnvironment({ + HOME: tmpDir, + SHELL: shellPath, + PATH: '/usr/bin:/bin', + }) + + expect(env).toBeNull() + } finally { + if (stdinDescriptor) { + Object.defineProperty(process.stdin, 'isTTY', stdinDescriptor) + } else { + delete (process.stdin as { isTTY?: boolean }).isTTY + } + if (stdoutDescriptor) { + Object.defineProperty(process.stdout, 'isTTY', stdoutDescriptor) + } else { + delete (process.stdout as { isTTY?: boolean }).isTTY + } + } + }) }) diff --git a/src/utils/terminalShellEnvironment.ts b/src/utils/terminalShellEnvironment.ts index f13367a8..8ee4b629 100644 --- a/src/utils/terminalShellEnvironment.ts +++ b/src/utils/terminalShellEnvironment.ts @@ -104,7 +104,10 @@ async function captureTerminalShellEnvironment( ): Promise | null> { if ( process.platform === 'win32' || - isEnvTruthy(baseEnv.CC_HAHA_DISABLE_TERMINAL_SHELL_ENV) + isEnvTruthy(baseEnv.CC_HAHA_DISABLE_TERMINAL_SHELL_ENV) || + // Direct terminal launches already inherit shell env; spawning an + // interactive login shell here can interfere with the active TTY. + (process.stdin.isTTY && process.stdout.isTTY) ) { return null }