Skip to content

Commit 3c25d26

Browse files
authored
Write summary messages to stderr when fixing via stdin (instead of omitting them) (#7838)
Previously we just omitted diagnostic summaries when using `--fix` or `--diff` with a stdin file. Now, we still write the summaries to stderr instead of the main writer (which is generally stdout but could be changed by `--output-file`).
1 parent 4f95df1 commit 3c25d26

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

crates/ruff_cli/src/lib.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
208208
}
209209
_ => Box::new(BufWriter::new(io::stdout())),
210210
};
211+
let stderr_writer = Box::new(BufWriter::new(io::stderr()));
211212

212213
if cli.show_settings {
213214
commands::show_settings::show_settings(
@@ -392,15 +393,18 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
392393
)?
393394
};
394395

395-
// Always try to print violations (the printer itself may suppress output),
396-
// unless we're writing fixes via stdin (in which case, the transformed
397-
// source code goes to stdout).
398-
if !(is_stdin && matches!(fix_mode, FixMode::Apply | FixMode::Diff)) {
399-
if cli.statistics {
400-
printer.write_statistics(&diagnostics, &mut writer)?;
401-
} else {
402-
printer.write_once(&diagnostics, &mut writer)?;
403-
}
396+
// Always try to print violations (though the printer itself may suppress output)
397+
// If we're writing fixes via stdin, the transformed source code goes to the writer
398+
// so send the summary to stderr instead
399+
let mut summary_writer = if is_stdin && matches!(fix_mode, FixMode::Apply | FixMode::Diff) {
400+
stderr_writer
401+
} else {
402+
writer
403+
};
404+
if cli.statistics {
405+
printer.write_statistics(&diagnostics, &mut summary_writer)?;
406+
} else {
407+
printer.write_once(&diagnostics, &mut summary_writer)?;
404408
}
405409

406410
if !cli.exit_zero {

crates/ruff_cli/tests/integration_test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ fn stdin_fix_py() {
150150
print(sys.version)
151151
152152
----- stderr -----
153+
Found 1 error (1 fixed, 0 remaining).
153154
"###);
154155
}
155156

@@ -317,6 +318,7 @@ fn stdin_fix_jupyter() {
317318
"nbformat_minor": 5
318319
}
319320
----- stderr -----
321+
Found 2 errors (2 fixed, 0 remaining).
320322
"###);
321323
}
322324

@@ -336,6 +338,8 @@ fn stdin_fix_when_not_fixable_should_still_print_contents() {
336338
print(sys.version)
337339
338340
----- stderr -----
341+
-:3:4: F634 If test is a tuple, which is always `True`
342+
Found 2 errors (1 fixed, 1 remaining).
339343
"###);
340344
}
341345

@@ -961,6 +965,9 @@ fn fix_applies_safe_fixes_by_default() {
961965
print('foo')
962966
963967
----- stderr -----
968+
-:1:14: F601 Dictionary key literal `'a'` repeated
969+
Found 2 errors (1 fixed, 1 remaining).
970+
1 hidden fix can be enabled with the `--unsafe-fixes` option.
964971
"###);
965972
}
966973

@@ -988,6 +995,7 @@ fn fix_applies_unsafe_fixes_with_opt_in() {
988995
print('foo')
989996
990997
----- stderr -----
998+
Found 2 errors (2 fixed, 0 remaining).
991999
"###);
9921000
}
9931001

@@ -1014,6 +1022,7 @@ fn fix_only_flag_applies_safe_fixes_by_default() {
10141022
print('foo')
10151023
10161024
----- stderr -----
1025+
Fixed 1 error.
10171026
"###);
10181027
}
10191028

@@ -1041,6 +1050,7 @@ fn fix_only_flag_applies_unsafe_fixes_with_opt_in() {
10411050
print('foo')
10421051
10431052
----- stderr -----
1053+
Fixed 2 errors.
10441054
"###);
10451055
}
10461056

@@ -1070,6 +1080,7 @@ fn diff_shows_safe_fixes_by_default() {
10701080
10711081
10721082
----- stderr -----
1083+
Would fix 1 error.
10731084
"###
10741085
);
10751086
}
@@ -1102,6 +1113,7 @@ fn diff_shows_unsafe_fixes_with_opt_in() {
11021113
11031114
11041115
----- stderr -----
1116+
Would fix 2 errors.
11051117
"###
11061118
);
11071119
}

0 commit comments

Comments
 (0)