hyiso / lint_staged

Run linters on git staged files for your Flutter and Dart projects. Inspired by JavaScript lint-staged library
https://pub.dev/packages/lint_staged
Apache License 2.0
6 stars 2 forks source link

If i add three rules in lint staged not performing first two rules only perform final rule #17

Open vijaymsc opened 3 weeks ago

vijaymsc commented 3 weeks ago

pubspec.yaml dev_dependencies: husky: 0.1.7 lint_staged: 0.5.1 lint_staged: 'lib/**.dart': dart format . -l 120 --fix && dart fix --apply && dart analyze --fatal-infos --fatal-warnings

husky pre_commit file

!/usr/bin/env sh

. "$(dirname -- "$0")/_/husky.sh"

dart run lint_staged

Outpute is

✗ dart analyze --fatal-infos --fatal-warnings lib/main.dart Analyzing main.dart...

error - main.dart:35:3 - Don't invoke 'print' in production code. Try using a logging framework. - avoid_print info - main.dart:53:14 - Use 'const' with the constructor to improve performance. Try adding the 'const' keyword to the constructor invocation. - prefer_const_con structors

2 issues found. husky - pre-commit hook exited with code 1 (error)

Expected is

  1. formate the staged file
  2. then fix lin error like const key
  3. then check flutter anlyze the issue

    error - main.dart:35:3 - Don't invoke 'print' in production code. Try using a logging framework. - avoid_print

note: I think first two rules are executed and if run third one before rules are reverted and final result is third rule outputes

hyiso commented 3 weeks ago

Is lib/main.dart in git staged state?(aka git add lib/main.dart)

Also, you can add DEBUG=true before dart run lint_staged in .husky/pre-commit to make the verbose logging message visible

DEBUG=true dart run lint_staged
vijaymsc commented 3 weeks ago

yes my main.dart file is git added file, i check three scenario my main.dart file have parint statement and some methods not use const key also main.dart file not fomarmated. i add main.dart file in git and then try commit my file the pre commit file executed.

Actual Output:- ✗ dart analyze --fatal-infos --fatal-warnings lib/main.dart Analyzing main.dart...

error - main.dart:35:3 - Don't invoke 'print' in production code. Try using a logging framework. - avoid_print info - main.dart:53:14 - Use 'const' with the constructor to improve performance. Try adding the 'const' keyword to the constructor invocation. - prefer_const_con

lint_stageed commands: lint_staged: 'lib/**.dart': dart format . -l 120 --fix && dart fix --apply && dart analyze --fatal-infos --fatal-warnings

My goal is: when i try commit format my staged files and auto fix and apply lint issues like const keys and then finely execute dart analyze still have any issue like print statement that issue only return in output. not entire analyze issue

If i remove flutter analyze command and run flutter format and dart fix --apply this is working fine.

hyiso commented 3 weeks ago

If you run these three scripts by hand, does they all succeed or any failure happens?

lint_staged will revert tasks' modification when script fails

vijaymsc commented 2 weeks ago

I set 'lib/**.dart': dart format . -l 120 --fix && dart fix --apply && dart analyze --fatal-infos --fatal-warnings. command in pubspec.yaml file.

main.dart have this lint issues error - main.dart:35:3 - Don't invoke 'print' in production code. Try using a logging framework. - avoid_print info - main.dart:53:14 - Use 'const' with the constructor to improve performance. Try adding the 'const' keyword to the constructor invocation. - prefer_const_con

git add lib/main.dart and try commit the file using lint staged
again give two issues error - main.dart:35:3 - Don't invoke 'print' in production code. Try using a logging framework. - avoid_print info - main.dart:53:14 - Use 'const' with the constructor to improve performance. Try adding the 'const' keyword to the constructor invocation. - prefer_const_con.

=> dart fix --apply command clear a const key issues in automaticaly and only give print statement issue but actual result result is again give two warning const,print in lint staged commit. dart fix --apply not workking

hyiso commented 2 weeks ago

@vijaymsc Thanks for the detailed information, this is working by design:

  1. dart fix --apply did work, it fixed the const key issue, but it could not fix the avoid_print issue
  2. dart analyze --fatal-infos --fatal-warnings failed with the avoid_print issue
  3. lint_staged reverted the modification of dart fix --apply because script failure happened
  4. the code remains unchanged
vijaymsc commented 2 weeks ago

@hyiso thanks for clarification but i need another doubt.

  1. my main.dart file only have have const key issue that time dart fix --apply did work right. but dart analyze --fatal-infos --fatal-warnings failed with the const key .
  2. also when i try to commit my file using lint staged its take time to long , is there any chance to reduce commit execution time.
hyiso commented 2 weeks ago

@hyiso thanks for clarification but i need another doubt.

  1. my main.dart file only have have const key issue that time dart fix --apply did work right. but dart analyze --fatal-infos --fatal-warnings failed with the const key .

Do you mean dart fix --apply fixed the issue but dart analyze --fatal-infos --fatal-warnings still report the issue?

  1. also when i try to commit my file using lint staged its take time to long , is there any chance to reduce commit execution time.

This is because dart analyze takes time to analyze code. lint_staged's work is find out the staged files and passes every file to every script configured under lint_staged

vijaymsc commented 2 weeks ago

Yes i have only const key lint error only dart fix --apply fixed the issue but dart analyze --fatal-infos --fatal-warnings still report the issue

hyiso commented 2 weeks ago

This is unexpected, could you provide a small reproducible code example?