Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#### :bug: Bug fix

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

#### :memo: Documentation

Expand Down
10 changes: 5 additions & 5 deletions rewatch/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub fn incremental_build(
logs::finalize(&build_state.packages);

if !plain_output && show_progress {
println!(
eprintln!(
"{}{} {}Error parsing source files in {:.2}s",
LINE_CLEAR,
format_step(current_step, total_steps),
Expand All @@ -280,7 +280,7 @@ pub fn incremental_build(
pb.finish();
}

println!("{}", &err);
eprintln!("{}", &err);
return Err(IncrementalBuildError {
kind: IncrementalBuildErrorKind::SourceFileParseError,
plain_output,
Expand Down Expand Up @@ -358,9 +358,9 @@ pub fn incremental_build(
if !compile_errors.is_empty() {
if show_progress {
if plain_output {
println!("Compiled {num_compiled_modules} modules")
eprintln!("Compiled {num_compiled_modules} modules")
} else {
println!(
eprintln!(
"{}{} {}Compiled {} modules in {:.2}s",
LINE_CLEAR,
format_step(current_step, total_steps),
Expand All @@ -377,7 +377,7 @@ pub fn incremental_build(
log_config_warnings(build_state);
}
if helpers::contains_ascii_characters(&compile_errors) {
println!("{}", &compile_errors);
eprintln!("{}", &compile_errors);
}
Err(IncrementalBuildError {
kind: IncrementalBuildErrorKind::CompileError(None),
Expand Down
47 changes: 42 additions & 5 deletions rewatch/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,24 @@ fn main() -> Result<()> {

let log_level_filter = cli.verbose.log_level_filter();

env_logger::Builder::new()
let stdout_logger = env_logger::Builder::new()
.format(|buf, record| writeln!(buf, "{}:\n{}", record.level(), record.args()))
.filter_level(log_level_filter)
.target(env_logger::fmt::Target::Stdout)
.init();
.build();

let stderr_logger = env_logger::Builder::new()
.format(|buf, record| writeln!(buf, "{}:\n{}", record.level(), record.args()))
.filter_level(log_level_filter)
.target(env_logger::fmt::Target::Stderr)
.build();

log::set_max_level(log_level_filter);
log::set_boxed_logger(Box::new(SplitLogger {
Copy link
Member Author

Choose a reason for hiding this comment

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

Seems there is no better way with env_logger.

stdout: stdout_logger,
stderr: stderr_logger,
}))
.expect("Failed to initialize logger");

let mut command = cli.command;

Expand Down Expand Up @@ -62,7 +75,7 @@ fn main() -> Result<()> {
(*build_args.warn_error).clone(),
) {
Err(e) => {
println!("{:#}", e);
eprintln!("{:#}", e);
std::process::exit(1)
}
Ok(_) => {
Expand Down Expand Up @@ -98,7 +111,7 @@ fn main() -> Result<()> {
(*watch_args.warn_error).clone(),
) {
Err(e) => {
println!("{:#}", e);
eprintln!("{:#}", e);
std::process::exit(1)
}
Ok(_) => Ok(()),
Expand Down Expand Up @@ -134,9 +147,33 @@ fn main() -> Result<()> {
fn get_lock(folder: &str) -> lock::Lock {
match lock::get(folder) {
lock::Lock::Error(error) => {
println!("Could not start ReScript build: {error}");
eprintln!("Could not start ReScript build: {error}");
std::process::exit(1);
}
acquired_lock => acquired_lock,
}
}

struct SplitLogger {
stdout: env_logger::Logger,
stderr: env_logger::Logger,
}

impl log::Log for SplitLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
self.stdout.enabled(metadata) || self.stderr.enabled(metadata)
}

fn log(&self, record: &log::Record) {
if record.level() == log::Level::Error {
self.stderr.log(record);
} else {
self.stdout.log(record);
}
}

fn flush(&self) {
self.stdout.flush();
self.stderr.flush();
}
}
2 changes: 1 addition & 1 deletion rewatch/tests/lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ success "Watcher Started"

sleep 2

if rewatch build | grep 'Could not start ReScript build:' &> /dev/null;
if rewatch build 2>&1 | grep 'Could not start ReScript build:' &> /dev/null;
then
success "Lock is correctly set"
exit_watcher
Expand Down
10 changes: 5 additions & 5 deletions tests/build_tests/build_warn_as_error/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ const first_message = stripAnsi(o1.stdout)
.find(s => s === "Warning number 110");

if (!first_message) {
assert.fail(o1.stdout);
assert.fail(o1.stdout + o1.stderr);
}

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

const second_message = stripAnsi(o2.stdout)
const second_message = stripAnsi(o2.stderr)
.split("\n")
.map(s => s.trim())
.find(s => s === "Warning number 110 (configured as error)");

if (!second_message) {
assert.fail(o2.stdout);
assert.fail(o2.stdout + o2.stderr);
}

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

const third_message = stripAnsi(o3.stdout)
const third_message = stripAnsi(o3.stderr)
.split("\n")
.map(s => s.trim())
.find(s => s === "Warning number 110 (configured as error)");

if (o3.status !== 0 || third_message) {
assert.fail(o3.stdout);
assert.fail(o3.stdout + o3.stderr);
}

await execClean();
Loading