Skip to content

[Windows] Hooks create flashing console windows on every tool execution #32140

Closed as duplicate

Description

@Yang642514

问题描述

在 Windows 平台上使用 Claude Code 时,每次工具操作(Read, Edit, Write, Bash 等)都会触发 hooks,导致命令行窗口短暂闪现。在高频率操作时,这些闪现的窗口严重影响用户体验。

复现步骤

  1. 在 Windows 11 上安装 Claude Code(使用官方推荐安装方式)
  2. 配置任意 hooks(如 SecurityValidator, AlgorithmTracker 等)
  3. 执行任何工具操作(如 Read 文件、Edit 文件)
  4. 观察结果:每次操作都会看到黑色的命令行窗口短暂闪现
  5. 预期结果:hooks 应该在后台静默执行,不显示任何窗口

环境信息

  • 操作系统: Windows 11 Home China 10.0.26200
  • Claude Code 版本: 官方最新版本(通过官方推荐方式安装)
  • Bun 版本: 1.3.5
  • 安装路径: C:\Users\30305\.bun\bin\bun.exe

根本原因分析

问题根源

Claude Code 在创建子进程执行 hooks 时,在 Windows 平台上使用了 CREATE_NEW_CONSOLE 标志,导致每次执行都创建新的控制台窗口。

建议的修复方案

在 Node.js 的 child_process.spawn 或 child_process.exec 中添加 windowsHide: true 选项:

// 建议修改为
const child = spawn('bun', ['run', hookPath], {
  windowsHide: true,  // 这个标志可以防止创建新窗口
  stdio: ['pipe', 'pipe', 'pipe']
});

参考文档

尝试过的临时方案(均不理想)

❌ 方案 1: VBScript 包装器

"command": "cscript //NoLogo //B C:\path\to\run-bun-hidden.vbs C:\path\to\hook.ts"

问题: cscript.exe 本身仍会显示窗口,且增加了 +40ms 的性能开销

❌ 方案 2: PowerShell 包装器

"command": "powershell -WindowStyle Hidden -Command \"bun run xxx.hook.ts\""

问题: PowerShell 启动慢(+150ms),严重影响性能

影响

用户体验影响

  • 视觉干扰: 频繁的窗口闪现分散注意力
  • 专业性: 在专业环境中影响产品形象
  • 可用性: 对于需要长时间使用 Claude Code 的用户,这种闪烁会导致视觉疲劳

性能影响

  • 频繁的窗口创建/销毁: 每次操作都创建和销毁控制台窗口,增加系统开销

期望的解决方案

  1. 短期: 在 Windows 平台上默认使用 windowsHide: true 创建子进程
  2. 长期: 提供配置选项让用户可以选择是否显示 hooks 执行的调试窗口

额外信息

这是 Windows 平台的特定问题。在 Linux/macOS 上不存在此问题,因为 Unix 系统的进程创建机制不需要控制台窗口。

建议参考其他跨平台工具(如 VS Code, Git)在 Windows 上的实现方式,它们都能在后台执行脚本而不显示控制台窗口。

Activity

  1. added
    bug
    platform:windows
    has repro
    on Mar 8, 2026
  2. github-actions commented on Mar 8, 2026

    @github-actions

    Found 3 possible duplicate issues:

    1. Windows: plugin hooks spawn visible cmd.exe/conhost.exe windows despite windowsHide:true #27115
    2. Windows: Hook commands cause brief console window flash #19012
    3. [FEATURE] Add windowsHide option for hooks on Windows #17230

    This issue will be automatically closed as a duplicate in 3 days.

    • If your issue is a duplicate, please close it and 👍 the existing issue instead
    • To prevent auto-closure, add a comment or 👎 this comment

    🤖 Generated with Claude Code

  3. added
    duplicate
    and removed
    bug
    has repro
    platform:windows
    on Mar 12, 2026
  4. github-actions commented on Mar 12, 2026

    @github-actions

    This issue has been automatically closed as a duplicate of #27115.

    If this is incorrect, please re-open this issue or create a new one.

    🤖 Generated with Claude Code

  5. github-actions commented on Mar 21, 2026

    @github-actions

    This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

  6. locked as resolved and limited conversation to collaborators on Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions