Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stdin.js #58

Closed
wants to merge 1 commit into from
Closed

fix stdin.js #58

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented Sep 15, 2015

get expectPipeIn by the right way

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

Nice catch! Do you know the support of Stat#isFIFO? I couldn’t find it anywhere...

@anko
Copy link

anko commented Sep 15, 2015

Is this really correct?

Old method:

$ node -e "console.log(\!process.stdin.isTTY)" <<< "hello"
true
$ node -e "console.log(\!process.stdin.isTTY)"            
false

Proposed method:

$ node -e "console.log(require('fs').fstatSync(0).isFIFO())" <<< "hello"
false
$ node -e "console.log(require('fs').fstatSync(0).isFIFO())"            
false

I think our tests don't touch this, hence why Travis shows OK.

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

@yukkurisinai @anko Interesing, they do seem to work for echo "hello" | node ex.js, where ex.js looks as follows:

console.log(!process.stdin.isTTY);
console.log(require('fs').fstatSync(0).isFIFO())

Yields:

true
true

But for node ex.js <<< "hello":

true
false

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

@yukkurisinai What was the case where this failed?

@ghost
Copy link
Author

ghost commented Sep 15, 2015

when I use mdast in drone.io, !process.stdin.isTTY is always true :(
In addition to this, If I write gulpfile.js as below, it output true.

var shell = require("gulp-shell");
gulp.task("foobar", shell.task("node -e \"console.log(\!process.stdin.isTTY)\""));

when I use mdast by gulp-shell, the same thing happened at line 24.

gulp.task("lint-md", shell.task("mdast -u mdast-lint *.md --frail"));

@ghost
Copy link
Author

ghost commented Sep 15, 2015

In the first place, mdast check program.files.length at line 37. why must mdast check !process.stdin.isTTY ?

@ghost
Copy link
Author

ghost commented Sep 15, 2015

sorry to my poor english :(

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

@yukkurisinai because when not piping in, passing no files is an error. When piping in, passing files is an error!

@ghost
Copy link
Author

ghost commented Sep 15, 2015

ok, I understood... I thought that when passing files, mdast can ignore expectPipeIn.

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

Okay, I tested a lot, it has to do with subprocesses. And I couldn’t find a method, either !process.stdin.isTTY, require('fs').fstatSync(0).isFIFO(), or !require('tty').isatty(process.stdin) which detects it everywhere...

@ghost
Copy link
Author

ghost commented Sep 15, 2015

umm... If you can ignore <<< , I think that require('fs').fstatSync(0).isFIFO() is the best way.

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

But I’d rather not ignore it though!

@ghost
Copy link
Author

ghost commented Sep 15, 2015

ok, I investigation about "<<<". please wait a moment to conclude plz...

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

Yeah I’m testing it out too. Thanks for helping!

@ghost
Copy link
Author

ghost commented Sep 15, 2015

if user use "<<<", isFIFO can't catch pipe. so, I wrote a code.

var expectPipeIn = !process.stdin.isTTY || fs.fstatSync(0).isFIFO();

How is this?

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

stdin: undefined false false  // `<<<` pipe in bash
stdin: undefined false false  // `<<<` pipe in gulp
stdin: undefined true false   // `|` pipe in bash
stdin: undefined true false   // `|` pipe in gulp
stdin: true      false true   // `files` in bash
stdin: undefined false false  // `files` in gulp

As you can see from line 6, it’s exactly the same as line 1 and 2 (meaning it
can’t be detected).

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

@anko How standard is <<< it seems pretty hidden?

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

I was able to get almost all cases to work:

mdast # throws, correctly
mdast readme.md
echo 'hello' | mdast
mdast <<< hello

...and in gulp (see below for example code):

mdast readme.md
echo 'hello' mdast
mdast <<< hello

By only throwing if there’s definitly a TTY.

However, this doesn’t work for the below case (which keeps hanging)

var gulp = require('gulp');
var shell = require('gulp-shell');

var task = shell.task('mdast'); // this hangs.

gulp.task('default', task);

@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

Plus, the system outlined above: In bash and gulp: Throws on echo 'hello' | mdast readme.md, but chooses readme.md on mdast readme.md <<< hello (without error).

I’m not sure if “hanging” / waiting for stdin, in a child-process, when not given files is a big problem. I think I’ll just add it :)

@wooorm wooorm closed this in 8f1d7f1 Sep 15, 2015
@wooorm
Copy link
Member

wooorm commented Sep 15, 2015

Thanks for your work and help. This ensures that at least no false errors are thrown. It will be released when GH-57 is merged.

@ghost
Copy link
Author

ghost commented Sep 15, 2015

thank you very much!

wooorm added a commit that referenced this pull request Sep 15, 2015
There’s no way to accuratly detect *if* text is going to be
piped in, especially when using child-processes (e.g., in gulp)
This fixes that by only throwing errors when the CLI is
sure nothing is going to be piped in.

See the below issue for more info.

Closes GH-58.
@wooorm wooorm added 🐛 type/bug This is a problem remark labels Jan 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
remark 🐛 type/bug This is a problem
Development

Successfully merging this pull request may close these issues.

None yet

2 participants