-
Notifications
You must be signed in to change notification settings - Fork 3.2k
write a tool to sort rust files alphabetically #4360
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
take |
updated the description @marvelshan, let me know if you have any questions |
Thanks so much for the clear and detailed explanation, it was super helpful and really clarified a lot for me. I was able to implement everything without issues! That said, I did run into a small hiccup when running the Nix commands. Specifically, I encountered the following error:
To work around it, I've been running commands like this: nix --extra-experimental-features 'nix-command flakes' <command> It seems to be a common issue for users who haven't enabled these experimental features globally. I found this helpful thread that talks about the problem in more detail: https://discourse.nixos.org/t/error-experimental-nix-feature-nix-command-is-disabled/18089 |
you can add |
## Which issue does this PR close? closes #4360 ## Why This change introduces a deterministic Rust file sorting tool (rustfmt-sort) into the proto code generation pipeline. The motivation is to ensure that all generated Rust files have a stable, alphabetically sorted item order, which: - Reduces unnecessary diffs in generated code, - Improves reproducibility and code review experience, - Makes it easier to track meaningful changes in generated files. This is especially important for large proto-generated codebases where minor changes in proto definitions or generation order can otherwise cause noisy diffs. Additionally, due to limitations in the proto generator, a manual patch for the timestamp field in CanonicalVote is still required after generation. ## Implementation details - Uses syn to parse and mutate the Rust syntax tree, and quote to regenerate the code. - Recursively sorts items in modules as well. - The tool is invoked automatically as part of the proto generation process. ## Modules Affected - tools/rustfmt-sort - tools/rust-proto.nix - tools/tools.nix - Cargo.lock - Cargo.toml
Uh oh!
There was an error while loading. Please reload this page.
see rust-lang/rustfmt#2359 (comment) for why. should be trivial to do with syn, just ensure doc comments are retained (shouldn't require any special handling since they parse as
#[doc]
attributes anyways).after some experimentation, this is slightly more annoying than i thought it would be. here is a working example, using syn and quote:
make a new bin under
tools/
calledrustfmt-sort
, expose it as a package intools.nix
, and use it here inrust-proto.nix
, after therustfmt
call. once this is all done, runnix run .#generate-rust-proto
and commit the changes in a separate commit. you will need to manually patchgenerated/rust/protos/src/tendermint.types.rs
to add back thetimestamp
field toCanonicalVote
.The text was updated successfully, but these errors were encountered: