Skip to content

Conversation

@feliblo
Copy link
Contributor

@feliblo feliblo commented Oct 17, 2025

Description

Following the information provided in 880. This is an implementation of the pretty-format-json hook.

The pretty-format-json has 1k grep.app hits. It's the final unimplemented hook used by airflow as mentioned in this comment.


Notes

  • Preserve JSON Order

    • Added the preserve_order feature to serde_json. This prevents serde from automatically ordering JSON data, which is important for comparisons with older implementations.
    • This change does not appear to affect other tests/features.
    • By default, serde uses a BTreeMap for Object. Enabling preserve_order switches it to IndexMap.
    • This keeps the sorting logic within the sort-keys argument.
  • Git-Style Diff with similar

    • Added the similar library to perform git-style diff checks.
    • Binary size increase is minimal.
    • Useful as a general utility and could be leveraged in other parts of the project, maybe needed to be relocated?
    • Added color highlighting to the diffs, improving readability compared to original pre-commit.
cargo clean
cargo build --release 
  • Master branch:  8.49 MB
  • Feature branch: 8.55 MB
  • +66,176 bytes (+0.74%) increase from master to feature branch

Q: why is my size so much smaller than both sizes mentioned here?


Performance

Ran these commands on the airflow repository:

# old
pre-commit run pretty-format-json --all-files --verbose
Format JSON files........................................................Passed
- hook id: pretty-format-json
- duration: 0.03s

# Old prek implementation (python)
prek run pretty-format-json --all-files --verbose
Format JSON files........................................................Passed
- hook id: pretty-format-json
- duration: 0.03s

# New implementation (rust)
prek run pretty-format-json --all-files --verbose
Format JSON files........................................................Passed
- hook id: pretty-format-json
- duration: 0.00s

Output

Old pre-commit:
image

New:
image

Where both would autofix to the same:
image

@feliblo feliblo changed the title feat: implement pretty_format_json feat: (pretty_format_json) implement builtin hook Oct 17, 2025
@feliblo feliblo closed this Oct 17, 2025
@feliblo feliblo reopened this Oct 17, 2025
@feliblo feliblo force-pushed the pretty_format_json branch from 897d3f3 to ae79e95 Compare October 17, 2025 11:52
@codecov
Copy link

