forked from bazelbuild/rules_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add target for running rustfmt directly (bazelbuild#2648)
Previously you either needed to go via our bazel-aware wrapper, or write a rule to grab the file from the toolchain.
- Loading branch information
1 parent
5b0a6a9
commit 4a3ffcb
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::path::PathBuf; | ||
use std::process::{exit, Command}; | ||
|
||
fn main() { | ||
let runfiles = runfiles::Runfiles::create().unwrap(); | ||
|
||
let rustfmt = runfiles::rlocation!(runfiles, env!("RUSTFMT")); | ||
if !rustfmt.exists() { | ||
panic!("rustfmt does not exist at: {}", rustfmt.display()); | ||
} | ||
|
||
let working_directory = std::env::var_os("BUILD_WORKING_DIRECTORY") | ||
.map(PathBuf::from) | ||
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get working directory")); | ||
|
||
let status = Command::new(rustfmt) | ||
.current_dir(&working_directory) | ||
.args(std::env::args_os().skip(1)) | ||
.status() | ||
.expect("Failed to run rustfmt"); | ||
if let Some(exit_code) = status.code() { | ||
exit(exit_code); | ||
} | ||
exit_for_signal(&status); | ||
panic!("Child rustfmt process didn't exit or get a signal - don't know how to handle it"); | ||
} | ||
|
||
#[cfg(target_family = "unix")] | ||
fn exit_for_signal(status: &std::process::ExitStatus) { | ||
use std::os::unix::process::ExitStatusExt; | ||
if let Some(signal) = status.signal() { | ||
exit(signal); | ||
} | ||
} | ||
|
||
#[cfg(not(target_family = "unix"))] | ||
#[allow(unused)] | ||
fn exit_for_signal(status: &std::process::ExitStatus) {} |