Skip to content

Conversation

@Rua
Copy link
Contributor

@Rua Rua commented Nov 11, 2025

Rather than running cargo fmt, this runs the rustfmt binary directly so that it can be used on individual files, and also when there are no build files.

@Rua Rua force-pushed the rustfmt branch 3 times, most recently from cc8a44b to f519f02 Compare November 11, 2025 18:00
Comment on lines +680 to +684
if !status.map_or(false, |status| status.success()) {
warn!("rustfmt failed, code may not be well-formatted");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we handle these errors differently? The command will fail if rustc isn't found or something, and the status will fail if rustc exits with an error. That said, they shouldn't really fail, so I think we can just panic here, maybe with an .expect.

Also, if we're calling rustfmt here, we can remove the rustfmt run in snapshots.rs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think a panic is appropriate, as this is just an extra convenience for the user and not a hard requirement of the transpilation process. The transpilation should still succeed if rustfmt is not installed or doesn't complete for whatever reason. Though unlikely, rustfmt may not be installed even if cargo and rustc are.

@Rua Rua force-pushed the rustfmt branch 2 times, most recently from 3c732f2 to 9e83e14 Compare November 12, 2025 09:28
let output_path = build_dir.join(file_name);
let output = reg.render("lib.rs", &json).unwrap();
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
rustfmt(&output_path, build_dir);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we make this optional (with a command line argument)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why would we not do it, though?

Copy link
Contributor

Choose a reason for hiding this comment

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

The user might want a custom formatting configuration. They could add that to rustfmt.toml, but that creates a race between adding that file and running the transpiler.

Another reason is performance: what if the user wants to run the transpiler, a bunch of refactoring steps, then run the formatter?

And what if the user just doesn't want to run it at all?

Copy link
Contributor

Choose a reason for hiding this comment

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

The user might want a custom formatting configuration. They could add that to rustfmt.toml, but that creates a race between adding that file and running the transpiler.

They can still run that after the transpiler (which would run rustfmt).

Another reason is performance: what if the user wants to run the transpiler, a bunch of refactoring steps, then run the formatter?

There are plenty of places in the transpiler we don't care about performance, and rustfmt is very fast. It's never going to be the bottleneck compared to the rest of the transpiler.

And what if the user just doesn't want to run it at all?

We already don't make any guarantees about the formatting of the output.

Copy link
Contributor

Choose a reason for hiding this comment

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

I realized that rustfmt could occasionally have effects, like eating a comment, and it might interact with how comment insertion will be done, so leaving it as an on-by-default option is better actually.

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.

Use rustfmt when translating

3 participants