2

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.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76

3 Answers3

2

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.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42
  • 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 Feb 29 '20 at 23:54
  • 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. – Philippe Fanaro Mar 01 '20 at 02:03
  • 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. – Z4-tier Mar 01 '20 at 14:57
  • 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"`. – Philippe Fanaro Mar 01 '20 at 21:23
0

To format your dart code regularly, you can follow one of the two ways mentioned below:

Preferred way:

In IntelliJ Idea go to Settings -> Language & Frameworks -> Flutter -> Select Format Code on save option.

This will format your code every few seconds. This is preferred because you can customize your personal formatting settings such as max words in a line etc.

enter image description here

Alternatively

From Official website run dartfmt -w bin lib to format your code from the command line.

nimi0112
  • 2,065
  • 1
  • 18
  • 32
0

Add dartfmt reference in PATH, like this:

export PATH="/xxx/flutter/bin/cache/dart-sdk/bin:$PATH"
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
zhimin
  • 507
  • 4
  • 5