GeezerVison is showing github.com — Input from TTY is strange · Issue #2504 · nodejs/node
Input from TTY is strange #2504Labels readlineIssues and PRs related to the built-in readline module.streamIssues and PRs related to Node.js streams.windowsIssues and PRs related to the Windows platform. DescriptionI tried read from STDIN by fs.readSync that is given process.stdin.fd.
For example:
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));
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.fd in non-Win because fs.readSync can't read it.
Therefore, process.stdin of v2.3.2+ might have problem. (not fs.readSync) Activityadded fsIssues and PRs related to file-system APIs and the fs module. windowsIssues and PRs related to the Windows platform. on Aug 23, 2015brendanashworth commented 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 ? anseki commented on Aug 23, 2015 If 8cee8f5 made this problem, I think that windows label is not suitably, because I didn't use process.stdin in non-Win. And in case that doesn't use process.stdin (open('CONIN$') instead of it) in Windows is no problem. mscdex commented on Aug 23, 2015 From what I can tell it has to do with process.stdin taking 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 ready is displayed. mscdex commented on Aug 23, 2015 You can also replace process.stdin.fd in the original code with just 0 and 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. anseki commented on Aug 24, 2015 Thank you, but... I tried your solution, and it did not read first line.
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 0 instead of process.stdin.fd. It works fine before accessing to process.stdin, but it doesn't read first line after accessing to process.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 fd is 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.stdin might 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.stdin makes problem, and it should not so. (this is not designed behavior, right?) orangemocha commented on Sep 9, 2015 @nodejs/streams : is it legal to use fs.readSync to read from process.stdin, which in this case is not a file? calvinmetcalf commented on Sep 9, 2015 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? anseki commented on Sep 9, 2015 It seems that programmers use read/readSync with STDIN to get the user's input easily instead of process.stdin.on('data'). Also my module readlineSync tries readSync.
https://www.google.com/search?q=nodejs+read+from+stdin
This 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 | script etc) anseki commented on Oct 17, 2015 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:
In io.js v2.3.2+, strange second line is shown:
Node.js v4.2.1 also returns same result:
anseki commented on Oct 17, 2015 It seems that REPL also is affected by this problem.
I typed a first 1 and pushed the Enter key.
In io.js v2.3.1-, no problem:
In io.js v2.3.2+, strange second 1 is shown:
Node.js v4.2.1 also returns same result:
changed the title [-]fs.readSync of v2.3.2+ can't read first line from STDIN[/-] [+]Input from STDIN is strange[/+] on Oct 17, 2015changed the title [-]Input from STDIN is strange[/-] [+]Input from TTY is strange[/+] on Oct 17, 2015anseki commented 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: bar
E:\test>node
> 1
1
1
> 2
2
>
silverwind commented on Oct 22, 2015 Filed #3490 for the revert. I could use some assistence with a regression test for this. anseki commented on Jun 4, 2016 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.');
}
MetadataMetadatareadlineIssues and PRs related to the built-in readline module.streamIssues and PRs related to Node.js streams.windowsIssues and PRs related to the Windows platform. DevelopmentNo branches or pull requestsIssue actions
|