Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaslyang authored and nicholaslyang committed Jul 25, 2023
1 parent ce633a3 commit e08b1f4
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 47 deletions.
22 changes: 0 additions & 22 deletions cli/internal/fs/hash/capnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
package hash

import (
"encoding/binary"
"encoding/hex"
"fmt"
"sort"

capnp "capnproto.org/go/capnp/v3"
Expand Down Expand Up @@ -368,24 +366,6 @@ func HashFileHashes(fileHashes map[turbopath.AnchoredUnixPath]string) (string, e
return HashMessage(globalMsg.Message())
}

// HashMessage hashes a capnp message using xxhash
func HashMessage2(msg *capnp.Message) (string, error) {
bytes, err := msg.Marshal()
if err != nil {
return "", err
}

fmt.Printf("\n\ngo buffer: %+v\n\n\n", bytes)
digest := xxhash.New()
digest.Write(bytes)
out := digest.Sum(nil)

fmt.Printf("go external deps hash big: %v\n", binary.BigEndian.Uint64(out))
fmt.Printf("go external deps hash little: %v\n", binary.LittleEndian.Uint64(out))

return hex.EncodeToString(out), nil
}

func HashMessage(msg *capnp.Message) (string, error) {
root, err := msg.Root()
if err != nil {
Expand All @@ -397,8 +377,6 @@ func HashMessage(msg *capnp.Message) (string, error) {
return "", err
}

println(hex.EncodeToString(bytes))

digest := xxhash.New()
_, err = digest.Write(bytes)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions cli/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,14 @@ func (r *run) run(ctx gocontext.Context, targets []string, executionState *turbo

if globalHash, err := calculateGlobalHashFromHashableInputs(globalHashInputs); err == nil {
r.base.Logger.Debug("global hash", "value", globalHash)
if executionState.GlobalHash != globalHash {
fmt.Println("global hash differs between Rust and Go: ", "rust", executionState.GlobalHash, "go", globalHash)
} else {
fmt.Println("global hash matches between Rust and Go")
if executionState.GlobalHash != nil {
if *executionState.GlobalHash != globalHash {
return fmt.Errorf("global hash differs between Rust and Go: rust %v go %v\n", executionState.GlobalHash, globalHash)
} else {
r.base.Logger.Debug("global hash matches between Rust and Go")
}
}

g.GlobalHash = globalHash
} else {
return fmt.Errorf("failed to calculate global hash: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/turbostate/turbostate.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type ParsedArgsFromRust struct {

// ExecutionState is the entire state of a turbo execution that is passed from the Rust shim.
type ExecutionState struct {
GlobalHash string `json:"global_hash"`
GlobalHash *string `json:"global_hash"`
APIClientConfig APIClientConfig `json:"api_client_config"`
PackageManager string `json:"package_manager"`
CLIArgs ParsedArgsFromRust `json:"cli_args"`
Expand Down
16 changes: 13 additions & 3 deletions crates/turborepo-lib/src/execution_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{

#[derive(Debug, Serialize)]
pub struct ExecutionState<'a> {
global_hash: String,
global_hash: Option<String>,
pub api_client_config: APIClientConfig<'a>,
package_manager: PackageManager,
pub cli_args: &'a Args,
Expand All @@ -31,7 +31,17 @@ impl<'a> TryFrom<&'a CommandBase> for ExecutionState<'a> {

fn try_from(base: &'a CommandBase) -> Result<Self, Self::Error> {
let run = Run::new((*base).clone());
let global_hash = run.get_global_hash()?;

let global_hash;
#[cfg(debug_assertions)]
{
global_hash = Some(run.get_global_hash()?);
}
#[cfg(not(debug_assertions))]
{
global_hash = None;
}

let root_package_json =
PackageJson::load(&base.repo_root.join_component("package.json")).ok();

Expand All @@ -54,7 +64,7 @@ impl<'a> TryFrom<&'a CommandBase> for ExecutionState<'a> {
};

Ok(ExecutionState {
global_hash: hex::encode(global_hash.to_be_bytes()),
global_hash,
api_client_config,
package_manager,
cli_args: base.args(),
Expand Down
4 changes: 2 additions & 2 deletions crates/turborepo-lib/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct TaskHashable {
pub struct GlobalHashable {
pub global_cache_key: String,
pub global_file_hash_map: HashMap<turbopath::RelativeUnixPathBuf, String>,
pub root_external_dependencies_hash: u64,
pub root_external_dependencies_hash: String,
pub env: Vec<String>,
pub resolved_env_vars: Vec<String>,
pub pass_through_env: Vec<String>,
Expand Down Expand Up @@ -285,7 +285,7 @@ impl From<GlobalHashable> for Builder<HeapAllocator> {
}
}

builder.set_root_external_deps_hash(hashable.root_external_dependencies_hash);
builder.set_root_external_deps_hash(&hashable.root_external_dependencies_hash);

{
let mut entries = builder.reborrow().init_env(hashable.env.len() as u32);
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/hash/proto.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct TaskOutputs {
struct GlobalHashable {
globalCacheKey @0 :Text;
globalFileHashMap @1 :List(Entry);
rootExternalDepsHash @2 :UInt64;
rootExternalDepsHash @2 :Text;
env @3 :List(Text);
resolvedEnvVars @4 :List(Text);
passThroughEnv @5 :List(Text);
Expand Down
4 changes: 0 additions & 4 deletions crates/turborepo-lib/src/hash/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ where

let buf = message.get_segments_for_output()[0];

if should_print {
println!("\n\nrust buffer {:?}\n\n", buf);
}

let mut hasher = twox_hash::XxHash64::with_seed(0);
hasher.write(buf);
let out = hasher.finish();
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl PackageJson {
pub fn get_external_deps_hash(
&self,
external_deps: HashSet<&turborepo_lockfiles::Package>,
) -> u64 {
) -> String {
let mut transitive_deps = Vec::with_capacity(1);
for dependency in external_deps {
transitive_deps.push(dependency.clone());
Expand Down
13 changes: 5 additions & 8 deletions crates/turborepo-lib/src/run/global_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum GlobalHashError {}
pub struct GlobalHashableInputs {
global_cache_key: &'static str,
global_file_hash_map: HashMap<RelativeUnixPathBuf, String>,
pub root_external_dependencies_hash: u64,
pub root_external_dependencies_hash: String,
env: Vec<String>,
// Only Option to allow #[derive(Default)]
resolved_env_vars: Option<DetailedMap>,
Expand Down Expand Up @@ -112,16 +112,13 @@ pub fn get_global_hash_inputs<L: ?Sized + Lockfile>(

debug!(
"rust external deps hash: {}",
hex::encode(root_external_dependencies_hash.to_le_bytes())
root_external_dependencies_hash
);

let root_external_dependencies_hash =
hex::encode(root_external_dependencies_hash.to_be_bytes());

Ok(GlobalHashableInputs {
global_cache_key: GLOBAL_CACHE_KEY,
global_file_hash_map,
root_external_dependencies_hash: 0,
root_external_dependencies_hash,
env: global_env,
resolved_env_vars: Some(global_hashable_env_vars),
pass_through_env: Some(global_pass_through_env),
Expand All @@ -132,7 +129,7 @@ pub fn get_global_hash_inputs<L: ?Sized + Lockfile>(
}

impl GlobalHashableInputs {
pub fn calculate_global_hash_from_inputs(mut self) -> u64 {
pub fn calculate_global_hash_from_inputs(mut self) -> String {
match self.env_mode {
EnvMode::Infer
if self
Expand All @@ -156,7 +153,7 @@ impl GlobalHashableInputs {
self.calculate_global_hash()
}

fn calculate_global_hash(self) -> u64 {
fn calculate_global_hash(self) -> String {
let global_hashable = GlobalHashable {
global_cache_key: self.global_cache_key.to_string(),
global_file_hash_map: self.global_file_hash_map,
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 @@ -126,7 +126,7 @@ impl Run {
Ok(())
}

pub fn get_global_hash(&self) -> Result<u64> {
pub fn get_global_hash(&self) -> Result<String> {
let env_at_execution_start = EnvironmentVariableMap::infer();

let package_json_path = self.base.repo_root.join_component("package.json");
Expand Down

0 comments on commit e08b1f4

Please sign in to comment.