Skip to content
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

feat(query): package changes reason #9240

Merged
merged 16 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion crates/turborepo-lib/src/package_changes_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ impl Subscriber {
}
}
}
Ok(PackageChanges::Some(mut filtered_pkgs)) => {
Ok(PackageChanges::Some(filtered_pkgs)) => {
let mut filtered_pkgs: HashSet<WorkspacePackage> =
filtered_pkgs.into_iter().map(|(pkg, _)| pkg).collect();
// If the root package has changed, we only send it if we have root
// tasks. Otherwise it's not worth sending as it will only
// pollute up the output logs
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use thiserror::Error;
use tokio::select;
use turbo_trace::TraceError;
use turbopath::AbsoluteSystemPathBuf;
use turborepo_repository::package_graph::PackageName;
use turborepo_repository::{change_mapper::AllPackageChangeReason, package_graph::PackageName};

use crate::{
get_version,
Expand Down
21 changes: 15 additions & 6 deletions crates/turborepo-lib/src/run/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use turborepo_cache::AsyncCache;
use turborepo_env::EnvironmentVariableMap;
use turborepo_errors::Spanned;
use turborepo_repository::{
change_mapper::PackageChangeReason,
package_graph::{PackageGraph, PackageName},
package_json,
package_json::PackageJson,
Expand Down Expand Up @@ -42,7 +43,10 @@ use crate::{
engine::{Engine, EngineBuilder},
opts::Opts,
process::ProcessManager,
run::{scope, task_access::TaskAccess, task_id::TaskName, Error, Run, RunCache},
run::{
scope, scope::PackageChange, task_access::TaskAccess, task_id::TaskName, Error, Run,
RunCache,
},
shim::TurboState,
signal::{SignalHandler, SignalSubscriber},
turbo_json::{TurboJson, TurboJsonLoader, UIMode},
Expand Down Expand Up @@ -148,7 +152,7 @@ impl RunBuilder {
pkg_dep_graph: &PackageGraph,
scm: &SCM,
root_turbo_json: &TurboJson,
) -> Result<HashSet<PackageName>, Error> {
) -> Result<HashSet<PackageChange>, Error> {
let (mut filtered_pkgs, is_all_packages) = scope::resolve_packages(
&opts.scope_opts,
repo_root,
Expand All @@ -166,7 +170,12 @@ impl RunBuilder {
}

if root_turbo_json.tasks.contains_key(&task_name) {
filtered_pkgs.insert(PackageName::Root);
filtered_pkgs.insert(PackageChange {
name: PackageName::Root,
reason: PackageChangeReason::RootTask {
task: task_name.to_string(),
},
});
break;
}
}
Expand Down Expand Up @@ -453,7 +462,7 @@ impl RunBuilder {
api_client: self.api_client,
api_auth: self.api_auth,
env_at_execution_start,
filtered_pkgs,
filtered_pkgs: filtered_pkgs.into_iter().map(|p| p.name).collect(),
pkg_dep_graph: Arc::new(pkg_dep_graph),
root_turbo_json,
scm,
Expand All @@ -469,7 +478,7 @@ impl RunBuilder {
&self,
pkg_dep_graph: &PackageGraph,
root_turbo_json: &TurboJson,
filtered_pkgs: &HashSet<PackageName>,
filtered_pkgs: &HashSet<PackageChange>,
turbo_json_loader: TurboJsonLoader,
) -> Result<Engine, Error> {
let mut builder = EngineBuilder::new(
Expand All @@ -480,7 +489,7 @@ impl RunBuilder {
)
.with_root_tasks(root_turbo_json.tasks.keys().cloned())
.with_tasks_only(self.opts.run_opts.only)
.with_workspaces(filtered_pkgs.clone().into_iter().collect())
.with_workspaces(filtered_pkgs.clone().into_iter().map(|p| p.name).collect())
.with_tasks(self.opts.run_opts.tasks.iter().map(|task| {
// TODO: Pull span info from command
Spanned::new(TaskName::from(task.as_str()).into_owned())
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod error;
pub(crate) mod global_hash;
mod graph_visualizer;
pub(crate) mod package_discovery;
mod scope;
pub(crate) mod scope;
pub(crate) mod summary;
pub mod task_access;
pub mod task_id;
Expand Down
39 changes: 27 additions & 12 deletions crates/turborepo-lib/src/run/scope/change_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ use std::collections::HashSet;
use tracing::debug;
use turbopath::{AbsoluteSystemPath, AnchoredSystemPathBuf};
use turborepo_repository::{
change_mapper::{ChangeMapper, DefaultPackageChangeMapper, LockfileChange, PackageChanges},
package_graph::{PackageGraph, PackageName},
change_mapper::{
AllPackageChangeReason, ChangeMapper, DefaultPackageChangeMapper, LockfileChange,
PackageChangeReason, PackageChanges,
},
package_graph::PackageGraph,
};
use turborepo_scm::{git::ChangedFiles, SCM};
use turborepo_scm::{git::ChangedFilesResult, SCM};

use crate::{
global_deps_package_change_mapper::{Error, GlobalDepsPackageChangeMapper},
run::scope::ResolutionError,
run::scope::{PackageChange, ResolutionError},
};

/// Given two git refs, determine which packages have changed between them.
Expand All @@ -22,7 +25,7 @@ pub trait GitChangeDetector {
include_uncommitted: bool,
allow_unknown_objects: bool,
merge_base: bool,
) -> Result<HashSet<PackageName>, ResolutionError>;
) -> Result<HashSet<PackageChange>, ResolutionError>;
}

pub struct ScopeChangeDetector<'a> {
Expand Down Expand Up @@ -94,7 +97,7 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {
include_uncommitted: bool,
allow_unknown_objects: bool,
merge_base: bool,
) -> Result<HashSet<PackageName>, ResolutionError> {
) -> Result<HashSet<PackageChange>, ResolutionError> {
let changed_files = match self.scm.changed_files(
self.turbo_root,
from_ref,
Expand All @@ -103,15 +106,21 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {
allow_unknown_objects,
merge_base,
)? {
ChangedFiles::All => {
ChangedFilesResult::Err { from_ref, to_ref } => {
debug!("all packages changed");
return Ok(self
.pkg_graph
.packages()
.map(|(name, _)| name.to_owned())
.map(|(name, _)| PackageChange {
name: name.to_owned(),
reason: PackageChangeReason::All(AllPackageChangeReason::GitRefNotFound {
from_ref: from_ref.clone(),
to_ref: to_ref.clone(),
}),
})
.collect());
}
ChangedFiles::Some(changed_files) => changed_files,
ChangedFilesResult::Ok(changed_files) => changed_files,
};

let lockfile_contents = self.get_lockfile_contents(from_ref, &changed_files);
Expand All @@ -133,7 +142,10 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {
Ok(self
.pkg_graph
.packages()
.map(|(name, _)| name.to_owned())
.map(|(name, _)| PackageChange {
name: name.to_owned(),
reason: PackageChangeReason::All(reason.clone()),
})
.collect())
}
PackageChanges::Some(packages) => {
Expand All @@ -142,13 +154,16 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {
packages.len(),
&packages
.iter()
.map(|x| x.name.to_string())
.map(|(x, _)| x.name.to_string())
.collect::<Vec<String>>()
);

Ok(packages
.iter()
.map(|package| package.name.to_owned())
.map(|(package, reason)| PackageChange {
name: package.name.to_owned(),
reason: reason.clone(),
})
.collect())
}
}
Expand Down
Loading