这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(tui): replay all logs sent to tui with forced crlf #9962

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

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
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/task_graph/visitor/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<W: Write> CacheOutput for TaskCacheOutput<W> {
let writer = direct.output_prefixed_writer();
turborepo_ui::replay_logs(writer, log_file)
}
TaskCacheOutput::UI(task) => turborepo_ui::replay_logs(task, log_file),
TaskCacheOutput::UI(task) => turborepo_ui::replay_logs_with_crlf(task, log_file),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use thiserror::Error;
pub use crate::{
color_selector::ColorSelector,
line::LineWriter,
logs::{replay_logs, LogWriter},
logs::{replay_logs, replay_logs_with_crlf, LogWriter},
output::{OutputClient, OutputClientBehavior, OutputSink, OutputWriter},
prefixed::{PrefixedUI, PrefixedWriter},
tui::{TaskTable, TerminalPane},
Expand Down
75 changes: 74 additions & 1 deletion crates/turborepo-ui/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,48 @@ pub fn replay_logs<W: Write>(
Ok(())
}

/// Replay logs, but enforce crlf line endings
// TODO: refactor to share code with `replay_logs`
pub fn replay_logs_with_crlf<W: Write>(
mut output: W,
log_file_name: &AbsoluteSystemPath,
) -> Result<(), Error> {
debug!("start replaying logs");

let log_file = File::open(log_file_name).map_err(|err| {
warn!("error opening log file: {:?}", err);
Error::CannotReadLogs(err)
})?;

let mut log_reader = BufReader::new(log_file);

let mut buffer = Vec::new();
loop {
let num_bytes = log_reader
.read_until(b'\n', &mut buffer)
.map_err(Error::CannotReadLogs)?;
if num_bytes == 0 {
break;
}

let line_without_lf = buffer.strip_suffix(b"\n").unwrap_or(&buffer);
let line_without_crlf = line_without_lf
.strip_suffix(b"\r")
.unwrap_or(line_without_lf);

output
.write_all(line_without_crlf)
.map_err(Error::CannotReadLogs)?;
output.write_all(b"\r\n").map_err(Error::CannotReadLogs)?;

buffer.clear();
}

debug!("finish replaying logs");

Ok(())
}

#[cfg(test)]
mod tests {
use std::{fs, io::Write};
Expand All @@ -128,7 +170,8 @@ mod tests {
use turbopath::AbsoluteSystemPathBuf;

use crate::{
logs::replay_logs, ColorConfig, LogWriter, PrefixedUI, PrefixedWriter, BOLD, CYAN,
logs::replay_logs, replay_logs_with_crlf, ColorConfig, LogWriter, PrefixedUI,
PrefixedWriter, BOLD, CYAN,
};

#[test]
Expand Down Expand Up @@ -207,4 +250,34 @@ mod tests {
assert_eq!(output, [b'>', 0, 159, 146, 150, b'\n']);
Ok(())
}

#[test]
fn test_replay_logs_crlf() -> Result<()> {
let dir = tempdir()?;
let log_file_path = AbsoluteSystemPathBuf::try_from(dir.path().join("test.txt"))?;
fs::write(&log_file_path, "\none fish\ntwo fish\nred fish\nblue fish")?;
let mut output = Vec::new();
replay_logs_with_crlf(&mut output, &log_file_path)?;
let output_str = std::str::from_utf8(&output)?;
assert_eq!(
output_str,
"\r\none fish\r\ntwo fish\r\nred fish\r\nblue fish\r\n"
);

Ok(())
}

#[test]
fn test_replay_logs_crlf_noop() -> Result<()> {
let dir = tempdir()?;
let log_file_path = AbsoluteSystemPathBuf::try_from(dir.path().join("test.txt"))?;
let contents = "\r\none fish\r\ntwo fish\r\nred fish\r\nblue fish\r\n";
fs::write(&log_file_path, contents)?;
let mut output = Vec::new();
replay_logs_with_crlf(&mut output, &log_file_path)?;
let output_str = std::str::from_utf8(&output)?;
assert_eq!(output_str, contents,);

Ok(())
}
}
Loading