Skip to content

Windows compatibility: stdin reading fails silently with fs.openSync(0, 'r') #1

@TC407-api

Description

@TC407-api

Problem

The hooks fail silently on Windows because fs.openSync(0, 'r') doesn't work for reading stdin on Windows. The file descriptor approach throws an error:

The "path" argument must be of type string or an instance of Buffer or URL. Received type number (0)

This causes the hook to receive empty input and approve all commands - effectively disabling all damage control protection on Windows systems.

Environment

  • Windows 11
  • Node.js v24.3.0
  • Claude Code CLI

Root Cause

The synchronous stdin reading pattern used in the hooks:

const fd = fs.openSync(0, 'r');
while ((bytesRead = fs.readSync(fd, buf, 0, BUFSIZE)) > 0) {
  input += buf.toString('utf8', 0, bytesRead);
}

This Unix file descriptor approach (fd 0 = stdin) doesn't work on Windows.

Solution

Use async stdin reading with process.stdin:

function main() {
  let input = "";

  process.stdin.setEncoding("utf8");
  process.stdin.on("readable", () => {
    let chunk;
    while ((chunk = process.stdin.read()) !== null) {
      input += chunk;
    }
  });
  process.stdin.on("end", () => {
    processInput(input);
  });
  process.stdin.on("error", () => {
    // Handle no stdin gracefully
    output("approve");
  });
}

This works cross-platform (tested on Windows 11, should work on Mac/Linux too).

Additional Notes

Great video and repo! I integrated your patterns.yaml approach into my existing pre-tool-batch.js hook and it's working well now with the stdin fix. The comprehensive AWS/GCP/Firebase/Docker/K8s patterns are really valuable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions