0

I want to ignore yarn errors using this method

yarn install 2> >(grep -v warning 1>&2)

As I am using fish shell I did figure out that this command has to become yarn install 2> ">(grep -v warning 1>&2)" to avoid a syntax error

However after one I want to run yarn run postinstall --force

So my command became yarn install 2> ">(grep -v warning 1>&2)" && yarn run postinstall --force however it seems does not work, yarn run postinstall --force does not really runs. Command finishes right afteryarn install finishes.

Any ideas how to fix?

UPDATE: I also want that script to run in bash because this going to execute not just on my local dev machine but also on CD/CI that does not have fish-shell installed..

faho
  • 14,470
  • 2
  • 37
  • 47
Sergino
  • 10,128
  • 30
  • 98
  • 159

1 Answers1

1

EDIT: The question was originally tagged as "fish", so this answer assumes you'd want to use fish to run the script.

yarn install 2> >(grep -v warning 1>&2)

This is a bit of an awkward idiom. It pipes the stderr of yarn install into a process substitution (the >() syntax, which is like the reverse of its <() cousin). This creates a temporary file that it will then connect to grep -v's stdin, which will then get its output redirected to stderr again. The effect is that stderr is filtered and any line containing "warning" is removed.

Really, the reason for this is that it is awkward in bash to pipe stderr but not stdout - see How can I pipe stderr, and not stdout? - this is https://stackoverflow.com/a/15936384/3150338.

In fish, you can just pipe stderr with 2>|:

yarn install 2>| grep -v warning 1>&2

See e.g.

function printstuff
    echo error >&2
    echo warning >&2
    echo output
    echo warning but on stdout
end

printstuff 2>| grep -v warning 1>&2

This will print "error" on stderr, and "output" and "warning but on stdout" on stdout.

faho
  • 14,470
  • 2
  • 37
  • 47
  • oh btw, taking in to account that it should run in bash...because I am going to run that code on cd/ci that do not really have fish, sorry I should of mention it in question. Basically this `"yarn install 2> >(grep -v warning 1>&2) && yarn run postinstall --force"` should work in bash too... – Sergino Jun 14 '22 at 13:18
  • Fish and bash are incompatible, so if you want to run it in bash you'll have to run it in bash, and not attempt to run different code in fish. Just start bash and try it there, anything else is like trying to write ruby by running it in python. What you already have seems like it should work in bash to me. It's possible you have a very very old bash version (like the one that ships in macOS), or you are actually running *sh*, not bash, and that might not support process substitution. – faho Jun 14 '22 at 13:27
  • Also, `yarn install .. && yarn run` will of course only run the `yarn run` command if the `yarn install` command succeded. Since `">(grep -v warning 1>&2)"` tells `yarn install` to install a package called `>(grep -v warning 1>&2)` (this is a string literal!) it will most likely fail because that package doesn't exist. – faho Jun 14 '22 at 13:31
  • I do have bash 5.1.6 so I think it is pretty recent. If I run `yarn install 2> >(grep -v warning 1>&2) && yarn run postinstall --force` in bash it seems actually ignores everything that comes after `yarn install ...` as in that case it gives the same warning error as if I would just yarn `yarn install` – Sergino Jun 14 '22 at 13:40
  • even if I am running this `yarn install 2> >(grep -v warning 1>&2` seems like `2> >(grep -v warning 1>&2` gets ignored and so getting same warning error as if I would just run `yarn install` – Sergino Jun 14 '22 at 13:43
  • Are you running bash as bash or as sh? – faho Jun 14 '22 at 14:29
  • running bash, just by typing bash in console – Sergino Jun 15 '22 at 06:22
  • Well, maybe `yarn install` just fails? Does its output indicate that it didn't succeed? Does it suddenly succeed without the grep filter, i.e. does running `yarn install ...; echo $?` print 0 but `yarn install ... 2> >(grep -v warning 1>&2); echo $?` print a different number? – faho Jun 15 '22 at 06:57
  • problems solved I used `2> /dev/null` – Sergino Jun 16 '22 at 14:05