fluttercommunity / import_sorter

🎯 Automatically organize your dart imports. Maintainer: @gleich
https://pub.dev/packages/import_sorter
MIT License
171 stars 41 forks source link

Precommit hook doesn't sort imports #39

Closed martinloretzzz closed 3 years ago

martinloretzzz commented 3 years ago

Describe the bug Precommit hook doesn't sort imports

To Reproduce What command or actions did you take to produce this error? Followed the steps at https://pub.dev/packages/import_sorter When I run 'pre-commit run --all-files' nothing happens

.pre-commit-config.yaml:

repos:
    - repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v3.4.0
      hooks:
          - id: trailing-whitespace
          - id: end-of-file-fixer
          - id: check-yaml
          - id: check-added-large-files
    - repo: https://github.com/fluttercommunity/import_sorter
      rev: "master"
      hooks:
          - id: flutter-import-sorter

I got it working with this hotfix as .pre-commit-config.yaml:

repos:
    - repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v3.4.0
      hooks:
          - id: trailing-whitespace
          - id: end-of-file-fixer
          - id: check-yaml
          - id: check-added-large-files
    - repo: local
      hooks:
          - id: imputsorter
            name: imputsorter
            entry: flutter pub run import_sorter:main
            language: system
            files: ^lib/

It may have something to do with the 'files: .dart$'

Meta Information:

github-actions[bot] commented 3 years ago

👋 Hello! Thanks for submitting a issue! @Matt-Gleich will try to respond as soon as possible.

gleich commented 3 years ago

@lig worked on this. Any idea on why this isn't working?

lig commented 3 years ago

@lig worked on this. Any idea on why this isn't working?

I have some suspicions. I'll look into this.

files: \.dart$

It seems like it worked for me.

@0b01001101 just to clarify: what pre-commit version do you use? Does replacing flutter-import-sorter with dart-import-sorter change anything?

martinloretzzz commented 3 years ago

pre-commit 2.9.3

If i use dart-import-sorter i get this:

Dart Import Sorter.......................................................Failed
- hook id: dart-import-sorter
- exit code: 1

The Flutter SDK is not available.

If I remove the files completely, everything works fine(In my first example the ^lib/ regex has a pattern error, so I got the same effect). I use Windows 10

lig commented 3 years ago

@0b01001101 could you please run pre-commit autoupdate and try again with flutter-import-sorter? @Matt-Gleich has just merged a fix.

martinloretzzz commented 3 years ago

Still doesn't work for me

It may have something to do with windows: https://stackoverflow.com/a/23384429

I will investigate the problem further tomorrow

lig commented 3 years ago

Still doesn't work for me

Could you please provide the full output?

Please run it like this: pre-commit run --verbose --all-files flutter-import-sorter

martinloretzzz commented 3 years ago
- hook id: flutter-import-sorter
- duration: 1.76s

┏━━🗂  Sorting 0 dart files
┃  ┃┃
┗━━🙌 Sorted 0 imports in 0.113 seconds
lig commented 3 years ago

@0b01001101 have you run git add for any dart source files already? Files not known to git won't be checked by git hooks.

Also, consider removing any files patterns you still might have defined.

lig commented 3 years ago

This might be windows related indeed. However, pre-commit should be able to handle that.

martinloretzzz commented 3 years ago

It is strange: if I use types: [file] instead of types: [dart], then everything works like expected. It just doesn't like sorting only dart files.

For me the hotfix with sorting all files works more than good enough, I would say if nobody else has the same problem we close this issue for now. It may be a complex interaction between the flutter installation on a custom location, something related to windows or some other factors I can't even think of.

CelestialGuru commented 3 years ago

I am also having issues on Windows. This is my output of pre-commit run --verbose --all-files flutter-import-sorter:

Flutter Import Sorter....................................................Passed
- hook id: flutter-import-sorter
- duration: 4.79s

contains flutter: true
contains registrant: false
┏━━ Sorting 0 dart files
┗━━  Sorted 0 files in 0.266 seconds
contains flutter: true
contains registrant: false
┏━━ Sorting 0 dart files
┗━━  Sorted 0 files in 0.117 seconds

This is what I have in .pre-commit-config.yaml:

exclude: "build|debug-info|.git"
default_stages: [commit]
fail_fast: true

repos:
  - repo: "https://github.com/pre-commit/pre-commit-hooks"
    rev: v4.0.1
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-added-large-files

  - repo: https://github.com/fluttercommunity/import_sorter
    rev: "aeb26a791ea546506cc8cd2207b320b64a7ed6b5"
    hooks:
      - id: flutter-import-sorter