Skip to content

Commit 2e7bef2

Browse files
grievejiafacebook-github-bot
authored andcommitted
Make --output-format=json take effect when --output is not specified
Summary: When `--output-format=json` is specified, the JSON formatting currently only takes effect when we write type errors to files. If type errors get written into console, the flag would get ignored. See [Github Issue](#157) . This diff attempts to make `--output-format` consistently apply both in file writing and in console writing. NOTE: For JSON format, I'm directly the console output to `stdout` so we could avoid clobbering with the `tracing` logger outputs and so downstream CLI tooling could have an easier way of extracting the data. For text format, I'm leaving the default console output to `stderr` still since I sense that one would be a more contentious decision and expect it to be addressed separately. Reviewed By: ndmitchell Differential Revision: D74208375 fbshipit-source-id: e3f5aae791f2854a64eee530a294b593df7c2839
1 parent 2619cd2 commit 2e7bef2

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

pyrefly/lib/commands/check.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,50 @@ impl OutputFormat {
158158
Ok(())
159159
}
160160

161+
fn write_error_text_to_console(errors: &[Error]) -> anyhow::Result<()> {
162+
print_errors(errors);
163+
Ok(())
164+
}
165+
166+
fn write_error_json(writer: &mut impl Write, errors: &[Error]) -> anyhow::Result<()> {
167+
let legacy_errors = LegacyErrors::from_errors(errors);
168+
serde_json::to_writer_pretty(writer, &legacy_errors)?;
169+
Ok(())
170+
}
171+
172+
fn buffered_write_error_json(writer: impl Write, errors: &[Error]) -> anyhow::Result<()> {
173+
let mut writer = BufWriter::new(writer);
174+
Self::write_error_json(&mut writer, errors)?;
175+
writer.flush()?;
176+
Ok(())
177+
}
178+
161179
fn write_error_json_to_file(path: &Path, errors: &[Error]) -> anyhow::Result<()> {
162180
fn f(path: &Path, errors: &[Error]) -> anyhow::Result<()> {
163-
let legacy_errors = LegacyErrors::from_errors(errors);
164-
let mut file = BufWriter::new(File::create(path)?);
165-
serde_json::to_writer_pretty(&mut file, &legacy_errors)?;
166-
Ok(file.flush()?)
181+
let file = File::create(path)?;
182+
OutputFormat::buffered_write_error_json(file, errors)
167183
}
168184
f(path, errors)
169185
.with_context(|| format!("while writing JSON errors to `{}`", path.display()))
170186
}
171187

188+
fn write_error_json_to_console(errors: &[Error]) -> anyhow::Result<()> {
189+
Self::buffered_write_error_json(std::io::stdout(), errors)
190+
}
191+
172192
fn write_errors_to_file(&self, path: &Path, errors: &[Error]) -> anyhow::Result<()> {
173193
match self {
174194
Self::Text => Self::write_error_text_to_file(path, errors),
175195
Self::Json => Self::write_error_json_to_file(path, errors),
176196
}
177197
}
198+
199+
fn write_errors_to_console(&self, errors: &[Error]) -> anyhow::Result<()> {
200+
match self {
201+
Self::Text => Self::write_error_text_to_console(errors),
202+
Self::Json => Self::write_error_json_to_console(errors),
203+
}
204+
}
178205
}
179206

180207
/// A data structure to facilitate the creation of handles for all the files we want to check.
@@ -523,7 +550,7 @@ impl Args {
523550
self.output_format
524551
.write_errors_to_file(path, &errors.shown)?;
525552
} else {
526-
print_errors(&errors.shown);
553+
self.output_format.write_errors_to_console(&errors.shown)?;
527554
}
528555
memory_trace.stop();
529556
if let Some(limit) = self.count_errors {

test/basic.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ $ $PYREFLY check $TEST_PY -a
1616
[0]
1717
```
1818

19+
## JSON output
20+
21+
```scrut
22+
$ echo "x: str = 42" > $TMPDIR/test.py && $PYREFLY check $TMPDIR/test.py --output-format json | $JQ '.[] | length'
23+
1
24+
[0]
25+
```
26+
1927
## Error on a non-existent file
2028

2129
```scrut {output_stream: stderr}

0 commit comments

Comments
 (0)