Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/forge/src/cmd/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,19 @@ impl FmtArgs {
let path = source_unit.file.name.as_real();
let original = source_unit.file.src.as_str();
let formatted = forge_fmt::format_ast(gcx, source_unit, fmt_config.clone())?;
let from_stdin = path.is_none();

// Return formatted code when read from stdin without check or raw switch.
// <https://github.com/foundry-rs/foundry/issues/11871>
if from_stdin && !self.check && !self.raw {
return Some(Ok(formatted));
}

if original == formatted {
return None;
}

if self.check || path.is_none() {
if self.check || from_stdin {
let summary = if self.raw {
formatted
} else {
Expand Down
9 changes: 9 additions & 0 deletions crates/forge/tests/cli/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,12 @@ Diff in stdin:

"#]]);
});

// Test that original is returned if read from stdin and no diff.
// <https://github.com/foundry-rs/foundry/issues/11871>
forgetest!(fmt_stdin_original, |_prj, cmd| {
cmd.args(["fmt", "-"]);

cmd.stdin(FORMATTED.as_bytes());
cmd.assert_success().stdout_eq(FORMATTED.as_bytes());
});
Loading