Skip to content

fix(ext/node): do not throw error for readline.createInterface outside TTY - #32874

Merged
bartlomieju merged 11 commits into
denoland:mainfrom
KnorpelSenf:avoid-error-outside-tty
Mar 22, 2026
Merged

bartlomieju merged 11 commits into
denoland:mainfrom
KnorpelSenf:avoid-error-outside-tty

Conversation

@KnorpelSenf

Copy link
Copy Markdown
Contributor

Matches Node's behaviour by ignoring terminal: true when not inside a TTY.

Fixes #32873.

Matches Node's behaviour by ignoring `terminal: true` when not inside a
TTY.

Fixes denoland#32873.
@bartlomieju

Copy link
Copy Markdown
Member

Can you please add a test?

@KnorpelSenf

Copy link
Copy Markdown
Contributor Author

@bartlomieju I can try. I found tests/node_compat/runner/suite/test/pseudo-tty/readline-dumb-tty.js and I assume that my test should be somewhat similar. But how can I make sure that it is run outside of a TTY? Do I need to spawn a process in the same way tests/node_compat/runner/suite/test/pseudo-tty/test-set-raw-mode-reset.js does it?

@KnorpelSenf

KnorpelSenf commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor Author

We can also do something like

'use strict';
const readline = require('readline');
const Stream = require('stream');

const stream = new Stream();
let setRawModeCalled = false;

stream.isTTY = false;

stream.setRawMode = function () {
  setRawModeCalled = true;
  throw new Error('setRawMode should not be called for non-TTY streams');
};
stream.resume = function () { };
stream.pause = function () { };

const rli = readline.createInterface({
  input: stream,
  output: stream,
  terminal: true
});
rli.close();
console.log(setRawModeCalled);

and match it with a fixture

false

but then I still don't know where to place these two files, so any guidance would be appreciated

@bartlomieju

Copy link
Copy Markdown
Member

@bartlomieju I can try. I found tests/node_compat/runner/suite/test/pseudo-tty/readline-dumb-tty.js and I assume that my test should be somewhat similar. But how can I make sure that it is run outside of a TTY? Do I need to spawn a process in the same way tests/node_compat/runner/suite/test/pseudo-tty/test-set-raw-mode-reset.js does it?

If that test tests it, it's enough to add it to tests/node_compat/config.jsonc and it will be run automatically for you.

@KnorpelSenf

Copy link
Copy Markdown
Contributor Author

@bartlomieju I can try. I found tests/node_compat/runner/suite/test/pseudo-tty/readline-dumb-tty.js and I assume that my test should be somewhat similar. But how can I make sure that it is run outside of a TTY? Do I need to spawn a process in the same way tests/node_compat/runner/suite/test/pseudo-tty/test-set-raw-mode-reset.js does it?

If that test tests it, it's enough to add it to tests/node_compat/config.jsonc and it will be run automatically for you.

It does not. There is no test case that covers the bug I fixed. Which is why I'm wondering how and where to place a new test, and how to run it correctly. I can't find any documentation about this, nor do I find existing tests that cover edge cases not contained in Node's test suite.

@bartlomieju

Copy link
Copy Markdown
Member

Suggestion from Claude:

The best test uses process.stdin directly — but in spec tests, stdin is piped (not a TTY), which is exactly the failing
  condition. So the simplest test:

  __test__.jsonc:
  {
    // Regression test for https://github.com/denoland/deno/issues/32873
    // readline.createInterface with terminal: true should not throw
    // when stdin is not a TTY.
    "args": "run main.js",
    "output": "success!\n"
  }

  main.js:
  const readline = require("readline");
  readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: true,
  });
  console.log("success!");

  This works because spec tests run with piped stdin (not a TTY). Before the fix, this would throw ENOTTY. After the fix, it
  succeeds. It's the exact reproduction from the issue.

@KnorpelSenf

Copy link
Copy Markdown
Contributor Author

@bartlomieju done, please TAL

@KnorpelSenf

Copy link
Copy Markdown
Contributor Author

Oh I just saw your comment. Glad that the LLM agrees with my repro, but that still does not tell me where to place that test.

@bartlomieju

Copy link
Copy Markdown
Member

Anywhere in tests/specs/run/ would be fine. You can create a new directory

@KnorpelSenf

Copy link
Copy Markdown
Contributor Author

Ahhhh right, that was the info I'm missing, thank you! I'll remove my test again and add a proper one there. Thanks!

@KnorpelSenf

KnorpelSenf commented Mar 21, 2026 •

Copy link
Copy Markdown
Contributor Author
  1. If we skip setRawMode like in my initial fix, the node tests fail because they expect setRawMode to be called: https://github.com/denoland/deno/actions/runs/23377733632/job/68012713998
  2. If we call setRawMode unconditionally to make these tests pass, we change the behaviour of Deno.stdin.setRaw which probably undesired: f487e32

@bartlomieju how should we move forward here?

The readline [kSetRawMode] should match Node.js and only check if
setRawMode is a function (not check isTTY). The real issue is that
Deno's process.stdin.setRawMode is always defined (unlike Node where
it only exists on TTY stdin), so it needs to guard against calling
Deno.stdin.setRaw() on non-TTY streams.

This fixes the original ENOTTY error while keeping the node_compat
test-readline-set-raw-mode.js passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju

Copy link
Copy Markdown
Member

I pushed a fix. Here's the analysis:

Root cause: The problem isn't in readline's [kSetRawMode] — it's in process.stdin.setRawMode. In Node.js, process.stdin.setRawMode only exists when stdin is a TTY. In Deno, it's always defined (in _process/streams.mjs), and it unconditionally calls Deno.stdin.setRaw() which throws ENOTTY on non-TTY streams.

The fix (277fbc3):

  1. Revert the isTTY check in readline — [kSetRawMode] should match Node.js and only check typeof this.input.setRawMode === "function". This keeps test-readline-set-raw-mode.js passing (that test uses a custom stream with setRawMode but no isTTY).

  2. Guard process.stdin.setRawMode — check io.stdin?.isTerminal() before calling io.stdin.setRaw(). This prevents the ENOTTY throw when stdin is piped/not a terminal.

This way both the original bug (#32873) and the node_compat test pass.

@KnorpelSenf

Copy link
Copy Markdown
Contributor Author

Sure, that works!

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@bartlomieju
bartlomieju merged commit ac659a1 into denoland:main Mar 22, 2026
111 checks passed
@KnorpelSenf
KnorpelSenf deleted the avoid-error-outside-tty branch March 22, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENOTTY error for readline.createInterface

2 participants