0

I am working on a pre-commit hook bash script that makes use of a cli tool (snyk). Here is an example:

#!/usr/bin/env bash
echo "running snyk code test"
snyk code test

If I invoke the above using "pre-commit run --all-files" the tool runs as expected however it runs multiple times.

When this occurs the exit code is 1

If I then modify the script to include a flag that I know will result in a pass

#!/usr/bin/env bash
echo "running snyk code test"
snyk code test --severity-threshold=high

The script will run once and results in an exit code of 0.

Is this behavior related to a lack of error handling in my script? If so could someone please show me how to catch the exception so the command runs once the script fails and returns an exit code of 1.

Jonathan Gruber
  • 408
  • 1
  • 16
  • You need to determine first which command is retrying, is it `pre-commit` or `snyk`?. Run the script by itself to test. – LMC Apr 21 '22 at 15:25
  • 1
    Thank you. My apologies that is something I should have done first. It would appear that pre-commit is the cause of the retry. If I run the bash script on its own where I know the exit code will be 1 it only runs once. – super-fun Apr 21 '22 at 15:33
  • Check [snyk exit codes](https://docs.snyk.io/snyk-cli/commands/code#exit-codes) on the script, may be not all of them are `1`. `snyk code test; echo "snyk exit code: $?"` – LMC Apr 21 '22 at 15:39
  • 1
    While looking into this a bit more it seems that I can into this [link](https://github.com/pre-commit/pre-commit/issues/836). I was able to correct the issue by adding `pass_filenames: false` to the custom hook I added to my `.pre-commit-config.yaml` file. @LMC thank you very much for your help! – super-fun Apr 21 '22 at 16:06
  • You can answer your own question with the fix you found. That's even encouraged on SO :-) – LMC Apr 21 '22 at 16:54

0 Answers0