Git Pre-Commit Hook: Unable to Run `dartfmt` (command not found) (Windows)

1,714

to make dartfmt work, try running which dartfmt manually to get the path to the executable, and then use the absolute path when calling it in the script.

If which isn't able to find it, and assuming you know the complete path to the directory where dartfmt is located, try adding that directory to PATH in the script:

#!/bin/bash
PATH="/path/to/dart-sdk/bin:$PATH"
export PATH

Also, I'd suggest taking a moment double check what git will use for the working directory when it calls those hook scripts. There might be some undesired behavior by using . if the CWD isn't what is expected. See this post.

Share:
1,714
Philippe Fanaro
Author by

Philippe Fanaro

Graduated as an Electrical/Telecommunications Engineer in 2017, from the University of São Paulo, Brazil. Converted to Machine Learning in 2018. Became a Flutter App Developer in 2019. I'm also a ("retired") Go (Baduk or Weiqi) player, and I also have a website with some curious and interesting stuff: fanaro.io

Updated on December 18, 2022

Comments

  • Philippe Fanaro
    Philippe Fanaro over 1 year

    Ideally, I would like to have dartfmt to format my code on every commit, and I think that git hooks are ideal for this. So far, I've tried the code found in this link, but with no success, despite it appearing on multiple other websites — maybe it's outdated.

    In the end, I think nothing much more complicated than this should work in most cases (inside the .git/hooks/pre-commit file):

    #!/bin/bash
    
    dartfmt -w . # or maybe `flutter format .` for Flutter
    

    The errors I get are:

    • For dartfmt -w .: dartfmt: command not found
    • For flutter format .: find: ‘> bin [’: No such file or directory

    Both of those commands do work if placed directly in the terminal.

  • Philippe Fanaro
    Philippe Fanaro about 4 years
    Strangely, when I try which dartfmt, I get things like /usr/bin/which: no dartfmt in (/c/Program Files/Python38/Script ,,,, Maybe I need to add it to the PATH?
  • Philippe Fanaro
    Philippe Fanaro about 4 years
    The bizarre thing is that, in git's shell (Windows), even though the dart-sdk seems to be in the PATH (env | grep PATH), I still get bash: dartfmt: command not found. Maybe dartfmt cannot be run from git? I don't know what's going on here.
  • Z4-tier
    Z4-tier about 4 years
    Definitely try adding the actual complete path for dartfmt to PATH and then export it too. Add PATH="[/c/[....]/path/to/dart-sdk/bin]:$PATH"; export PATH; and see what you get. If you can run it manually there is no reason it shouldn't work from the git hook. It is for sure a path problem. You could also try taking the output of env | grep PATH and copy/pasting it into the hook script, assigning it as PATH and export it.
  • Philippe Fanaro
    Philippe Fanaro about 4 years
    Still not working even after doing putting in your suggestion, unfortunately. I tried it with PATH="/c/src/flutter/flutter/bin/cache/dart-sdk/bin:$PATH".