Repository navigation
Input from TTY is strange #2504
Description
Activity
- addedfsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.windowsIssues and PRs related to the Windows platform.Issues and PRs related to the Windows platform.
on Aug 23, 2015 This is seems to be a regression that could have been introduced by 8cee8f5 (didn't do
git bisect, just layman's bisecting). @nodejs/platform-windows ?If 8cee8f5 made this problem, I think that
windowslabel is not suitably, because I didn't useprocess.stdinin non-Win. And in case that doesn't useprocess.stdin(open('CONIN$')instead of it) in Windows is no problem.From what I can tell it has to do with
process.stdintaking so long to initialize when first accessed.For example, if you use:
process.stdin; console.log('ready'); var fs = require('fs'), buffer = new Buffer(1024), fd = process.platform === 'win32' ? process.stdin.fd : fs.openSync('/dev/tty', 'r'), readSize = fs.readSync(fd, buffer, 0, 1024); console.log('INPUT: ' + buffer.toString('utf8', 0, readSize));
Then the first line will get read as expected if you type it after
readyis displayed.You can also replace
process.stdin.fdin the original code with just0and it will get the first line too.I'm trying this all on Windows, so I haven't looked at it on *nix. Also, reverting 8cee8f5 does not affect this particular issue.
Thank you, but... I tried your solution, and it did not read first line.
ready foo bar INPUT: bar
I tried your sample code on Windows 8.1 64bit with iojs v2.3.1, v2.3.2 and v3.1.0. Of course v2.3.1 works fine.
Also I tried
0instead ofprocess.stdin.fd. It works fine before accessing toprocess.stdin, but it doesn't read first line after accessing toprocess.stdin.For example, this code doesn't read first line:
var fs = require('fs'), buffer = new Buffer(1024), fd = process.platform === 'win32' ? process.stdin.fd : fs.openSync('/dev/tty', 'r'), //readSize = fs.readSync(fd, buffer, 0, 1024); readSize = fs.readSync(0, buffer, 0, 1024); console.log('INPUT: ' + buffer.toString('utf8', 0, readSize));
Yes I know that
fdis meaningless now, then this works fine:var fs = require('fs'), buffer = new Buffer(1024), //fd = process.platform === 'win32' ? process.stdin.fd : fs.openSync('/dev/tty', 'r'), //readSize = fs.readSync(fd, buffer, 0, 1024); readSize = fs.readSync(0, buffer, 0, 1024); console.log('INPUT: ' + buffer.toString('utf8', 0, readSize));
But
process.stdinmight be used at a place I don't know. (e.g. other packages)
That is, this code doesn't read first line:process.stdin; var fs = require('fs'), buffer = new Buffer(1024), readSize = fs.readSync(0, buffer, 0, 1024); console.log('INPUT: ' + buffer.toString('utf8', 0, readSize));
I think that a point is that the programers don't know that
process.stdinmakes problem, and it should not so. (this is not designed behavior, right?)@nodejs/streams : is it legal to use fs.readSync to read from process.stdin, which in this case is not a file?
so is the issue that references to process.stdin initiate the readable stream, which does some buffering and that buffering prevents somebody from reading stdin like it was a file?
It seems that programmers use
read/readSyncwith STDIN to get the user's input easily instead ofprocess.stdin.on('data'). Also my module readlineSync triesreadSync.
https://www.google.com/search?q=nodejs+read+from+stdinThis problem occurs when it reads from TTY. It seems no problem when it reads from the file or pipe. (e.g.
script < file,echo foo | scriptetc)It seems that the Readline module also is affected by this problem.
For example:
var readline = require('readline').createInterface({ input: process.stdin, output: process.stdout }); readline.question('> ', function(answer) { console.log('INPUT: ', answer); readline.close(); });
In io.js v2.3.1-, no problem:
> foo INPUT: fooIn io.js v2.3.2+, strange second line is shown:
> foo foo INPUT: fooNode.js v4.2.1 also returns same result:
> foo foo INPUT: fooIt seems that REPL also is affected by this problem.
I typed a first
1and pushed the Enter key.
In io.js v2.3.1-, no problem:E:\test>node > 1 1 >In io.js v2.3.2+, strange second
1is shown:E:\test>node > 1 1 1 >Node.js v4.2.1 also returns same result:
E:\test>node > 1 1 1 >- changed the title
[-]fs.readSync of v2.3.2+ can't read first line from STDIN[/-][+]Input from STDIN is strange[/+]on Oct 17, 2015 - changed the title
[-]Input from STDIN is strange[/-][+]Input from TTY is strange[/+]on Oct 17, 2015 It seems that only the first line of both readline module and REPL is strange, like readSync.
#2504 (comment)For example:
> foo foo INPUT: foo > bar INPUT: barE:\test>node > 1 1 1 > 2 2 >6 remaining items
Filed #3490 for the revert. I could use some assistence with a regression test for this.
- added a commit that references this issue
on Nov 7, 2015 - added 2 commits that reference this issue
on Nov 10, 2015 - added a commit that references this issue
on Dec 4, 2015 - added 2 commits that reference this issue
on Dec 17, 2015 Related to #5384
This issue came again, and it was fixed in v6.2.0.
Then, the versions of Node.js that have the problem:- VERSION >= 2.3.2 && VERSION < 4.2.4
- VERSION >= 5.0.0 && VERSION < 5.1.0
- VERSION >= 5.6.0 && VERSION < 6.2.0
Easy check:
var verNum = (function(ver) { var nums = ver.replace(/^\D+/, '').split('.'); var verNum = 0; if ((nums[0] = +nums[0])) { verNum += nums[0] * 10000; } if ((nums[1] = +nums[1])) { verNum += nums[1] * 100; } if ((nums[2] = +nums[2])) { verNum += nums[2]; } return verNum; })(process.version), hasProblem = verNum >= 20302 && verNum < 40204 || verNum >= 50000 && verNum < 50100 || verNum >= 50600 && verNum < 60200; if (hasProblem) { console.log('TTY has problem.'); } else { console.log('TTY has NO problem.'); }
- added a commit that references this issue
on Jul 27, 2026
I tried read from STDIN by
fs.readSyncthat is givenprocess.stdin.fd.For example:
In v2.3.1-, no problem:
In v2.3.2+, first line is ignored, and second line is accepted:
This problem occurs in only Windows + iojs v2.3.2+.
I don't use
process.stdin.fdin non-Win becausefs.readSynccan't read it.Therefore,
process.stdinof v2.3.2+ might have problem. (notfs.readSync)