Skip to content

Commit

Permalink
Completed task hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaslyang authored and nicholaslyang committed Aug 18, 2023
1 parent 66ad287 commit 96a828a
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 60 deletions.
41 changes: 18 additions & 23 deletions crates/turborepo-lib/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::Serialize;
pub use traits::TurboHash;
use turborepo_env::ResolvedEnvMode;

use crate::cli::EnvMode;
use crate::{cli::EnvMode, task_graph::TaskOutputs};

mod proto_capnp {
use turborepo_env::ResolvedEnvMode;
Expand Down Expand Up @@ -42,25 +42,25 @@ mod proto_capnp {
}
}

pub struct TaskHashable {
pub struct TaskHashable<'a> {
// hashes
global_hash: String,
task_dependency_hashes: Vec<String>,
hash_of_files: String,
external_deps_hash: String,
pub(crate) global_hash: &'a str,
pub(crate) task_dependency_hashes: Vec<&'a String>,
pub(crate) hash_of_files: &'a str,
pub(crate) external_deps_hash: String,

// task
package_dir: turbopath::RelativeUnixPathBuf,
task: String,
outputs: TaskOutputs,
pass_thru_args: Vec<String>,
pub(crate) package_dir: turbopath::RelativeUnixPathBuf,
pub(crate) task: &'a str,
pub(crate) outputs: TaskOutputs,
pub(crate) pass_through_args: &'a [String],

// env
env: Vec<String>,
resolved_env_vars: EnvVarPairs,
pub(crate) pass_through_env: Vec<String>,
pub(crate) env: &'a [String],
pub(crate) resolved_env_vars: EnvVarPairs,
pub(crate) pass_through_env: &'a [String],
pub(crate) env_mode: ResolvedEnvMode,
dot_env: Vec<turbopath::RelativeUnixPathBuf>,
pub(crate) dot_env: &'a [turbopath::RelativeUnixPathBuf],
}

#[derive(Debug)]
Expand All @@ -76,11 +76,6 @@ pub struct GlobalHashable<'a> {
pub dot_env: &'a [turbopath::RelativeUnixPathBuf],
}

struct TaskOutputs {
inclusions: Vec<String>,
exclusions: Vec<String>,
}

pub struct LockFilePackages(pub Vec<turborepo_lockfiles::Package>);

#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -198,7 +193,7 @@ impl From<FileHashes> for Builder<HeapAllocator> {

type EnvVarPairs = Vec<String>;

