Skip to content

BSN formatter + CLI - #26391

Draft
taboky-dev wants to merge 39 commits into
bevyengine:mainfrom
taboky-dev:bsnfmt
Draft

taboky-dev wants to merge 39 commits into
bevyengine:mainfrom
taboky-dev:bsnfmt

Conversation

@taboky-dev

@taboky-dev taboky-dev commented Oct 31, 2026 •

Copy link
Copy Markdown

Adopted from #23586
⚠️ This is a work in progress, there are still formatting bugs and it currently only works with macros.

Objective

BSN formatter

Solution

Add a formatter for BSN that works with the bsn! and bsn_list! macros, as well as the upcoming .bsn file.

Testing

  • Unit tests were written to test formatting
  • Currently using this in some projects, it can be run manually from a terminal:
    # Installed
    bsnfmt /path/to/file.rs --write
    # From cloned repo
    cargo run --manifest-path /path/to/bevy/crates/bevy_scene/macros/Cargo.toml --bin bsnfmt --features fmt -- /path/to/file.rs --write

There is a small readme with some instructions on how to install and use the CLI.

Notes

Copied from #23586

  • draft for now, needs cleanup / polish and could be moved but I'd probably prefer to ship it rather than refactor out bsn types in this PR. Maybe it could be done in another PR. Either a follow-up or as a blocker for this one, but bsn types and traits etc. need to be accessible for tooling like this.
  • eventually I can see this being it's own standalone bevy fmt crate in bevy_cli but only if it is a standalone binary because that is fundamentally a requirement for editor workflows (and other things too) imo.
  • depends on bsn! macro code quality improvements: codegen, error handling, tests, docs #23561. Changes will disappear once it's merged. I can rebase and soft revert if required but I think it's probably already fine without a rebase since I already soft reverted the changes onto this branch.
  • Doesn't support comments, if there are any comments present in the bsn macro, it will fail to format. Could be added in the future but it is rather complex since syn strips out comments and as far as I know this requires either matching surrounding areas after formatting or changes to the bsn parser itself.

Showcase

Copied from #23586, it is outdated.

Screenshot 2026-03-30 154435
Recording.2026-03-30.211926.mp4

@github-actions

Copy link
Copy Markdown
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide, as well as our policy regarding AI usage, and we look forward to reviewing your pull request shortly ✨

@taboky-dev

taboky-dev commented Oct 31, 2026 •

Copy link
Copy Markdown
Author

A lot has changed since the previous PR was created, and I am updating this to fit the new BSN changes and keep it up-to-date (as I am in need of a BSN formatter).

I will be fixing the CI errors (and formatting bugs), if it is better to move these changes somewhere else, do let me know!

EDIT: I just became aware of the existence of the bevy_bsn crate, I could move this CLI there and/or use bevy_bsn, or close this PR if there is another formatter in the works. :]

EDIT 2: Just to experiment, I tried to move bsnfmt to bevy_bsn, it is incomplete, and changes to bevy_bsn are needed, an attempt can be seen here: taboky-dev#1, I am wondering if I should make a separate PR for these changes in bevy_bsn, or keep them as part of this PR.

@taboky-dev

taboky-dev commented Oct 31, 2026 •

Copy link
Copy Markdown
Author

There's also rules that aren't defined yet, for example, how Children with 1 entry or a component with 1 field should be formatted:

bsn! {
    Node {
        width: px(100),
    }
    Children [
        @scene_function()
    ]
    // OR
    Node { width: px(100) }
    Children [ @scene_function() ]
}

Or how dot expressions should be formatted:

bsn! {
    ComponentA::default().methodA(1.0)
    // OR
    ComponentA::default()
        .methodA(1.0)

    // IMO a chain should be formatted like this in either case
    ComponentA::default()
        .methodA(1.0)
        .methodB(true)
        .methodC("Hello, World")
}

I couldn't find a discussion about this, I am going with what's easiest to implement first as I want to fix the bugs and make this usable.

@alice-i-cecile
alice-i-cecile requested a review from cart November 1, 2026 18:34
@alice-i-cecile alice-i-cecile added A-Scenes Composing and serializing ECS objects D-Complex Quite challenging from either a design or technical perspective. Ask for help! X-Needs-SME This type of work requires an SME to approve it. M-Release-Note Work that should be called out in the blog due to impact A-Dev-Tools Tools used to debug Bevy applications. S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Nov 1, 2026
@alice-i-cecile

Copy link
Copy Markdown
Member

Awesome! Let us know when this is ready for review.

This branch has not been deployed

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

Labels

A-Dev-Tools Tools used to debug Bevy applications. A-Scenes Composing and serializing ECS objects D-Complex Quite challenging from either a design or technical perspective. Ask for help! M-Release-Note Work that should be called out in the blog due to impact S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Needs-SME This type of work requires an SME to approve it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants