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

Hash correct file paths for global dependencies #888

Merged
merged 6 commits into from
Mar 16, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cli/internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,12 @@ func calculateGlobalHash(rootpath string, rootPackageJSON *fs.PackageJSON, exter

if !util.IsYarn(backend.Name) {
// If we are not in Yarn, add the specfile and lockfile to global deps
globalDeps.Add(backend.Specfile)
globalDeps.Add(backend.Lockfile)
globalDeps.Add(filepath.Join(rootpath, backend.Specfile))
globalDeps.Add(filepath.Join(rootpath, backend.Lockfile))
jaredpalmer marked this conversation as resolved.
Show resolved Hide resolved
}

globalFileHashMap, err := fs.GitHashForFiles(globalDeps.UnsafeListOfStrings(), rootpath)
// No prefix, global deps already have full paths
globalFileHashMap, err := fs.GetHashableDeps(globalDeps.UnsafeListOfStrings(), rootpath)
if err != nil {
return "", fmt.Errorf("error hashing files. make sure that git has been initialized %w", err)
}
Expand Down
68 changes: 39 additions & 29 deletions cli/internal/fs/package_deps_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"path/filepath"
"regexp"
"strings"
"github.com/vercel/turborepo/cli/internal/util"

"github.com/pkg/errors"
)

// Predefine []byte variables to avoid runtime allocations.
Expand Down Expand Up @@ -37,6 +38,7 @@ func GetPackageDeps(p *PackageDepsOptions) (map[string]string, error) {
return nil, fmt.Errorf("could not get git hashes for files in package %s: %w", p.PackagePath, err)
}
// Add all the checked in hashes.
// TODO(gsoltis): are these platform-dependent paths?
jaredpalmer marked this conversation as resolved.
Show resolved Hide resolved
result := parseGitLsTree(gitLsOutput)

if len(p.ExcludedPaths) > 0 {
Expand All @@ -53,51 +55,59 @@ func GetPackageDeps(p *PackageDepsOptions) (map[string]string, error) {
}
currentlyChangedFiles := parseGitStatus(gitStatusOutput, p.PackagePath)
var filesToHash []string
excludedPathsSet := new(util.Set)
for filename, changeType := range currentlyChangedFiles {
if changeType == "D" || (len(changeType) == 2 && string(changeType)[1] == []byte("D")[0]) {
delete(result, filename)
} else {
if !excludedPathsSet.Includes(filename) {
filesToHash = append(filesToHash, filename)
}
filesToHash = append(filesToHash, filepath.Join(p.PackagePath, filename))
}
}
normalized := make(map[string]string)
// These paths are platform-dependent, but already relative to the package root
for platformSpecificPath, hash := range result {
platformIndependentPath := filepath.ToSlash(platformSpecificPath)
normalized[platformIndependentPath] = hash
}
hashes, err := GetHashableDeps(filesToHash, p.PackagePath)
if err != nil {
return nil, err
}
for platformIndependentPath, hash := range hashes {
normalized[platformIndependentPath] = hash
}
return normalized, nil
}

// log.Printf("[TRACE] %v:", gitStatusOutput)
// log.Printf("[TRACE] start GitHashForFiles")
current, err := GitHashForFiles(
filesToHash,
p.PackagePath,
)
// GetHashableDeps hashes the list of given files, then returns a map of normalized path to hash
// this map is suitable for cross-platform caching.
func GetHashableDeps(absolutePaths []string, relativeTo string) (map[string]string, error) {
fileHashes, err := gitHashForFiles(absolutePaths)
if err != nil {
return nil, fmt.Errorf("could not retrieve git hash for files in %s", p.PackagePath)
return nil, errors.Wrapf(err, "failed to hash files %v", strings.Join(absolutePaths, ", "))
}
// log.Printf("[TRACE] end GitHashForFiles")
// log.Printf("[TRACE] GitHashForFiles files %v", current)
for filename, hash := range current {
// log.Printf("[TRACE] GitHashForFiles files %v: %v", filename, hash)
result[filename] = hash
result := make(map[string]string)
for filename, hash := range fileHashes {
// Normalize path as POSIX-style and relative to "relativeTo"
relativePath, err := filepath.Rel(relativeTo, filename)
if err != nil {
return nil, errors.Wrapf(err, "failed to get relative path from %v to %v", relativeTo, relativePath)
}
key := filepath.ToSlash(relativePath)
result[key] = hash
}
// log.Printf("[TRACE] GitHashForFiles result %v", result)
return result, nil
}

// GitHashForFiles a list of files returns a map of with their git hash values. It uses
// git hash-object under the
func GitHashForFiles(filesToHash []string, PackagePath string) (map[string]string, error) {
// gitHashForFiles a list of files returns a map of with their git hash values. It uses
// git hash-object under the hood.
// Note that filesToHash must have full paths.
func gitHashForFiles(filesToHash []string) (map[string]string, error) {
changes := make(map[string]string)
if len(filesToHash) > 0 {
var input = []string{"hash-object"}

for _, filename := range filesToHash {
input = append(input, filepath.Join(PackagePath, filename))
}
// fmt.Println(input)
input := []string{"hash-object"}
input = append(input, filesToHash...)
cmd := exec.Command("git", input...)
// https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html
cmd.Stdin = strings.NewReader(strings.Join(input, "\n"))
cmd.Dir = PackagePath
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("git hash-object exited with status: %w", err)
Expand Down
41 changes: 41 additions & 0 deletions cli/internal/fs/package_deps_hash_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fs

import (
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -83,3 +85,42 @@ R turbo.config.js -> turboooz.config.js
?? package_deps_hash_test.go`
assert.EqualValues(t, want, parseGitStatus(input, ""))
}

func Test_GetHashableDeps(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd %v", err)
}
cliDir, err := filepath.Abs(filepath.Join(cwd, "..", ".."))
if err != nil {
t.Fatalf("failed to get cli dir: %v", err)
}
if filepath.Base(cliDir) != "cli" {
t.Fatalf("did not find cli dir, found %v", cliDir)
}
turboPath := filepath.Join(cliDir, "..", "turbo.json")
makefilePath := filepath.Join(cliDir, "Makefile")
mainPath := filepath.Join(cliDir, "cmd", "turbo", "main.go")
hashes, err := GetHashableDeps([]string{turboPath, makefilePath, mainPath}, cliDir)
if err != nil {
t.Fatalf("failed to hash files: %v", err)
}
// Note that the paths here are platform independent, so hardcoded slashes should be fine
expected := []string{
"../turbo.json",
"Makefile",
"cmd/turbo/main.go",
}
for _, key := range expected {
if _, ok := hashes[key]; !ok {
t.Errorf("hashes missing %v", key)
}
}
if len(hashes) != len(expected) {
keys := []string{}
for key := range hashes {
keys = append(keys, key)
}
t.Errorf("hashes mismatch. got %v want %v", strings.Join(keys, ", "), strings.Join(expected, ", "))
}
}