codecov bot commented Oct 17, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.45%. Comparing base (697b101) to head (107da9e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #915      +/-   ##
==========================================
+ Coverage   90.06%   90.45%   +0.39%     
==========================================
  Files          65       66       +1     
  Lines       12077    12575     +498     
==========================================
+ Hits        10877    11375     +498     
  Misses       1200     1200              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@feliblo feliblo force-pushed the pretty_format_json branch from ae79e95 to 386ebd2 Compare October 17, 2025 12:00
@feliblo feliblo mentioned this pull request Oct 17, 2025
34 tasks
@lmmx
Copy link
Contributor

lmmx commented Oct 17, 2025

Not sure why our binary sizes don't match, a good idea would be to have this computed on CI (I know CodeCov can do that automatically for JavaScript bundles or something? But not sure about Rust)

Will open an issue about that, but relative size like you did is fine 👍

Edit: oh, did you build in release mode? I doubt it would matter though

I thought similar was the same crate difftastic uses but that is strsim

It says there is a testing crate for it https://github.com/mitsuhiko/similar-asserts maybe that could simplify the tests, but no need to

@lmmx lmmx mentioned this pull request Oct 17, 2025
@feliblo
Copy link
Contributor Author

feliblo commented Oct 17, 2025

@lmmx Yes I did, assumed you did aswell fom this snippet:

louis 🌟 ~/dev/prek $ du -h target/release/prek; echo; du target/release/prek
12M     target/release/prek

11920   target/release/prek

@feliblo
Copy link
Contributor Author

feliblo commented Oct 17, 2025

@lmmx I'll take a look at simplifying the tests. Could also assign a new issue to me, so I can pick it up later on, when time allows

@lmmx
Copy link
Contributor

lmmx commented Oct 17, 2025

@lmmx I'll take a look at simplifying the tests. Could also assign a new issue to me, so I can pick it up later on, when time allows

All good! Just FYI, you don't need to be assigned anything to work on it here. Open source repos work on a pull-based model where contributors pick up issues they want to work on. I'm just another contributor here, not a maintainer, so I can't assign tickets anyway 😁

My comment was just a suggestion if you were after any more ideas, feel free to leave it as is. Enjoy your weekend 😄

@j178
Copy link
Owner

j178 commented Oct 17, 2025

Not sure why our binary sizes don't match

I think you might be on different operating systems or architectures?

@feliblo
Copy link
Contributor Author

feliblo commented Oct 17, 2025

Arm, macbook here! Didnt think it would be such a difference. Like the ci suggestion!

@lmmx
Copy link
Contributor

lmmx commented Oct 18, 2025

Arm, macbook here! Didnt think it would be such a difference. Like the ci suggestion!

Linux, no I didn't expect it to be such a gap either

@j178 j178 changed the title feat: (pretty_format_json) implement builtin hook Implement pretty_format_json as builtin hook Oct 20, 2025
@j178 j178 added the enhancement New feature or request label Oct 20, 2025
@feliblo feliblo force-pushed the pretty_format_json branch from 386ebd2 to 333ae62 Compare October 22, 2025 13:27
@feliblo feliblo force-pushed the pretty_format_json branch from 333ae62 to 107da9e Compare October 22, 2025 13:28
@github-actions
Copy link

📦 Cargo Bloat Comparison

Binary size change: +0.62% (16.1 MiB → 16.2 MiB)

Expand for cargo-bloat output

PR Branch Results

 File  .text     Size          Crate Name
 0.6%   1.3% 101.1KiB          prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.6%   1.2%  91.7KiB           prek prek::builtin::pre_commit_hooks::Implemented::run::{{closure}}
 0.5%   1.0%  76.9KiB           prek prek::languages::<impl prek::config::Language>::run::{{closure}}
 0.4%   0.9%  70.0KiB           prek prek::run::{{closure}}
 0.4%   0.8%  63.8KiB           prek prek::archive::unpack::{{closure}}
 0.2%   0.5%  40.9KiB regex_automata regex_automata::meta::strategy::new
 0.2%   0.5%  40.1KiB          prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.2%   0.5%  38.5KiB           prek prek::identify::by_extension::{{closure}}
 0.2%   0.5%  35.0KiB           prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.4%  32.9KiB           prek prek::workspace::Workspace::discover
 0.2%   0.4%  32.8KiB           prek prek::languages::node::installer::NodeInstaller::install::{{closure}}
 0.2%   0.4%  32.8KiB           prek prek::cli::run::run::run::{{closure}}
 0.2%   0.4%  31.8KiB             h2 h2::proto::connection::DynConnection<B>::recv_frame
 0.2%   0.4%  31.7KiB             h2 h2::proto::connection::Connection<T,P,B>::poll
 0.2%   0.3%  26.2KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::send_request::{{closure}}
 0.2%   0.3%  25.8KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::connect_to::{{closure}}::{{closure}}::{{closure}}
 0.2%   0.3%  25.2KiB        globset globset::GlobSetBuilder::build
 0.2%   0.3%  24.9KiB           prek prek::main
 0.1%   0.3%  24.6KiB           prek prek::languages::golang::installer::GoInstaller::install::{{closure}}
 0.1%   0.3%  23.7KiB          hyper hyper::proto::h1::dispatch::Dispatcher<D,Bs,I,T>::poll_loop
39.9%  87.5%   6.5MiB                And 10164 smaller methods. Use -n N to show more.
45.6% 100.0%   7.4MiB                .text section size, the file size is 16.2MiB

Base Branch Results

 File  .text     Size          Crate Name
 0.6%   1.4% 101.1KiB          prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.5%   1.1%  85.8KiB           prek prek::builtin::pre_commit_hooks::Implemented::run::{{closure}}
 0.5%   1.0%  76.8KiB           prek prek::languages::<impl prek::config::Language>::run::{{closure}}
 0.4%   0.9%  70.0KiB           prek prek::run::{{closure}}
 0.4%   0.9%  63.8KiB           prek prek::archive::unpack::{{closure}}
 0.2%   0.5%  40.9KiB regex_automata regex_automata::meta::strategy::new
 0.2%   0.5%  40.1KiB          prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.2%   0.5%  38.5KiB           prek prek::identify::by_extension::{{closure}}
 0.2%   0.5%  35.0KiB           prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.4%  32.9KiB           prek prek::workspace::Workspace::discover
 0.2%   0.4%  32.8KiB           prek prek::cli::run::run::run::{{closure}}
 0.2%   0.4%  32.7KiB           prek prek::languages::node::installer::NodeInstaller::install::{{closure}}
 0.2%   0.4%  31.8KiB             h2 h2::proto::connection::DynConnection<B>::recv_frame
 0.2%   0.4%  31.7KiB             h2 h2::proto::connection::Connection<T,P,B>::poll
 0.2%   0.4%  26.2KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::send_request::{{closure}}
 0.2%   0.3%  25.8KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::connect_to::{{closure}}::{{closure}}::{{closure}}
 0.2%   0.3%  25.2KiB        globset globset::GlobSetBuilder::build
 0.2%   0.3%  24.9KiB           prek prek::main
 0.1%   0.3%  24.6KiB           prek prek::languages::golang::installer::GoInstaller::install::{{closure}}
 0.1%   0.3%  23.7KiB          hyper hyper::proto::h1::dispatch::Dispatcher<D,Bs,I,T>::poll_loop
39.7%  87.4%   6.4MiB                And 10107 smaller methods. Use -n N to show more.
45.4% 100.0%   7.3MiB                .text section size, the file size is 16.1MiB

@feliblo
Copy link
Contributor Author

feliblo commented Oct 25, 2025

@j178 Anything I can add to this PR? I checked the similar-asserts crate, but didn't find it cleaned up tests more than simple test files, like it has at the moment. If you still prefer the other way, I'm happy to change it.

@j178
Copy link
Owner

j178 commented Oct 25, 2025

Your work is fantastic - the rest is on my end now. I just need more time to figure out all the details and make sure everything lines up with the Python implementation. If I think anything needs tweaking, I’ll make adjustments based on what you’ve built. Thanks for all your hard work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants