Skip to content

Commit 6d06970

Browse files
committed
fix(fmt): return diff if source from stdin
1 parent 918eb6a commit 6d06970

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

crates/forge/src/cmd/fmt.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,19 @@ impl FmtArgs {
127127
let path = source_unit.file.name.as_real();
128128
let original = source_unit.file.src.as_str();
129129
let formatted = forge_fmt::format_ast(gcx, source_unit, fmt_config.clone())?;
130+
let from_stdin = path.is_none();
131+
132+
// Return formatted code when read from stdin without check or raw switch.
133+
// <https://github.com/foundry-rs/foundry/issues/11871>
134+
if from_stdin && !self.check && !self.raw {
135+
return Some(Ok(formatted));
136+
}
130137

131138
if original == formatted {
132139
return None;
133140
}
134141

135-
if self.check || path.is_none() {
142+
if self.check || from_stdin {
136143
let summary = if self.raw {
137144
formatted
138145
} else {

crates/forge/tests/cli/fmt.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,12 @@ Diff in stdin:
8989
9090
"#]]);
9191
});
92+
93+
// Test that original is returned if read from stdin and no diff.
94+
// <https://github.com/foundry-rs/foundry/issues/11871>
95+
forgetest!(fmt_stdin_original, |_prj, cmd| {
96+
cmd.args(["fmt", "-"]);
97+
98+
cmd.stdin(FORMATTED.as_bytes());
99+
cmd.assert_success().stdout_eq(FORMATTED.as_bytes());
100+
});

0 commit comments

Comments
 (0)