impl From<TaskHashable> for Builder<HeapAllocator> {
impl From<TaskHashable<'_>> for Builder<HeapAllocator> {
fn from(task_hashable: TaskHashable) -> Self {
let mut message =
::capnp::message::TypedBuilder::<proto_capnp::task_hashable::Owned>::new_default();
Expand Down Expand Up @@ -230,8 +225,8 @@ impl From<TaskHashable> for Builder<HeapAllocator> {
{
let mut pass_thru_args_builder = builder
.reborrow()
.init_pass_thru_args(task_hashable.pass_thru_args.len() as u32);
for (i, arg) in task_hashable.pass_thru_args.iter().enumerate() {
.init_pass_thru_args(task_hashable.pass_through_args.len() as u32);
for (i, arg) in task_hashable.pass_through_args.iter().enumerate() {
pass_thru_args_builder.set(i as u32, arg);
}
}
Expand Down Expand Up @@ -399,7 +394,7 @@ mod test {
inclusions: vec!["inclusions".to_string()],
exclusions: vec!["exclusions".to_string()],
},
pass_thru_args: vec!["pass_thru_args".to_string()],
pass_through_args: vec!["pass_thru_args".to_string()],
env: vec!["env".to_string()],
resolved_env_vars: vec![],
pass_through_env: vec!["pass_thru_env".to_string()],
Expand Down
9 changes: 1 addition & 8 deletions crates/turborepo-lib/src/package_graph/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,12 @@ impl<'a> BuildState<'a, ResolvedPackageManager> {
) -> Result<(), Error> {
let relative_json_path =
AnchoredSystemPathBuf::relative_path_between(self.repo_root, &package_json_path);
<<<<<<< HEAD
let name = WorkspaceName::Other(
json.name
.clone()
.ok_or(Error::PackageJsonMissingName(package_json_path))?,
);
=======
let name = WorkspaceName::Other(json.name.clone().ok_or_else(|| {
Error::PackageJsonMissingName {
path: package_json_path,
}
})?);
>>>>>>> 8c3242ddf (Fixes)

let entry = WorkspaceInfo {
package_json: json,
package_json_path: relative_json_path,
Expand Down
27 changes: 27 additions & 0 deletions crates/turborepo-lib/src/package_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod builder;

pub use builder::{Error, PackageGraphBuilder};

use crate::hash::{LockFilePackages, TurboHash};

pub struct PackageGraph {
workspace_graph: petgraph::Graph<WorkspaceNode, ()>,
#[allow(dead_code)]
Expand All @@ -36,6 +38,31 @@ impl WorkspaceInfo {
pub fn package_json_path(&self) -> &AnchoredSystemPathBuf {
&self.package_json_path
}

/// The directory of the workspace (computed from package.json path)
pub fn dir(&self) -> &AnchoredSystemPath {
self.package_json_path
.parent()
.unwrap_or_else(|| AnchoredSystemPath::new("").unwrap())
}

pub fn get_external_deps_hash(&self) -> String {
let mut transitive_deps = Vec::with_capacity(
self.transitive_dependencies
.as_ref()
.map_or(0, |deps| deps.len()),
);
for dependency in self.transitive_dependencies.iter().flatten() {
transitive_deps.push(dependency.clone());
}

transitive_deps.sort_by(|a, b| match a.key.cmp(&b.key) {
std::cmp::Ordering::Equal => a.version.cmp(&b.version),
other => other,
});

LockFilePackages(transitive_deps).hash()
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
Expand Down
7 changes: 3 additions & 4 deletions crates/turborepo-lib/src/run/global_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use turborepo_scm::SCM;
use crate::{
cli::EnvMode,
hash::{GlobalHashable, TurboHash},
package_graph::WorkspaceInfo,
package_json::PackageJson,
package_manager::PackageManager,
};
Expand Down Expand Up @@ -41,9 +42,8 @@ pub struct GlobalHashableInputs<'a> {

#[allow(clippy::too_many_arguments)]
pub fn get_global_hash_inputs<'a, L: ?Sized + Lockfile>(
root_external_dependencies: HashSet<&turborepo_lockfiles::Package>,
root_path: &AbsoluteSystemPath,
root_package_json: &PackageJson,
root_workspace: &WorkspaceInfo,
package_manager: &PackageManager,
lockfile: Option<&L>,
global_file_dependencies: &'a [String],
Expand Down Expand Up @@ -107,8 +107,7 @@ pub fn get_global_hash_inputs<'a, L: ?Sized + Lockfile>(
}
}

let root_external_dependencies_hash =
root_package_json.get_external_deps_hash(root_external_dependencies);
let root_external_dependencies_hash = root_workspace.get_external_deps_hash();

debug!(
"rust external deps hash: {}",
Expand Down
20 changes: 10 additions & 10 deletions crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ impl Run {
// }
let env_at_execution_start = EnvironmentVariableMap::infer();

let root_external_dependencies =
pkg_dep_graph.transitive_external_dependencies(std::iter::once(&WorkspaceName::Root));

let team_id = self.base.repo_config()?.team_id();

let token = self.base.user_config()?.token();
Expand Down Expand Up @@ -181,10 +178,13 @@ impl Run {
return Ok(());
}

let root_workspace = pkg_dep_graph
.workspace_info(&WorkspaceName::Root)
.expect("must have root workspace");

let global_hash_inputs = get_global_hash_inputs(
root_external_dependencies,
&self.base.repo_root,
pkg_dep_graph.root_package_json(),
root_workspace,
pkg_dep_graph.package_manager(),
pkg_dep_graph.lockfile(),
&root_turbo_json.global_deps,
Expand Down Expand Up @@ -245,18 +245,18 @@ impl Run {
let root_turbo_json =
TurboJson::load(&self.base.repo_root, &root_package_json, is_single_package)?;

let root_external_dependencies =
pkg_dep_graph.transitive_external_dependencies(std::iter::once(&WorkspaceName::Root));

let scm = SCM::new(&self.base.repo_root);

let filtered_pkgs =
scope::resolve_packages(&opts.scope_opts, &self.base, &pkg_dep_graph, &scm)?;

let root_workspace = pkg_dep_graph
.workspace_info(&WorkspaceName::Root)
.expect("must have root workspace");

let global_hash_inputs = get_global_hash_inputs(
root_external_dependencies,
&self.base.repo_root,
pkg_dep_graph.root_package_json(),
root_workspace,
pkg_dep_graph.package_manager(),
pkg_dep_graph.lockfile(),
&root_turbo_json.global_deps,
Expand Down
4 changes: 2 additions & 2 deletions crates/turborepo-lib/src/run/task_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub const ROOT_PKG_NAME: &str = "//";
/// A task identifier as it will appear in the task graph
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub struct TaskId<'a> {
package: Cow<'a, str>,
task: Cow<'a, str>,
pub(crate) package: Cow<'a, str>,
pub(crate) task: Cow<'a, str>,
}

/// A task name as it appears in in a `turbo.json` it might be for all
Expand Down
4 changes: 2 additions & 2 deletions crates/turborepo-lib/src/task_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ pub struct TaskDefinition {
pub(crate) cache: bool,

// This field is custom-marshalled from `env` and `depends_on``
env: Vec<String>,
pub(crate) env: Vec<String>,

pass_through_env: Vec<String>,
pub(crate) pass_through_env: Vec<String>,

pub(crate) dot_env: Vec<RelativeUnixPathBuf>,

Expand Down
Loading

0 comments on commit 96a828a

Please sign in to comment.