-
Notifications
You must be signed in to change notification settings - Fork 125
Add ability to exclude files #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I don't know if it if it's worthwhile to add too much complexity directly in dartfmt for determining what files it runs on. I don't want to have to reinvent and maintain a subset $ find . -name *.dart -not -name *.g.dart -exec flutter format {} \; |
The problem with this approach (which I should have mentioned in my issue) is that I have found it to be much slower. That said, I'm running on Windows and using Grinder. Let me play around with it a little more and report back here. |
Here are some results running Approach 1: run
|
This sounds like a performance issue with $ time flutter format temp.dart
Unchanged bin/temp.dart
real 0m0.957s
user 0m1.063s
sys 0m0.358s
$ time dartfmt -w temp.dart
Unchanged bin/temp.dart
real 0m0.119s
user 0m0.103s
sys 0m0.056s And formatting the entire Flutter repo: $ time flutter format .
...
real 0m9.746s
user 0m12.987s
sys 0m1.239s
$ time dartfmt -w .
...
real 0m8.287s
user 0m10.011s
sys 0m0.702s I don't see why For example, if I run: $ time find . -name *w*.dart -not -name *_test.dart -exec dartfmt -w {} \; It matches 112 files and runs in:
That's not blindingly fast, but it's not intolerable. But when I change it to: $ time find . -name *w*.dart -not -name *_test.dart -exec flutter format {} \; Then it takes:
Which is... really bad. Perhaps it's worth filing an issue on Flutter around the startup performance of
Maybe I'm asking something dumb but... why not just format the generated files as soon as you generate them? Then you don't have to exclude them. |
This would be awesome, but I'm not sure of any way of doing this. I'm using this command to generate code as I work on my code base:
To my knowledge, there are no options on |
Not in general. Many builders which emit |
As per this issue, the problem is that my code base uses a 120 column width rather than the standard, claustrophobic, 80 characters. So I still feel like this issue is best solved by enhancing Meanwhile, I don't see any easy path forward for me right now (other than switching to 80 char column width, which I hate). It's worth bearing in mind that I run |
I'm having performance issue with both $ time find . -name "*.dart" ! -path "*/generated/*" ! -name "*.g.dart" -exec dartfmt --fix --dry-run --set-exit-if-changed {} \;
find . -name "*.dart" ! -path "*/generated/*" ! -name "*.g.dart" -exec dartfm 19.79s user 11.38s system 143% cpu 21.770 total
$ time find . -name "*.dart" ! -path "*/generated/*" ! -name "*.g.dart" -exec dart format --fix --show none --summary none --output none --set-exit-if-changed {} \;
find . -name "*.dart" ! -path "*/generated/*" ! -name "*.g.dart" -exec dart 88.94s user 32.95s system 88% cpu 2:18.50 total
$ time find . -name "*.dart" ! -path "*/generated/*" ! -name "*.g.dart" -exec flutter format --dry-run {} \;
find . -name "*.dart" ! -path "*/generated/*" ! -name "*.g.dart" -exec flutte 168.47s user 74.83s system 110% cpu 3:39.73 total It is really really fast when I run the command directly: $ time dartfmt --fix --dry-run --set-exit-if-changed lib test test_driver
dartfmt --fix --dry-run --set-exit-if-changed lib test test_driver 1.10s user 0.28s system 158% cpu 0.870 total
$ time dart format --fix --show none --summary none --output none --set-exit-if-changed lib test test_driver
dart format --fix --show none --summary none --output none lib test 1.91s user 0.47s system 123% cpu 1.923 total
$ time flutter format --dry-run lib test test_driver
flutter format --dry-run lib test test_driver 2.63s user 0.79s system 132% cpu 2.588 total Some more info about the project:
@munificent |
@kentcb I found a way around this:
dart format --fix --show changed --summary none --output none lib test test_driver It should have an output like this:
mapfile -t unformatted_files < <(dart format --fix --show changed --summary none --output none lib test test_driver)
for file in "${unformatted_files[@]}"; do
file=${file#"Changed "}
# Warn about the unformatted file if it's not in the whitelist
if [[ \
! "$file" = *".g.dart" \
&& ! "$file" = *"/generated/"* \
]]; then
echo
echo "\"$file\" is not properly formatted"
echo "Please run the below command to format it:"
echo
echo " dart format --fix $file"
echo
exit 1
fi
done @munificent this is rather hacky and is not stable (depending on how stable the output of |
@munificent - the startup time for each individual @kentcb - I'd suggest documenting a few of these approaches in the issue description so that others looking at this bug can get the suggestions. I appreciate my method only works for repos of modest size.
|
This is maybe a silly question, but is there a reason you don't just go ahead and format the generated files? Doing so is harmless, probably makes them easier to read, and lets you just run dartfmt on your whole source tree. This is generally what we do for all of the Google-maintained Dart projects. |
@munificent - this is a good point and on second thoughts I agree with you. I didn't realize this before but the WHO App codebase already does what you suggested of formatting the generated files. @kentcb - I still think it's worth summarizing some of these thoughts in the description, so people can learn good strategies for this. |
@munificent I'm looking for a similar solution because I'm using |
I am running into similar issue with my Github Action. Trying to run flutter format lib/, but this includes generated translation files. I want the Action to cease if there is a format so I use flutter format lib/ --set-exit-if-changed, but this exits because of the generated files. @munificent my understanding would be best practice is to not check generated files into the repo. I can explicitly list every sub directory, but would be far easier to just exclude the generated file vs. having to list every directory that needs to be formatted. Thoughts? |
@mbaz2 you could run |
Often that is true. However, I too am interested in an exclude directory feature for |
The easy fix here is to generate them, format them, then commit that. |
I'm trying to convince my colleague of that. 🤞 |
Thanks for your answer, I adapted to exclude the generated * .g.dart classes and adapted with --set-exit-if-changed to run in my pipeline.
|
I still find this option worth implementing. The codebase for my medium sized app takes 1m 22s to format, way too long for snappy feeling. This is because we are formatting protobuf generated classes.
It is over 15x faster to remove these generated classes, format the code and regenerate them back.
And we are talking about relatively fast CPU not some slow CI machine. I realize that it is not common for someone to format entire codebase every time, but I see few cases where that could be useful like pre-commit checks, coding without IDE, formatting multiple projects. |
Can you provide some details on which generated classes are the slowest? It should never take dartfmt that long to format even a very large repo, but there are a couple of edge cases (issues) where the perf degrades significantly. I'd rather spend time fixing those performance bugs than spend time adding features that let users work around those bugs. |
Can't share my file, but if I generate class from Google APIs Protobufs it has similar time/file size. https://gist.github.com/pr0gramista/8dfa8d653f3cd5dd075eeb5c36dbe0ca
https://gist.github.com/pr0gramista/5a9615f1a4bfb40a4b9b3301bf8cc78f
One of my generated file with few classes in it has ~2500 LOC and takes 17s, a bit longer than expected - maybe because of how nested our structures are. I'll post it if I manage to obscure the contents. |
Manual find because Dart/Flutter tools don't support excluding files: dart-lang/dart_style#864
Manual find because Dart/Flutter tools don't support excluding files: dart-lang/dart_style#864
Fwiw I would be happy to contribute here. This has caused me really significant pain on several occasions and I would love to fix it properly. |
Sharing tests results from @mvanbeusekom suggestion, compared with @munificent first suggestion, running on a MacBook Pro M2: time find . -name '*w*.dart' -not -name '*_test.dart' -exec dart format {} \; 83.08s user 28.74s system 84% cpu 2:12.90 total time dart format $(find . -name '*w*.dart' -not -name '*_test.dart') 1.61s user 0.23s system 117% cpu 1.569 total
Formatted 388 files (265 changed) in 1.17 seconds. |
For my use case, I ended up using the following, where dart format --set-exit-if-changed $(find lib test -name '*.dart' -not -name 'job.dart' -and -not -name '*.freezed.dart') |
* Worthy of first commit * Add actual generator * Tweak pubspec.yaml for bette PANA score * move ejson project into ejson folder * Rudimentary generator inplace * Custom decoding tests * Fix realm generator tests * Update pubspec repo ref * wip * Add copyright headers * Fix multi parameter ctor bug. More tests * Add bad input test, and cheat a bit for dynamic tests * A few compile tests in place * Split in multiple project and move to melos * Basic source error reporting * add qa script to melos.yaml (combines format:check, analyze, and test) * testCompile supports skip * Enable melos qa on github actions * Use utc in test * Add LICENSE file + repo link in pubspecs * More melos magic * Make ejson package public * Add internal dep * Use >- over | to work around windows line ending issue * Force \n in DartFormatter due to windows line ending issue * Drop qa:static scripts (qa:full calls qa) * Fix bug in testCompile * format and check coverage * Use ejson in realm * Lint rules (WIP) * Report lints as errors instead of infos * Use links for LICENSE file * Link READMEs * Expand melos:qa to include test:lints and analyze:deps * Fix tests after rebase * Support ObjectId and Uuid out-of-the-box * Update analyzer dependency for ejson * Update lint related deps * Fix lints and tests * Run builder runner * Fix realm generator tests * Flatten package structure * Avoid path deps in public packages and remove publish_to: * Add melos support * Add lints package on bootstrap Ensure lints is added to all packages during bootstrap, if missing * Simplify CI a bit with melos * Update root .gitignore * build_native.dart (wip) * Split bootstrap pre-hook into separate setup script (for speed when setup not needed) * Align SDK requirement (handled by melos bootstrap) * melos bootstrap needed This is because some packages are not published yet * fixup: bad rebase * Reorder steps * missing deps in example * Cleanup .gitignore hierarchy * Align analysis_options.yaml files with symlinks * realm/example is a flutter project * Remove last remnants of toplevel ejson folder * Tweak melos.yaml * TMP: workaround * Coverage (wip) * Use combine_coverage package instead of lcov * Only report on lib folder * Prune coverage a bit * Don't run tests twice * Skip redundant step * Update checkout action to v4 * Update upload-artifact action to v4 * Update download-artifact action to v4 * Update dorny/test-reporter action to v1.8.0 * Skip redundant step * Update actions/setup-java action to v4 * Update gradle/gradle-build-action action to v3 * Don't use random github action to install ninja-build. It is not maintained * Update futureware-tech/simulator-action action to v3 * Update geekyeggo/delete-artifact action to v4 * Fix symlink blunder * Tighten analysis rules * add upgrade script to melos.yaml * bump custom_lint_builder * tweak publish-release.yml * Update format and lint:format to not touch generated files (workaround for dart-lang/dart_style#864) * Implicit casts made explicit, as mandated by stricter analysis_options.yaml * melos run format * tweak melos.yaml * Fix lints and increase coverage * switch to melos coverage:groom * strong-mode has been superceeded by strict * enable custom_lint tools * Add example to package ejson * Fix missing quotes on int and double values in canonical mode * Relax Array type from LIst to Iterable * Update a ejson_lint/example README.md * Fix missing quotes on DateTime in canonical mode * Fix ejson_generator tests * Only install ninja on linux builds * Tweak a test * Force a new native build.. * Tweak condition for Ninja (android- & linux-) * Fix linux.. and simplify * Upgrade realm-core to v14.0.1 * testing hypothesis * Rework install command * Drop ejson_serialization_setup.g.dart (for now) * Support Uint8List * Get rid of to<T> extension (use fromJson<T> instead) * Update ejson codecs * Fix "generator" tests * Fix realm_value_test.dart * Convert coverage, despite test failure * More encoding tests * Fix blunder regarding canonical vs. relaxed * Don't forget windows *sigh* * Split test and coverage handling * Small formatting error * Use super.<x> syntax * Drop some imports * Don't use _ prefix on already local stuff * DateTime codec use local time in relaxed mode, but millisecondSinceEpoch (utc) in canonical mode * export ejson.dart from realm.dart to avoid extra import in user code * export ejson_annotation.dart from ejson.dart to avoid extra import in user code * Update copyright * Remove redundant "library;" * Remove the now unnecesary imports * Remove deprecated exports * Add unsynced to CSpell * Don't use ejson_generator for realm objects, but have realm_generator create the codec pair * Update .expected files * Drop dep on ejson_annotation and ejson_generator * Rerun generator (.g.dart files no longer generated) * format * Absolute symlinks should be relative * Upgrade simulator to iPhone SE (3rd generation) * Update example with to/From-EJson * Update CHANGELOG * Dart doc updates * Missing headers * Missing headers .. unrelated to ejson * Implement PR feedback (part 1) * Refactor int.toEJson to take an optional forcedFormat argument, and otherwise infer format from size of value * Add toEJsonString and fromEJsonString<T> convinience functions * Missing tests for Uint8List aka binary with subtype '00' * Fix Uint8List decoding bug * Generate registerX function * Refactor realm_generator EJSON support a bit * fix realm example * Update CONTRIBUTING.md * No need to build realm_dart for maccatalyst by default, as flutter doesn't currentlys * More work on build.dart * Update build:native script to use tool/build.dart * A bit of gold plating.. * .. and some bugfixes * Important to drain both stdout and stderr
It's been 5 years since this issue was created and almost a year since the last comment. Any updates? It would be a great addition to this tool, since so many users (myself included) seem to be needing it. |
@munificent does dart format support the excludes from analysis_options.yaml files now? |
It does not. I'd like to, but I haven't had the time and I didn't want to keep delaying shipping the new style. Hopefully in a future version. |
Sounds reasonable... it also isn't clear just because a file is opted out of analysis it should also be opted out of formatting, so then you get requests for a |
It would probably be: formatter:
exclude:
- some/file/path.dart So it should be pretty clear what you're excluding it from. |
Are those relative to the file in which they appear, or the root analysis options file? (I guess, do whatever the analyzer does for its I suppose this applies to all configuration but this is yet another thing that tools using the formatter as a library would ideally respect, but won't be able to in all cases (if the analysis options doesn't live under a package but just at the root of a workspace as an example, then build_runner builders can't access it). We can possibly just live with the fact that this configuration won't be respected by all tools though. |
Probably whatever analyzer does, which I think is relative to the
Yeah. :-/
Yeah. :( Configuration is hard. |
* See: https://medium.com/dartlang/announcing-dart-3-7-bf864a1b195c * dart format . && dart fix --apply * pub get before formatten * try to change l10n settings * workaround for dart-lang/dart_style#864 * flutter build macos * flutter build macos * empty commit
Do we have the ability to exclude specific file/directories yet? |
No, but you can put |
edit: I forgot to update to |
I'm using
dartfmt
viaflutter format
, which I run it like this:This formats all
.dart
files recursively, which is almost exactly what I want. Unfortunately, it also formats.g.dart
files, so my generated files bounce between their generated state and a formatted version thereof.It would be very useful if
dartfmt
could exclude files matching certain patterns. Something like:(surfacing this via
flutter format
would of course be a separate issue)The text was updated successfully, but these errors were encountered: