Skip to content

Commit

Permalink
feat(Turborepo): Initial run preamble (#6205)
Browse files Browse the repository at this point in the history
Co-authored-by: Greg Soltis <Greg Soltis>
  • Loading branch information
Greg Soltis authored Oct 18, 2023
1 parent ef2b800 commit 9e36691
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 33 deletions.
21 changes: 10 additions & 11 deletions crates/turborepo-lib/src/run/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,24 @@ impl TaskCache {
)
.await?;

let notify_result = match self.daemon_client.as_mut() {
Some(daemon_client) => daemon_client
if let Some(daemon_client) = self.daemon_client.as_mut() {
let notify_result = daemon_client
.notify_outputs_written(
self.hash.to_string(),
self.repo_relative_globs.inclusions.clone(),
self.repo_relative_globs.exclusions.clone(),
duration.as_millis() as u64,
)
.await
.map_err(Error::from),
None => Err(Error::NoDaemon),
};
.map_err(Error::from);

if let Err(err) = notify_result {
let task_id = &self.task_id;
warn!("Failed to mark outputs as cached for {task_id}: {err}");
prefixed_ui.warn(format!(
"Failed to mark outputs as cached for {task_id}: {err}",
));
if let Err(err) = notify_result {
let task_id = &self.task_id;
warn!("Failed to mark outputs as cached for {task_id}: {err}");
prefixed_ui.warn(format!(
"Failed to mark outputs as cached for {task_id}: {err}",
));
}
}

self.expanded_outputs = relative_paths;
Expand Down
74 changes: 54 additions & 20 deletions crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use turborepo_cache::{AsyncCache, RemoteCacheOpts};
use turborepo_env::EnvironmentVariableMap;
use turborepo_repository::package_json::PackageJson;
use turborepo_scm::SCM;
use turborepo_ui::ColorSelector;
use turborepo_ui::{cprint, cprintln, ColorSelector, BOLD_GREY, GREY};

use self::task_id::TaskName;
use crate::{
Expand Down Expand Up @@ -73,13 +73,34 @@ impl<'a> Run<'a> {
"performing run on {:?}",
TurboState::platform_name(),
);

let start_at = Local::now();
let package_json_path = self.base.repo_root.join_component("package.json");
let root_package_json =
PackageJson::load(&package_json_path).context("failed to read package.json")?;
let mut opts = self.opts()?;

let config = self.base.config()?;
let team_id = config.team_id();
let team_slug = config.team_slug();
let token = config.token();

let api_auth = team_id.zip(token).map(|(team_id, token)| APIAuth {
team_id: team_id.to_string(),
token: token.to_string(),
team_slug: team_slug.map(|s| s.to_string()),
});
let api_client = self.base.api_client()?;

// Pulled from initAnalyticsClient in run.go
let is_linked = api_auth.is_some();
if !is_linked {
opts.cache_opts.skip_remote = true;
} else if let Some(enabled) = config.enabled {
// We're linked, but if the user has explicitly enabled or disabled, use that
// value
opts.cache_opts.skip_remote = !enabled;
}

let _is_structured_output = opts.run_opts.graph.is_some() || opts.run_opts.dry_run_json;

let is_single_package = opts.run_opts.single_package;
Expand Down Expand Up @@ -110,10 +131,10 @@ impl<'a> Run<'a> {
}

// There's some warning handling code in Go that I'm ignoring
let is_ci_and_not_tty = turborepo_ci::is_ci() && !std::io::stdout().is_terminal();
let is_ci_or_not_tty = turborepo_ci::is_ci() || !std::io::stdout().is_terminal();

let mut daemon = None;
if is_ci_and_not_tty && !opts.run_opts.no_daemon {
if is_ci_or_not_tty && !opts.run_opts.no_daemon {
info!("skipping turbod since we appear to be in a non-interactive context");
} else if !opts.run_opts.no_daemon {
let connector = DaemonConnector {
Expand Down Expand Up @@ -160,25 +181,38 @@ impl<'a> Run<'a> {
filtered_pkgs
};

let env_at_execution_start = EnvironmentVariableMap::infer();
let targets_list = opts.run_opts.tasks.join(", ");
if opts.run_opts.single_package {
cprint!(self.base.ui, GREY, "{}", "• Running");
cprint!(self.base.ui, BOLD_GREY, " {}\n", targets_list);
} else {
let mut packages = filtered_pkgs
.iter()
.map(|workspace_name| workspace_name.to_string())
.collect::<Vec<String>>();
packages.sort();
cprintln!(
self.base.ui,
GREY,
"• Packages in scope: {}",
packages.join(", ")
);
cprint!(self.base.ui, GREY, "{} ", "• Running");
cprint!(self.base.ui, BOLD_GREY, "{}", targets_list);
cprint!(self.base.ui, GREY, " in {} packages\n", filtered_pkgs.len());
}

let config = self.base.config()?;
let team_id = config.team_id();
let team_slug = config.team_slug();
let token = config.token();
let use_http_cache = !opts.cache_opts.skip_remote;
if use_http_cache {
cprintln!(self.base.ui, GREY, "• Remote caching enabled");
} else {
cprintln!(self.base.ui, GREY, "• Remote caching disabled");
}

let api_auth = team_id.zip(token).map(|(team_id, token)| APIAuth {
team_id: team_id.to_string(),
token: token.to_string(),
team_slug: team_slug.map(|s| s.to_string()),
});
let env_at_execution_start = EnvironmentVariableMap::infer();

let async_cache = AsyncCache::new(
&opts.cache_opts,
&self.base.repo_root,
self.base.api_client()?,
api_auth,
)?;
let async_cache =
AsyncCache::new(&opts.cache_opts, &self.base.repo_root, api_client, api_auth)?;

info!("created cache");
let engine = EngineBuilder::new(
Expand Down
16 changes: 14 additions & 2 deletions crates/turborepo-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,24 @@ macro_rules! cprintln {
}

#[macro_export]
macro_rules! cwrite {
macro_rules! cprint {
($ui:expr, $color:expr, $format_string:expr $(, $arg:expr)*) => {{
let formatted_str = format!($format_string $(, $arg)*);

let colored_str = $color.apply_to(formatted_str);

write!("{}", $ui.apply(colored_str))
print!("{}", $ui.apply(colored_str))
}};
}

#[macro_export]
macro_rules! cwrite {
($dst:expr, $ui:expr, $color:expr, $format_string:expr $(, $arg:expr)*) => {{
let formatted_str = format!($format_string $(, $arg)*);

let colored_str = $color.apply_to(formatted_str);

write!($dst, "{}", $ui.apply(colored_str))
}};
}

Expand Down Expand Up @@ -172,6 +183,7 @@ lazy_static! {
pub static ref MAGENTA: Style = Style::new().magenta();
pub static ref UNDERLINE: Style = Style::new().underlined();
pub static ref BOLD_CYAN: Style = Style::new().cyan().bold();
pub static ref BOLD_GREY: Style = Style::new().dim().bold();
}

pub const RESET: &str = "\x1b[0m";
Expand Down
2 changes: 2 additions & 0 deletions turborepo-tests/integration/tests/single_package/graph.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Graph

Graph in Rust
$ ${TURBO} run build --graph --experimental-rust-codepath
\xe2\x80\xa2 Running build (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
digraph {
\tcompound = "true" (esc)
\tnewrank = "true" (esc)
Expand Down

0 comments on commit 9e36691

Please sign in to comment.