Skip to content
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

Write summary messages to stderr when fixing via stdin (instead of omitting them) #7838

Merged
merged 1 commit into from
Oct 6, 2023
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
22 changes: 13 additions & 9 deletions crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
}
_ => Box::new(BufWriter::new(io::stdout())),
};
let stderr_writer = Box::new(BufWriter::new(io::stderr()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I move this down? Maybe. Should we write other things to stderr? Maybe.


if cli.show_settings {
commands::show_settings::show_settings(
Expand Down Expand Up @@ -392,15 +393,18 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
)?
};

// Always try to print violations (the printer itself may suppress output),
// unless we're writing fixes via stdin (in which case, the transformed
// source code goes to stdout).
if !(is_stdin && matches!(fix_mode, FixMode::Apply | FixMode::Diff)) {
if cli.statistics {
printer.write_statistics(&diagnostics, &mut writer)?;
} else {
printer.write_once(&diagnostics, &mut writer)?;
}
// Always try to print violations (though the printer itself may suppress output)
// If we're writing fixes via stdin, the transformed source code goes to the writer
// so send the summary to stderr instead
let mut summary_writer = if is_stdin && matches!(fix_mode, FixMode::Apply | FixMode::Diff) {
stderr_writer
} else {
writer
};
if cli.statistics {
printer.write_statistics(&diagnostics, &mut summary_writer)?;
} else {
printer.write_once(&diagnostics, &mut summary_writer)?;
}

if !cli.exit_zero {
Expand Down
12 changes: 12 additions & 0 deletions crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ fn stdin_fix_py() {
print(sys.version)

----- stderr -----
Found 1 error (1 fixed, 0 remaining).
"###);
}

Expand Down Expand Up @@ -317,6 +318,7 @@ fn stdin_fix_jupyter() {
"nbformat_minor": 5
}
----- stderr -----
Found 2 errors (2 fixed, 0 remaining).
"###);
}

Expand All @@ -336,6 +338,8 @@ fn stdin_fix_when_not_fixable_should_still_print_contents() {
print(sys.version)

----- stderr -----
-:3:4: F634 If test is a tuple, which is always `True`
Found 2 errors (1 fixed, 1 remaining).
"###);
}

Expand Down Expand Up @@ -961,6 +965,9 @@ fn fix_applies_safe_fixes_by_default() {
print('foo')

----- stderr -----
-:1:14: F601 Dictionary key literal `'a'` repeated
Found 2 errors (1 fixed, 1 remaining).
1 hidden fix can be enabled with the `--unsafe-fixes` option.
"###);
}

Expand Down Expand Up @@ -988,6 +995,7 @@ fn fix_applies_unsafe_fixes_with_opt_in() {
print('foo')

----- stderr -----
Found 2 errors (2 fixed, 0 remaining).
"###);
}

Expand All @@ -1014,6 +1022,7 @@ fn fix_only_flag_applies_safe_fixes_by_default() {
print('foo')

----- stderr -----
Fixed 1 error.
"###);
}

Expand Down Expand Up @@ -1041,6 +1050,7 @@ fn fix_only_flag_applies_unsafe_fixes_with_opt_in() {
print('foo')

----- stderr -----
Fixed 2 errors.
"###);
}

Expand Down Expand Up @@ -1070,6 +1080,7 @@ fn diff_shows_safe_fixes_by_default() {


----- stderr -----
Would fix 1 error.
"###
);
}
Expand Down Expand Up @@ -1102,6 +1113,7 @@ fn diff_shows_unsafe_fixes_with_opt_in() {


----- stderr -----
Would fix 2 errors.
"###
);
}