-
Notifications
You must be signed in to change notification settings - Fork 280
transpile: Run rustfmt on generated .rs files
#1458
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,10 +11,10 @@ use serde_json::json; | |
|
|
||
| use super::compile_cmds::LinkCmd; | ||
| use super::TranspilerConfig; | ||
| use crate::get_module_name; | ||
| use crate::CrateSet; | ||
| use crate::ExternCrateDetails; | ||
| use crate::PragmaSet; | ||
| use crate::{get_module_name, rustfmt}; | ||
|
|
||
| #[derive(Debug, Copy, Clone)] | ||
| pub enum BuildDirectoryContents { | ||
|
|
@@ -225,7 +225,10 @@ fn emit_build_rs( | |
| }); | ||
| let output = reg.render("build.rs", &json).unwrap(); | ||
| let output_path = build_dir.join("build.rs"); | ||
| maybe_write_to_file(&output_path, output, tcfg.overwrite_existing) | ||
| let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?; | ||
| rustfmt(&output_path, build_dir); | ||
|
|
||
| Some(path) | ||
| } | ||
|
|
||
| /// Emit lib.rs (main.rs) for a library (binary). Returns `Some(path)` | ||
|
|
@@ -252,8 +255,10 @@ fn emit_lib_rs( | |
|
|
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we make this optional (with a command line argument)?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we not do it, though?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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? |
||
|
|
||
| maybe_write_to_file(&output_path, output, tcfg.overwrite_existing) | ||
| Some(path) | ||
| } | ||
|
|
||
| /// If we translate variadic functions, the output will only compile | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -620,6 +620,8 @@ fn transpile_single( | |
| ), | ||
| }; | ||
|
|
||
| rustfmt(&output_path, build_dir); | ||
|
|
||
| Ok((output_path, pragmas, crates)) | ||
| } | ||
|
|
||
|
|
@@ -667,3 +669,17 @@ fn get_output_path( | |
| input_path | ||
| } | ||
| } | ||
|
|
||
| fn rustfmt(output_path: &Path, build_dir: &Path) { | ||
| let edition = "2021"; | ||
|
|
||
| let status = Command::new("rustfmt") | ||
| .args(["--edition", edition]) | ||
| .arg(output_path) | ||
| .current_dir(build_dir) | ||
| .status(); | ||
|
|
||
| if !status.map_or(false, |status| status.success()) { | ||
| warn!("rustfmt failed, code may not be well-formatted"); | ||
| } | ||
|
Comment on lines
+682
to
+684
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we handle these errors differently? The command will fail if Also, if we're calling
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.