Skip to content

Commit d04afe6

Browse files
authored
Rewatch: log errors to stderr (#8147)
* Rewatch: log errors to stderr * CHANGELOG
1 parent 66ba3f8 commit d04afe6

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#### :bug: Bug fix
2525

2626
- Fix rewatch swallowing parse warnings (%todo). https://github.com/rescript-lang/rescript/pull/8135
27+
- Rewatch: log errors to `stderr`. https://github.com/rescript-lang/rescript/pull/8147
2728

2829
#### :memo: Documentation
2930

rewatch/src/build.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ pub fn incremental_build(
270270
logs::finalize(&build_state.packages);
271271

272272
if !plain_output && show_progress {
273-
println!(
273+
eprintln!(
274274
"{}{} {}Error parsing source files in {:.2}s",
275275
LINE_CLEAR,
276276
format_step(current_step, total_steps),
@@ -280,7 +280,7 @@ pub fn incremental_build(
280280
pb.finish();
281281
}
282282

283-
println!("{}", &err);
283+
eprintln!("{}", &err);
284284
return Err(IncrementalBuildError {
285285
kind: IncrementalBuildErrorKind::SourceFileParseError,
286286
plain_output,
@@ -358,9 +358,9 @@ pub fn incremental_build(
358358
if !compile_errors.is_empty() {
359359
if show_progress {
360360
if plain_output {
361-
println!("Compiled {num_compiled_modules} modules")
361+
eprintln!("Compiled {num_compiled_modules} modules")
362362
} else {
363-
println!(
363+
eprintln!(
364364
"{}{} {}Compiled {} modules in {:.2}s",
365365
LINE_CLEAR,
366366
format_step(current_step, total_steps),
@@ -377,7 +377,7 @@ pub fn incremental_build(
377377
log_config_warnings(build_state);
378378
}
379379
if helpers::contains_ascii_characters(&compile_errors) {
380-
println!("{}", &compile_errors);
380+
eprintln!("{}", &compile_errors);
381381
}
382382
Err(IncrementalBuildError {
383383
kind: IncrementalBuildErrorKind::CompileError(None),

rewatch/src/main.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,24 @@ fn main() -> Result<()> {
1010

1111
let log_level_filter = cli.verbose.log_level_filter();
1212

13-
env_logger::Builder::new()
13+
let stdout_logger = env_logger::Builder::new()
1414
.format(|buf, record| writeln!(buf, "{}:\n{}", record.level(), record.args()))
1515
.filter_level(log_level_filter)
1616
.target(env_logger::fmt::Target::Stdout)
17-
.init();
17+
.build();
18+
19+
let stderr_logger = env_logger::Builder::new()
20+
.format(|buf, record| writeln!(buf, "{}:\n{}", record.level(), record.args()))
21+
.filter_level(log_level_filter)
22+
.target(env_logger::fmt::Target::Stderr)
23+
.build();
24+
25+
log::set_max_level(log_level_filter);
26+
log::set_boxed_logger(Box::new(SplitLogger {
27+
stdout: stdout_logger,
28+
stderr: stderr_logger,
29+
}))
30+
.expect("Failed to initialize logger");
1831

1932
let mut command = cli.command;
2033

@@ -62,7 +75,7 @@ fn main() -> Result<()> {
6275
(*build_args.warn_error).clone(),
6376
) {
6477
Err(e) => {
65-
println!("{:#}", e);
78+
eprintln!("{:#}", e);
6679
std::process::exit(1)
6780
}
6881
Ok(_) => {
@@ -98,7 +111,7 @@ fn main() -> Result<()> {
98111
(*watch_args.warn_error).clone(),
99112
) {
100113
Err(e) => {
101-
println!("{:#}", e);
114+
eprintln!("{:#}", e);
102115
std::process::exit(1)
103116
}
104117
Ok(_) => Ok(()),
@@ -134,9 +147,33 @@ fn main() -> Result<()> {
134147
fn get_lock(folder: &str) -> lock::Lock {
135148
match lock::get(folder) {
136149
lock::Lock::Error(error) => {
137-
println!("Could not start ReScript build: {error}");
150+
eprintln!("Could not start ReScript build: {error}");
138151
std::process::exit(1);
139152
}
140153
acquired_lock => acquired_lock,
141154
}
142155
}
156+
157+
struct SplitLogger {
158+
stdout: env_logger::Logger,
159+
stderr: env_logger::Logger,
160+
}
161+
162+
impl log::Log for SplitLogger {
163+
fn enabled(&self, metadata: &log::Metadata) -> bool {
164+
self.stdout.enabled(metadata) || self.stderr.enabled(metadata)
165+
}
166+
167+
fn log(&self, record: &log::Record) {
168+
if record.level() == log::Level::Error {
169+
self.stderr.log(record);
170+
} else {
171+
self.stdout.log(record);
172+
}
173+
}
174+
175+
fn flush(&self) {
176+
self.stdout.flush();
177+
self.stderr.flush();
178+
}
179+
}

rewatch/tests/lock.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ success "Watcher Started"
2525

2626
sleep 2
2727

28-
if rewatch build | grep 'Could not start ReScript build:' &> /dev/null;
28+
if rewatch build 2>&1 | grep 'Could not start ReScript build:' &> /dev/null;
2929
then
3030
success "Lock is correctly set"
3131
exit_watcher

tests/build_tests/build_warn_as_error/input.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,32 @@ const first_message = stripAnsi(o1.stdout)
1414
.find(s => s === "Warning number 110");
1515

1616
if (!first_message) {
17-
assert.fail(o1.stdout);
17+
assert.fail(o1.stdout + o1.stderr);
1818
}
1919

2020
// Second build using --warn-error +110
2121
const o2 = await execBuild(["--warn-error", "+110"]);
2222

23-
const second_message = stripAnsi(o2.stdout)
23+
const second_message = stripAnsi(o2.stderr)
2424
.split("\n")
2525
.map(s => s.trim())
2626
.find(s => s === "Warning number 110 (configured as error)");
2727

2828
if (!second_message) {
29-
assert.fail(o2.stdout);
29+
assert.fail(o2.stdout + o2.stderr);
3030
}
3131

3232
// Third build, without --warn-error +110
3333
// The result should not be a warning as error
3434
const o3 = await execBuild();
3535

36-
const third_message = stripAnsi(o3.stdout)
36+
const third_message = stripAnsi(o3.stderr)
3737
.split("\n")
3838
.map(s => s.trim())
3939
.find(s => s === "Warning number 110 (configured as error)");
4040

4141
if (o3.status !== 0 || third_message) {
42-
assert.fail(o3.stdout);
42+
assert.fail(o3.stdout + o3.stderr);
4343
}
4444

4545
await execClean();

0 commit comments

Comments
 (0)