Git Pre-Commit Hook: Unable to Run `dartfmt` (command not found) (Windows)
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.
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, 2022Comments
-
Philippe Fanaro over 1 year
Ideally, I would like to have
dartfmt
to format my code on every commit, and I think thatgit 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.
- For
-
Philippe Fanaro about 4 yearsStrangely, 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 thePATH
? -
Philippe Fanaro about 4 yearsThe bizarre thing is that, in
git
's shell (Windows), even though thedart-sdk
seems to be in thePATH
(env | grep PATH
), I still getbash: dartfmt: command not found
. Maybedartfmt
cannot be run fromgit
? I don't know what's going on here. -
Z4-tier about 4 yearsDefinitely try adding the actual complete path for
dartfmt
toPATH
and thenexport
it too. AddPATH="[/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 ofenv | grep PATH
and copy/pasting it into the hook script, assigning it asPATH
andexport
it. -
Philippe Fanaro about 4 yearsStill 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"
.