Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(watch): caching #9815

Merged
merged 2 commits into from
Jan 27, 2025
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
74 changes: 53 additions & 21 deletions crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,9 @@ impl Args {
clap::error::ErrorKind::UnknownArgument,
"Cannot use run arguments outside of run command",
))
} else if self.execution_args.is_some() && matches!(self.command, Some(Command::Watch(_))) {
} else if self.execution_args.is_some()
&& matches!(self.command, Some(Command::Watch { .. }))
{
let mut cmd = Self::command();
Err(cmd.error(
clap::error::ErrorKind::ArgumentConflict,
Expand Down Expand Up @@ -568,9 +570,7 @@ pub enum Command {
filter: Vec<String>,
},
/// Generate the autocompletion script for the specified shell
Completion {
shell: Shell,
},
Completion { shell: Shell },
/// Runs the Turborepo background daemon
Daemon {
/// Set the idle timeout for turbod
Expand Down Expand Up @@ -708,7 +708,13 @@ pub enum Command {
/// The query to run, either a file path or a query string
query: Option<String>,
},
Watch(Box<ExecutionArgs>),
Watch {
#[clap(flatten)]
execution_args: Box<ExecutionArgs>,
/// EXPERIMENTAL: Write to cache in watch mode.
#[clap(long)]
experimental_write_cache: bool,
},
/// Unlink the current directory from your Vercel organization and disable
/// Remote Caching
Unlink {
Expand Down Expand Up @@ -1209,7 +1215,7 @@ pub async fn run(
run_args: _,
execution_args,
}
| Command::Watch(execution_args) => {
| Command::Watch { execution_args, .. } => {
// Don't overwrite the flag if it's already been set for whatever reason
execution_args.single_package = execution_args.single_package
|| repo_state
Expand Down Expand Up @@ -1507,7 +1513,10 @@ pub async fn run(

Ok(query)
}
Command::Watch(execution_args) => {
Command::Watch {
execution_args,
experimental_write_cache,
} => {
let event = CommandEventBuilder::new("watch").with_parent(&root_telemetry);
event.track_call();
let base = CommandBase::new(cli_args.clone(), repo_root, version, color_config)?;
Expand All @@ -1518,7 +1527,7 @@ pub async fn run(
return Ok(1);
}

let mut client = WatchClient::new(base, event).await?;
let mut client = WatchClient::new(base, *experimental_write_cache, event).await?;
if let Err(e) = client.start().await {
client.shutdown().await;
return Err(e.into());
Expand Down Expand Up @@ -2331,37 +2340,60 @@ mod test {
#[test_case::test_case(
&["turbo", "watch", "build"],
Args {
command: Some(Command::Watch(Box::new(ExecutionArgs {
tasks: vec!["build".to_string()],
..get_default_execution_args()
}))),
command: Some(Command::Watch {
execution_args: Box::new(ExecutionArgs {
tasks: vec!["build".to_string()],
..get_default_execution_args()
}),
experimental_write_cache: false
}),
..Args::default()
};
"default watch"
)]
#[test_case::test_case(
&["turbo", "watch", "build", "--cache-dir", "foobar"],
Args {
command: Some(Command::Watch(Box::new(ExecutionArgs {
tasks: vec!["build".to_string()],
cache_dir: Some(Utf8PathBuf::from("foobar")),
..get_default_execution_args()
}))),
command: Some(Command::Watch {
execution_args: Box::new(ExecutionArgs {
tasks: vec!["build".to_string()],
cache_dir: Some(Utf8PathBuf::from("foobar")),
..get_default_execution_args()
}),
experimental_write_cache: false
}),
..Args::default()
};
"with cache-dir"
)]
#[test_case::test_case(
&["turbo", "watch", "build", "lint", "check"],
Args {
command: Some(Command::Watch(Box::new(ExecutionArgs {
tasks: vec!["build".to_string(), "lint".to_string(), "check".to_string()],
..get_default_execution_args()
}))),
command: Some(Command::Watch {
execution_args: Box::new(ExecutionArgs {
tasks: vec!["build".to_string(), "lint".to_string(), "check".to_string()],
..get_default_execution_args()
}),
experimental_write_cache: false
}),
..Args::default()
};
"with multiple tasks"
)]
#[test_case::test_case(
&["turbo", "watch", "build", "--experimental-write-cache"],
Args {
command: Some(Command::Watch {
execution_args: Box::new(ExecutionArgs {
tasks: vec!["build".to_string()],
..get_default_execution_args()
}),
experimental_write_cache: true
}),
..Args::default()
};
"with experimental-write-cache"
)]
fn test_parse_watch(args: &[&str], expected: Args) {
assert_eq!(Args::try_parse_from(args).unwrap(), expected);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Opts {
run_args,
execution_args,
}) => (execution_args, run_args),
Some(Command::Watch(execution_args)) => (execution_args, &Box::default()),
Some(Command::Watch { execution_args, .. }) => (execution_args, &Box::default()),
Some(Command::Ls {
affected, filter, ..
}) => {
Expand Down
20 changes: 15 additions & 5 deletions crates/turborepo-lib/src/run/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct WatchClient {
handler: SignalHandler,
ui_sender: Option<UISender>,
ui_handle: Option<JoinHandle<Result<(), turborepo_ui::Error>>>,
experimental_write_cache: bool,
}

struct RunHandle {
Expand Down Expand Up @@ -109,7 +110,11 @@ pub enum Error {
}

impl WatchClient {
pub async fn new(base: CommandBase, telemetry: CommandEventBuilder) -> Result<Self, Error> {
pub async fn new(
base: CommandBase,
experimental_write_cache: bool,
telemetry: CommandEventBuilder,
) -> Result<Self, Error> {
let signal = commands::run::get_signal()?;
let handler = SignalHandler::new(signal);

Expand Down Expand Up @@ -147,6 +152,7 @@ impl WatchClient {
connector,
handler,
telemetry,
experimental_write_cache,
persistent_tasks_handle: None,
ui_sender,
ui_handle,
Expand Down Expand Up @@ -285,8 +291,10 @@ impl WatchClient {
.collect();

let mut opts = self.base.opts().clone();
opts.cache_opts.cache.remote.write = false;
opts.cache_opts.cache.local.write = false;
if !self.experimental_write_cache {
opts.cache_opts.cache.remote.write = false;
opts.cache_opts.cache.remote.read = false;
}

let new_base = CommandBase::from_opts(
opts,
Expand Down Expand Up @@ -319,8 +327,10 @@ impl WatchClient {
}
ChangedPackages::All => {
let mut opts = self.base.opts().clone();
opts.cache_opts.cache.remote.write = false;
opts.cache_opts.cache.local.write = false;
if !self.experimental_write_cache {
opts.cache_opts.cache.remote.write = false;
opts.cache_opts.cache.remote.read = false;
}

let base = CommandBase::from_opts(
opts,
Expand Down
Loading