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

Wrong assumption about missing '-' arg if pipingStdin detected #1115

Closed
aleksandr-vin opened this issue Feb 16, 2022 · 4 comments
Closed

Wrong assumption about missing '-' arg if pipingStdin detected #1115

aleksandr-vin opened this issue Feb 16, 2022 · 4 comments
Labels

Comments

@aleksandr-vin
Copy link

Describe the bug
When yq is placed inside a bigger block where other commands read from stdin which is piped to that whole block, yq is egoistically reading the stdin.

The commit 703418d adds "-" to the args when pipingStdin is detected.

Version of yq: 4.20.1
Operating system: mac
Installed via: homebrew

Command

Problem appears when used in blocks, so the minimal script that reproduces the problem:

#!/usr/bin/env bash
set -e
set -x

cat >foo.json <<EOF
{}
EOF

ls foo*
printf "1\n2\n" | while read -r f
do
    yq -P '.' "foo.json" >"foo-$f.yaml"
done
find . -type f -depth 1 -name 'foo-*.yaml' -print -exec cat {} ";"

Actual behavior

The read of the while block is intended to read 1 and 2 on each iteration of while but in practice there is nothing left for read after yq finished his one call, and 2 can be found in foo-1.yaml:

+ cat
+ ls foo.json
foo.json
+ printf '1\n2\n'
+ read -r f
+ yq -P . foo.json
+ read -r f
+ find . -type f -depth 1 -name 'foo-*.yaml' -print -exec cat '{}' ';'
./foo-1.yaml
{}
---
2

Expected behavior

+ cat
+ ls foo.json
foo.json
+ printf '1\n2\n'
+ read -r f
+ yq -P . foo.json
+ read -r f
+ yq -P . foo.json
+ read -r f
+ find . -type f -depth 1 -name 'foo-*.yaml' -print -exec cat '{}' ';'
./foo-1.yaml
{}
./foo-2.yaml
{}

Workaround

In such cases when yq is involved in a block of commands which has its stdin piped (but not for yq) the egoism of yq can be fed with </dev/null. So the line in the before mentioned script would be:

  yq -P '.' </dev/null "foo.json" >"foo-$f.yaml"
@mikefarah
Copy link
Owner

Yeah I can replicate that - but not actually sure how to fix it.

Any golang experts? I'm doing this to detect if there's stdin:

stat, _ := os.Stdin.Stat()
pipingStdIn := (stat.Mode() & os.ModeCharDevice) == 0

which from what I read is the correct way, but yes it appears to be too eager :/

@aleksandr-vin
Copy link
Author

Maybe adding - only if no files are on the args list?

@mikefarah
Copy link
Owner

mikefarah commented Feb 17, 2022 via email

@mikefarah
Copy link
Owner

Fixed in 4.20.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants