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(turborepo): Port Global Hash Env Var #5166

Merged
merged 4 commits into from Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -116,6 +116,7 @@ turbopath = { path = "crates/turborepo-paths" }
turborepo = { path = "crates/turborepo" }
turborepo-api-client = { path = "crates/turborepo-api-client" }
turborepo-cache = { path = "crates/turborepo-cache" }
turborepo-env = { path = "crates/turborepo-env" }
turborepo-ffi = { path = "crates/turborepo-ffi" }
turborepo-fs = { path = "crates/turborepo-fs" }
turborepo-lib = { path = "crates/turborepo-lib" }
Expand Down Expand Up @@ -193,8 +194,10 @@ serde_json = "1.0.93"
serde_qs = "0.11.0"
serde_with = "2.3.2"
serde_yaml = "0.9.17"
sha2 = "0.10.6"
syn = "1.0.107"
tempfile = "3.3.0"
test-case = "3.0.0"
thiserror = "1.0.38"
tiny-gradient = "0.1.0"
tokio = "1.25.0"
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/env/env.go
Expand Up @@ -20,7 +20,7 @@ type BySource struct {

// DetailedMap contains the composite and the detailed maps of environment variables
// All is used as a taskhash input (taskhash.CalculateTaskHash)
// BySoure is used to print out a Dry Run Summary
// BySource is used to print out a Dry Run Summary
type DetailedMap struct {
All EnvironmentVariableMap
BySource BySource
Expand Down
6 changes: 6 additions & 0 deletions cli/internal/env/env_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"testing"

"github.com/vercel/turbo/cli/internal/ffi"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -200,7 +201,12 @@ func TestGetEnvVarsFromWildcards(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.self.FromWildcards(tt.wildcardPatterns)
var rustResult EnvironmentVariableMap
rustResult, rustErr := ffi.FromWildcards(tt.self, tt.wildcardPatterns)
assert.NilError(t, rustErr, "Rust implementation failed.")
assert.NilError(t, err, "Did not fail regexp compile.")

assert.DeepEqual(t, got, rustResult)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetEnvVarsFromWildcards() = %v, want %v", got, tt.want)
}
Expand Down
4 changes: 4 additions & 0 deletions cli/internal/ffi/bindings.h
Expand Up @@ -26,6 +26,10 @@ struct Buffer get_package_file_hashes_from_processing_git_ignore(struct Buffer b

struct Buffer glob(struct Buffer buffer);

struct Buffer from_wildcards(struct Buffer buffer);

struct Buffer get_global_hashable_env_vars(struct Buffer buffer);

struct Buffer transitive_closure(struct Buffer buf);

struct Buffer subgraph(struct Buffer buf);
Expand Down
61 changes: 61 additions & 0 deletions cli/internal/ffi/ffi.go
Expand Up @@ -384,6 +384,67 @@ func GetPackageFileHashesFromProcessingGitIgnore(rootPath string, packagePath st
if err := resp.GetError(); err != "" {
return nil, errors.New(err)
}

hashes := resp.GetHashes()
return hashes.GetHashes(), nil
}

// FromWildcards returns an EnvironmentVariableMap containing the variables
// in the environment which match an array of wildcard patterns.
func FromWildcards(environmentMap map[string]string, wildcardPatterns []string) (map[string]string, error) {
if wildcardPatterns == nil {
NicholasLYang marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}
req := ffi_proto.FromWildcardsRequest{
EnvVars: &ffi_proto.EnvVarMap{
Map: environmentMap,
},
WildcardPatterns: wildcardPatterns,
}
reqBuf := Marshal(&req)
resBuf := C.from_wildcards(reqBuf)
reqBuf.Free()

resp := ffi_proto.FromWildcardsResponse{}
if err := Unmarshal(resBuf, resp.ProtoReflect().Interface()); err != nil {
panic(err)
}

if err := resp.GetError(); err != "" {
return nil, errors.New(err)
}
envVarMap := resp.GetEnvVars().GetMap()
// If the map is nil, return an empty map instead of nil
// to match with existing Go code.
if envVarMap == nil {
return map[string]string{}, nil
}
return envVarMap, nil
}

// GetGlobalHashableEnvVars calculates env var dependencies
func GetGlobalHashableEnvVars(envAtExecutionStart map[string]string, globalEnv []string) (*ffi_proto.DetailedMap, error) {
req := ffi_proto.GetGlobalHashableEnvVarsRequest{
EnvAtExecutionStart: &ffi_proto.EnvVarMap{Map: envAtExecutionStart},
GlobalEnv: globalEnv,
}
reqBuf := Marshal(&req)
resBuf := C.get_global_hashable_env_vars(reqBuf)
reqBuf.Free()

resp := ffi_proto.GetGlobalHashableEnvVarsResponse{}
if err := Unmarshal(resBuf, resp.ProtoReflect().Interface()); err != nil {
panic(err)
}

if err := resp.GetError(); err != "" {
return nil, errors.New(err)
}

respDetailedMap := resp.GetDetailedMap()
if respDetailedMap == nil {
return nil, nil
}

return respDetailedMap, nil
}