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

port(Turborepo): Add github prefixing in Rust #6471

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
merged 8 commits into from
Nov 17, 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
9 changes: 9 additions & 0 deletions crates/turborepo-ci/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ impl Vendor {
}
}

pub fn github_header_footer(package: Option<&str>, task: &str) -> (String, String) {
let header = if let Some(package) = package {
format!("::group::{package}:{task}\n")
} else {
format!("::group::{task}\n")
};
(header, "::endgroup::\n".to_string())
}

#[cfg(test)]
mod tests {
use tracing::info;
Expand Down
9 changes: 7 additions & 2 deletions crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use cache::{RunCache, TaskCache};
use chrono::{DateTime, Local};
use itertools::Itertools;
use rayon::iter::ParallelBridge;
use tracing::{debug, info};
use tracing::debug;
use turbopath::AbsoluteSystemPathBuf;
use turborepo_analytics::{start_analytics, AnalyticsHandle, AnalyticsSender};
use turborepo_api_client::{APIAuth, APIClient};
Expand Down Expand Up @@ -423,8 +423,13 @@ impl<'a> Run<'a> {
// We hit some error, it shouldn't be exit code 0
.unwrap_or(if errors.is_empty() { 0 } else { 1 });

let error_prefix = if opts.run_opts.is_github_actions {
"::error::"
} else {
""
};
for err in &errors {
writeln!(std::io::stderr(), "{err}").ok();
writeln!(std::io::stderr(), "{error_prefix}{err}").ok();
}

visitor
Expand Down
22 changes: 17 additions & 5 deletions crates/turborepo-lib/src/task_graph/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tokio::{
};
use tracing::{debug, error, Span};
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf};
use turborepo_ci::github_header_footer;
use turborepo_env::{EnvironmentVariableMap, ResolvedEnvMode};
use turborepo_repository::{
package_graph::{PackageGraph, WorkspaceName, ROOT_PKG_NAME},
Expand Down Expand Up @@ -242,9 +243,9 @@ impl<'a> Visitor<'a> {
execution_env,
);

let output_client = self.output_client(&info);
let tracker = self.run_tracker.track_task(info.clone().into_owned());
let spaces_client = self.run_tracker.spaces_task_client();
let output_client = self.output_client();
let parent_span = Span::current();

tasks.push(tokio::spawn(async move {
Expand Down Expand Up @@ -337,7 +338,7 @@ impl<'a> Visitor<'a> {
OutputSink::new(out, err)
}

fn output_client(&self) -> OutputClient<impl std::io::Write> {
fn output_client(&self, task_id: &TaskId) -> OutputClient<impl std::io::Write> {
let behavior = match self.opts.run_opts.log_order {
crate::opts::ResolvedLogOrder::Stream if self.run_tracker.spaces_enabled() => {
turborepo_ui::OutputClientBehavior::InMemoryBuffer
Expand All @@ -347,7 +348,18 @@ impl<'a> Visitor<'a> {
}
crate::opts::ResolvedLogOrder::Grouped => turborepo_ui::OutputClientBehavior::Grouped,
};
self.sink.logger(behavior)

let mut logger = self.sink.logger(behavior);
if self.opts.run_opts.is_github_actions {
let package = if self.opts.run_opts.single_package {
None
} else {
Some(task_id.package())
};
let (header, footer) = github_header_footer(package, task_id.task());
logger.with_header_footer(Some(header), Some(footer));
}
logger
}

fn prefix<'b>(&self, task_id: &'b TaskId) -> Cow<'b, str> {
Expand Down Expand Up @@ -385,8 +397,8 @@ impl<'a> Visitor<'a> {
.with_warn_prefix(prefix);
if is_github_actions {
prefixed_ui = prefixed_ui
.with_error_prefix(Style::new().apply_to("[ERROR]".to_string()))
.with_warn_prefix(Style::new().apply_to("[WARN]".to_string()));
.with_error_prefix(Style::new().apply_to("[ERROR] ".to_string()))
.with_warn_prefix(Style::new().apply_to("[WARN] ".to_string()));
}
prefixed_ui
}
Expand Down
17 changes: 17 additions & 0 deletions crates/turborepo-ui/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct OutputClient<W> {
// Any locals held across an await must implement Sync and RwLock lets us achieve this
buffer: Option<RwLock<Vec<SinkBytes<'static>>>>,
writers: Arc<Mutex<SinkWriters<W>>>,
header: Option<String>,
footer: Option<String>,
}

pub struct OutputWriter<'a, W> {
Expand Down Expand Up @@ -80,11 +82,18 @@ impl<W: Write> OutputSink<W> {
behavior,
buffer,
writers,
header: None,
footer: None,
}
}
}

impl<W: Write> OutputClient<W> {
pub fn with_header_footer(&mut self, header: Option<String>, footer: Option<String>) {
self.header = header;
self.footer = footer;
}

/// A writer that will write to the underlying sink's out writer according
/// to this client's behavior.
pub fn stdout(&self) -> OutputWriter<W> {
Expand Down Expand Up @@ -112,6 +121,8 @@ impl<W: Write> OutputClient<W> {
behavior,
buffer,
writers,
header,
footer,
} = self;
let buffers = buffer.map(|cell| cell.into_inner().expect("lock poisoned"));

Expand All @@ -122,6 +133,9 @@ impl<W: Write> OutputClient<W> {
// We hold the mutex until we write all of the bytes associated for the client
// to ensure that the bytes aren't interspersed.
let mut writers = writers.lock().expect("lock poisoned");
if let Some(prefix) = header {
writers.out.write_all(prefix.as_bytes())?;
}
for SinkBytes {
buffer,
destination,
Expand All @@ -133,6 +147,9 @@ impl<W: Write> OutputClient<W> {
};
writer.write_all(buffer)?;
}
if let Some(suffix) = footer {
writers.out.write_all(suffix.as_bytes())?;
}
}

Ok(buffers.map(|buffers| {
Expand Down
4 changes: 2 additions & 2 deletions turborepo-tests/integration/tests/ordered/github.t
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ Verify that errors are grouped properly
npm ERR! Error: command failed
npm ERR! in workspace: util
npm ERR\! at location: (.*)/packages/util (re)
\[ERROR\] command finished with error: command \((.*)/packages/util\) npm run fail exited \(1\) (re)
\[ERROR\] command finished with error: command \((.*)/packages/util\) (.*)npm run fail exited \(1\) (re)
::endgroup::
::error::util#fail: command \(.*/packages/util\) npm run fail exited \(1\) (re)
::error::util#fail: command \(.*/packages/util\) (.*)npm run fail exited \(1\) (re)

Tasks: 0 successful, 1 total
Cached: 0 cached, 1 total
Expand Down