Skip to content

Option to disable skeleton formatting with rustfmt - #1465

Merged
d-e-s-o merged 1 commit into
libbpf:masterfrom
flxzt:fz/no-rustfmt
Oct 12, 2026
Merged

d-e-s-o merged 1 commit into
libbpf:masterfrom
flxzt:fz/no-rustfmt

Conversation

@flxzt

@flxzt flxzt commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Added ability to skip running rustfmt on the skeleton Rust source entirely
in libbpf-cargo.

  • In the crate, SkeletonBuilder::rustfmt() changed signature to take parameter
    Option<PathBuf>.
  • The CLI gained flag --disable-rustfmt.

closes #1404

@flxzt
flxzt force-pushed the fz/no-rustfmt branch 4 times, most recently from a47e37b to a11509b Compare October 9, 2026 15:54
Comment thread libbpf-cargo/CHANGELOG.md Outdated
Comment on lines +3 to +5
- Added ability to skip running `rustfmt` on the skeleton Rust source entirely in `libbpf-cargo`.
In the crate, `SkeletonBuilder::rustfmt()` changed signature to take parameter `Option<PathBuf>`.
The CLI gained flag `--disable-rustfmt`.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please stick to existing format; no sentences.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fixed.

Comment thread libbpf-cargo/CHANGELOG.md Outdated
Comment thread libbpf-cargo/src/main.rs Outdated
let rustfmt_path = if disable_rustfmt {
None
} else {
Some(rustfmt_path.unwrap_or("rustfmt".into()))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearly we don't want to have three copies of the string "rustfmt" floating around everywhere. Introduce a constant?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do.

Comment thread libbpf-cargo/src/gen/mod.rs Outdated
Command::new(r)
} else {
Command::new("rustfmt")
return Ok(Cow::Borrowed(s.as_bytes()));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is directly reachable from the public generate and you are changing semantics of said function subtly, for the worse, and for no good reason, from what I gather.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would skipping try_rustfmt() at caller side be better in your view when rustfmt_path is None (two occasions)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you question the change of semantics of the rustfmt argument itself: this was intentional, the fallback is resolved now as early as possible (in main and in SkeletonBuilder) which I thought is cleaner.

Otherwise one would have to deal with something like Option<Option<PathBuf>> where the outer option is resolved to the fallback "rustfmt" executable and the inner one let's it skip or something like that.
That was a pattern I was not comfortable with.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested Option is a way, but I tend to agree it's not ideal. My main gripe is that there is effectively no breaking change and yet semantics change, so users won't notice.

Personally, given how niche this entire rustfmt infrastructure is, I'd say we just special case Some("") to map to "skip". I'd think then we can also remove the public "private" constant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have changed it to do that.

Comment thread libbpf-cargo/src/lib.rs Outdated
/// Default searches `$PATH` for `rustfmt`
pub fn rustfmt<P: AsRef<Path>>(&mut self, rustfmt: P) -> &mut Self {
self.rustfmt = rustfmt.as_ref().to_path_buf();
pub fn rustfmt(&mut self, rustfmt: Option<PathBuf>) -> &mut Self {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we dropping the P generic?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a change to avoid that users have to supply None::<PathBuf>.
Since it is a breaking change anyway, I thought this makes the public API more ergonomic.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It penalizes usage of .rustfmt("xxxx"), though, which now requires an explicit PathBuf conversion first.

I'd think something like .rustfmt::<PathBuf>(None) is quite acceptable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, with the latest change this is untouched.

Comment thread libbpf-cargo/src/main.rs Outdated
Comment on lines +83 to +85
/// Whether to disable applying rustfmt to the generated skeleton Rust source.
#[arg(long)]
disable_rustfmt: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is bad in many ways. For one, at the very least it should be mutually exclusive with rustfmt_path. But also, it seems icky to have a separate argument like that to begin with. Can we fold that into rustfmt_path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered how to approach this in a better way as well - the reason I opted for this implementation is to avoid breakage as much as possible.
Users that keep invoking the CLI in the same way still fall back to rustfmt, while users that explicitly want to opt out of formatting can do so with the new flag.
I will apply that both arguments are mutually exclusive, for the rest I did not find a better pattern - I'm open to suggestions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When both options are supplied, the CLI now reports the following error:

$ cargo run -p libbpf-cargo -- libbpf gen --rustfmt-path some_path --disable-rustfmt

error: the argument '--rustfmt-path <RUSTFMT_PATH>' cannot be used with '--disable-rustfmt'

@d-e-s-o d-e-s-o Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we map --rustfmt-path="" to None?

Or plumb it through as per earlier comment and then special case where needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed it accordingly.

@flxzt
flxzt force-pushed the fz/no-rustfmt branch 2 times, most recently from 802ee6b to ef77273 Compare October 9, 2026 18:12
@flxzt
flxzt requested a review from d-e-s-o October 9, 2026 18:12
Added ability to skip running `rustfmt` on the skeleton Rust source
entirely in `libbpf-cargo` by setting `rustfmt-path` to an empty string
for the CLI or passing an empty path to `SkeletonBuilder::rustfmt()`.

@d-e-s-o d-e-s-o left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me, thanks.

@d-e-s-o
d-e-s-o merged commit ff2b750 into libbpf:master Oct 12, 2026
17 checks passed
d-e-s-o added a commit that referenced this pull request Oct 12, 2026
Improve the CHANGELOG message for #1465 and the style of some code
comments.

Signed-off-by: Daniel Müller <deso@posteo.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make rustfmt optional

2 participants