Skip to content

Commit

Permalink
fix(turborepo): use transitive closure of filtered packages in watch …
Browse files Browse the repository at this point in the history
…mode (#8161)

### Description

We were using the set of filtered packages to limit change events.
That's not exactly right since we need to account for the dependencies
of filtered packages as well. Therefore we take the transitive closure
of the filtered packages and restrict package change events to that set.

### Testing Instructions
  • Loading branch information
NicholasLYang authored May 17, 2024
1 parent 11e59b9 commit ef69b7e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 19 additions & 1 deletion crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use turbopath::AbsoluteSystemPathBuf;
use turborepo_api_client::{APIAuth, APIClient};
use turborepo_ci::Vendor;
use turborepo_env::EnvironmentVariableMap;
use turborepo_repository::package_graph::{PackageGraph, PackageName};
use turborepo_repository::package_graph::{PackageGraph, PackageName, PackageNode};
use turborepo_scm::SCM;
use turborepo_telemetry::events::generic::GenericEventBuilder;
use turborepo_ui::{cprint, cprintln, tui, tui::AppSender, BOLD_GREY, GREY, UI};
Expand Down Expand Up @@ -118,6 +118,24 @@ impl Run {
new_run
}

// Produces the transitive closure of the filtered packages,
// i.e. the packages relevant for this run.
pub fn get_relevant_packages(&self) -> HashSet<PackageName> {
let packages: Vec<_> = self
.filtered_pkgs
.iter()
.map(|pkg| PackageNode::Workspace(pkg.clone()))
.collect();
self.pkg_dep_graph
.transitive_closure(&packages)
.into_iter()
.filter_map(|node| match node {
PackageNode::Root => None,
PackageNode::Workspace(pkg) => Some(pkg.clone()),
})
.collect()
}

pub fn has_experimental_ui(&self) -> bool {
self.experimental_ui
}
Expand Down
11 changes: 9 additions & 2 deletions crates/turborepo-lib/src/run/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
DaemonConnector, DaemonPaths,
};

#[derive(Debug)]
enum ChangedPackages {
All,
Some(HashSet<PackageName>),
Expand All @@ -45,6 +46,7 @@ impl ChangedPackages {

pub struct WatchClient {
run: Run,
watched_packages: HashSet<PackageName>,
persistent_tasks_handle: Option<JoinHandle<Result<i32, run::Error>>>,
connector: DaemonConnector,
base: CommandBase,
Expand Down Expand Up @@ -114,6 +116,8 @@ impl WatchClient {
.build(&handler, telemetry.clone())
.await?;

let watched_packages = run.get_relevant_packages();

let (sender, handle) = run.start_experimental_ui().unzip();

let connector = DaemonConnector {
Expand All @@ -125,6 +129,7 @@ impl WatchClient {
Ok(Self {
base,
run,
watched_packages,
connector,
handler,
telemetry,
Expand Down Expand Up @@ -228,8 +233,8 @@ impl WatchClient {
let packages = packages
.into_iter()
.filter(|pkg| {
// If not in the filtered pkgs, ignore
self.run.filtered_pkgs.contains(pkg)
// If not in the watched packages set, ignore
self.watched_packages.contains(pkg)
})
.collect();

Expand Down Expand Up @@ -297,6 +302,8 @@ impl WatchClient {
.build(&self.handler, self.telemetry.clone())
.await?;

self.watched_packages = self.run.get_relevant_packages();

if let Some(sender) = &self.ui_sender {
let task_names = self.run.engine.tasks_with_command(&self.run.pkg_dep_graph);
sender
Expand Down

0 comments on commit ef69b7e

Please sign in to comment.