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
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use thiserror::Error;
use turbopath::AnchoredSystemPath;
use turborepo_repository::{
change_mapper::{DefaultPackageChangeMapper, PackageChangeMapper, PackageMapping},
change_mapper::{
AllPackageChangeReason, DefaultPackageChangeMapper, PackageChangeMapper, PackageMapping,
},
package_graph::{PackageGraph, WorkspacePackage},
};
use wax::{BuildError, Program};
Expand Down
6 changes: 3 additions & 3 deletions crates/turborepo-lib/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,12 @@ impl RepositoryQuery {
self.run.root_turbo_json(),
)?
.into_iter()
.map(|package| ChangedPackage {
.map(|(package, reason)| ChangedPackage {
package: Package {
run: self.run.clone(),
name: package.name,
name: package,
},
reason: package.reason.into(),
reason: reason.into(),
})
.filter(|package| filter.as_ref().map_or(true, |f| f.check(&package.package)))
.sorted_by(|a, b| a.package.name.cmp(&b.package.name))
Expand Down
29 changes: 13 additions & 16 deletions crates/turborepo-lib/src/run/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::HashSet,
collections::{HashMap, HashSet},
io::{ErrorKind, IsTerminal},
sync::Arc,
time::SystemTime,
Expand Down Expand Up @@ -43,10 +43,7 @@ use crate::{
engine::{Engine, EngineBuilder},
opts::Opts,
process::ProcessManager,
run::{
scope, scope::PackageChange, task_access::TaskAccess, task_id::TaskName, Error, Run,
RunCache,
},
run::{scope, task_access::TaskAccess, task_id::TaskName, Error, Run, RunCache},
shim::TurboState,
signal::{SignalHandler, SignalSubscriber},
turbo_json::{TurboJson, TurboJsonLoader, UIMode},
Expand Down Expand Up @@ -152,7 +149,7 @@ impl RunBuilder {
pkg_dep_graph: &PackageGraph,
scm: &SCM,
root_turbo_json: &TurboJson,
) -> Result<HashSet<PackageChange>, Error> {
) -> Result<HashMap<PackageName, PackageChangeReason>, Error> {
let (mut filtered_pkgs, is_all_packages) = scope::resolve_packages(
&opts.scope_opts,
repo_root,
Expand All @@ -170,12 +167,12 @@ impl RunBuilder {
}

if root_turbo_json.tasks.contains_key(&task_name) {
filtered_pkgs.insert(PackageChange {
name: PackageName::Root,
reason: PackageChangeReason::RootTask {
filtered_pkgs.insert(
PackageName::Root,
PackageChangeReason::RootTask {
task: task_name.to_string(),
},
});
);
break;
}
}
Expand Down Expand Up @@ -420,7 +417,7 @@ impl RunBuilder {
let mut engine = self.build_engine(
&pkg_dep_graph,
&root_turbo_json,
&filtered_pkgs,
filtered_pkgs.keys(),
turbo_json_loader.clone(),
)?;

Expand All @@ -429,7 +426,7 @@ impl RunBuilder {
engine = self.build_engine(
&pkg_dep_graph,
&root_turbo_json,
&filtered_pkgs,
filtered_pkgs.keys(),
turbo_json_loader,
)?;
}
Expand Down Expand Up @@ -462,7 +459,7 @@ impl RunBuilder {
api_client: self.api_client,
api_auth: self.api_auth,
env_at_execution_start,
filtered_pkgs: filtered_pkgs.into_iter().map(|p| p.name).collect(),
filtered_pkgs: filtered_pkgs.keys().cloned().collect(),
pkg_dep_graph: Arc::new(pkg_dep_graph),
root_turbo_json,
scm,
Expand All @@ -474,11 +471,11 @@ impl RunBuilder {
})
}

fn build_engine(
fn build_engine<'a>(
&self,
pkg_dep_graph: &PackageGraph,
root_turbo_json: &TurboJson,
filtered_pkgs: &HashSet<PackageChange>,
filtered_pkgs: impl Iterator<Item = &'a PackageName>,
turbo_json_loader: TurboJsonLoader,
) -> Result<Engine, Error> {
let mut builder = EngineBuilder::new(
Expand All @@ -489,7 +486,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().map(|p| p.name).collect())
.with_workspaces(filtered_pkgs.cloned().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
34 changes: 15 additions & 19 deletions crates/turborepo-lib/src/run/scope/change_detector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};

use tracing::debug;
use turbopath::{AbsoluteSystemPath, AnchoredSystemPathBuf};
Expand All @@ -7,13 +7,13 @@ use turborepo_repository::{
AllPackageChangeReason, ChangeMapper, DefaultPackageChangeMapper, LockfileChange,
PackageChangeReason, PackageChanges,
},
package_graph::PackageGraph,
package_graph::{PackageGraph, PackageName},
};
use turborepo_scm::{git::ChangedFilesResult, SCM};

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

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

pub struct ScopeChangeDetector<'a> {
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {
include_uncommitted: bool,
allow_unknown_objects: bool,
merge_base: bool,
) -> Result<HashSet<PackageChange>, ResolutionError> {
) -> Result<HashMap<PackageName, PackageChangeReason>, ResolutionError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be returning HashMap<PackageName, HashSet<PackageChangeReason>> so we correctly communicate to users if there are multiple reasons why a package is in scope?

let changed_files = match self.scm.changed_files(
self.turbo_root,
from_ref,
Expand All @@ -111,12 +111,14 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {
return Ok(self
.pkg_graph
.packages()
.map(|(name, _)| PackageChange {
name: name.to_owned(),
reason: PackageChangeReason::All(AllPackageChangeReason::GitRefNotFound {
from_ref: from_ref.clone(),
to_ref: to_ref.clone(),
}),
.map(|(name, _)| {
(
name.to_owned(),
PackageChangeReason::All(AllPackageChangeReason::GitRefNotFound {
from_ref: from_ref.clone(),
to_ref: to_ref.clone(),
}),
)
})
.collect());
}
Expand All @@ -142,10 +144,7 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {
Ok(self
.pkg_graph
.packages()
.map(|(name, _)| PackageChange {
name: name.to_owned(),
reason: PackageChangeReason::All(reason.clone()),
})
.map(|(name, _)| (name.to_owned(), PackageChangeReason::All(reason.clone())))
.collect())
}
PackageChanges::Some(packages) => {
Expand All @@ -160,10 +159,7 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {

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