Skip to content

Commit

Permalink
Clean up and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaslyang authored and nicholaslyang committed Aug 18, 2023
1 parent 67e9971 commit cd32737
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 52 deletions.
1 change: 0 additions & 1 deletion cli/internal/fs/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func HashTask(task *hash.TaskHashable) (string, error) {

// HashGlobal produces the global hash value to be incorporated in every task hash
func HashGlobal(global hash.GlobalHashable) (string, error) {
fmt.Printf("HashGlobal: %+v\n", global)
return hash.HashGlobalHashable(&global)
}

Expand Down
7 changes: 0 additions & 7 deletions cli/internal/fs/package_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fs
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"sync"

Expand Down Expand Up @@ -132,13 +131,7 @@ func (p *PackageJSON) SetExternalDeps(externalDeps mapset.Set) error {
dependency := dependency.(lockfile.Package)
p.TransitiveDeps = append(p.TransitiveDeps, dependency)
}

sort.Sort(lockfile.ByKey(p.TransitiveDeps))

if p.Name == "my-turborepo" {
fmt.Println(len(p.TransitiveDeps))
}

hashOfExternalDeps, err := HashLockfilePackages(p.TransitiveDeps)
if err != nil {
return err
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
21 changes: 21 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,25 @@ impl WorkspaceInfo {
pub fn package_json_path(&self) -> &AnchoredSystemPathBuf {
&self.package_json_path
}

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
21 changes: 1 addition & 20 deletions crates/turborepo-lib/src/package_json.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::collections::{BTreeMap, HashSet};
use std::collections::BTreeMap;

use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use turbopath::{AbsoluteSystemPath, RelativeUnixPathBuf};

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

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct PackageJson {
Expand Down Expand Up @@ -76,23 +74,6 @@ impl PackageJson {
.chain(self.dev_dependencies.iter().flatten())
.chain(self.optional_dependencies.iter().flatten())
}

pub fn get_external_deps_hash(
&self,
external_deps: HashSet<&turborepo_lockfiles::Package>,
) -> String {
let mut transitive_deps = Vec::with_capacity(external_deps.len());
for dependency in external_deps {
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()
}
}

#[cfg(test)]
Expand Down
10 changes: 3 additions & 7 deletions crates/turborepo-lib/src/run/global_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use turborepo_scm::SCM;
use crate::{
cli::EnvMode,
hash::{GlobalHashable, TurboHash},
package_json::PackageJson,
package_graph::WorkspaceInfo,
package_manager::PackageManager,
};

Expand Down Expand Up @@ -41,9 +41,8 @@ pub struct GlobalHashableInputs {

#[allow(clippy::too_many_arguments)]
pub fn get_global_hash_inputs<L: ?Sized + Lockfile>(
root_external_dependencies: HashSet<&turborepo_lockfiles::Package>,
root_workspace: &WorkspaceInfo,
root_path: &AbsoluteSystemPath,
root_package_json: &PackageJson,
package_manager: &PackageManager,
lockfile: Option<&L>,
global_file_dependencies: Vec<String>,
Expand Down Expand Up @@ -107,8 +106,7 @@ pub fn get_global_hash_inputs<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 Expand Up @@ -169,8 +167,6 @@ impl GlobalHashableInputs {
dot_env: self.dot_env,
};

println!("global hashable: {:#?}", global_hashable);

global_hashable.hash()
}
}
18 changes: 9 additions & 9 deletions crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,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 @@ -180,10 +177,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,
root_workspace,
&self.base.repo_root,
pkg_dep_graph.root_package_json(),
pkg_dep_graph.package_manager(),
pkg_dep_graph.lockfile(),
// TODO: Fill in these vec![] once turbo.json is ported
Expand Down Expand Up @@ -232,13 +232,13 @@ 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 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,
root_workspace,
&self.base.repo_root,
pkg_dep_graph.root_package_json(),
pkg_dep_graph.package_manager(),
pkg_dep_graph.lockfile(),
root_turbo_json.global_deps,
Expand Down

0 comments on commit cd32737

Please sign in to comment.