这是indexloc提供的服务,不要输入任何密码
Skip to content

Always include package.json in hash #1832

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

Merged
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
24 changes: 20 additions & 4 deletions cli/internal/hashing/package_deps_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,41 @@ func GetPackageDeps(rootPath turbopath.AbsolutePath, p *PackageDepsOptions) (map
pkgPath := rootPath.Join(p.PackagePath.ToStringDuringMigration())
// Add all the checked in hashes.
var result map[turbopath.AnchoredUnixPath]string
if len(p.InputPatterns) == 0 {

// make a copy of the inputPatterns array, becase we may be appending to it later.
calculatedInputs := make([]string, len(p.InputPatterns))
copy(calculatedInputs, p.InputPatterns)

if len(calculatedInputs) == 0 {
gitLsTreeOutput, err := gitLsTree(pkgPath)
if err != nil {
return nil, fmt.Errorf("could not get git hashes for files in package %s: %w", p.PackagePath, err)
}
result = gitLsTreeOutput
} else {
absoluteFilesToHash, err := globby.GlobFiles(pkgPath.ToStringDuringMigration(), p.InputPatterns, nil)

// Add in package.json to input patterns because if the `scripts` in
// the package.json change (i.e. the tasks that turbo executes), we want
// a cache miss, since any existing cache could be invalid.
// Note this package.json will be resolved relative to the pkgPath.
calculatedInputs = append(calculatedInputs, "package.json")

absoluteFilesToHash, err := globby.GlobFiles(pkgPath.ToStringDuringMigration(), calculatedInputs, nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to resolve input globs %v", p.InputPatterns)
return nil, errors.Wrapf(err, "failed to resolve input globs %v", calculatedInputs)
}

filesToHash := make([]turbopath.AnchoredSystemPath, len(absoluteFilesToHash))
for i, rawPath := range absoluteFilesToHash {
relativePathString, err := pkgPath.RelativePathString(rawPath)

if err != nil {
return nil, errors.Wrapf(err, "not relative to package: %v", rawPath)
}

filesToHash[i] = turbopath.AnchoredSystemPathFromUpstream(relativePathString)
}

hashes, err := gitHashObject(turbopath.AbsoluteSystemPathFromUpstream(pkgPath.ToStringDuringMigration()), filesToHash)
if err != nil {
return nil, errors.Wrap(err, "failed hashing resolved inputs globs")
Expand All @@ -59,7 +75,7 @@ func GetPackageDeps(rootPath turbopath.AbsolutePath, p *PackageDepsOptions) (map

// Update the checked in hashes with the current repo status
// The paths returned from this call are anchored at the package directory
gitStatusOutput, err := gitStatus(pkgPath, p.InputPatterns)
gitStatusOutput, err := gitStatus(pkgPath, calculatedInputs)
if err != nil {
return nil, fmt.Errorf("Could not get git hashes from git status: %v", err)
}
Expand Down
31 changes: 29 additions & 2 deletions cli/internal/hashing/package_deps_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,24 +239,43 @@ func TestGetPackageDeps(t *testing.T) {

repoRoot := fs.AbsolutePathFromUpstream(t.TempDir())
myPkgDir := repoRoot.Join("my-pkg")

// create the dir first
err := myPkgDir.MkdirAll()
assert.NilError(t, err, "CreateDir")

// create file 1
committedFilePath := myPkgDir.Join("committed-file")
err := committedFilePath.EnsureDir()
assert.NilError(t, err, "EnsureDir")
err = committedFilePath.WriteFile([]byte("committed bytes"), 0644)
assert.NilError(t, err, "WriteFile")

// create file 2
deletedFilePath := myPkgDir.Join("deleted-file")
err = deletedFilePath.WriteFile([]byte("delete-me"), 0644)
assert.NilError(t, err, "WriteFile")

// create file 3
nestedPath := myPkgDir.Join("dir", "nested-file")
assert.NilError(t, nestedPath.EnsureDir(), "EnsureDir")
assert.NilError(t, nestedPath.WriteFile([]byte("nested"), 0644), "WriteFile")

// create a package.json
packageJSONPath := myPkgDir.Join("package.json")
err = packageJSONPath.WriteFile([]byte("{}"), 0644)
assert.NilError(t, err, "WriteFile")
Comment on lines +263 to +265
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I miss seeing this last time because of single commit mode or inattentiveness? I'm sorry.


// set up git repo and commit all
requireGitCmd(t, repoRoot, "init", ".")
requireGitCmd(t, repoRoot, "config", "--local", "user.name", "test")
requireGitCmd(t, repoRoot, "config", "--local", "user.email", "test@example.com")
requireGitCmd(t, repoRoot, "add", ".")
requireGitCmd(t, repoRoot, "commit", "-m", "foo")

// remove a file
err = deletedFilePath.Remove()
assert.NilError(t, err, "Remove")

// create another untracked file in git
uncommittedFilePath := myPkgDir.Join("uncommitted-file")
err = uncommittedFilePath.WriteFile([]byte("uncommitted bytes"), 0644)
assert.NilError(t, err, "WriteFile")
Expand All @@ -265,25 +284,30 @@ func TestGetPackageDeps(t *testing.T) {
opts *PackageDepsOptions
expected map[turbopath.AnchoredUnixPath]string
}{
// base case. when inputs aren't specified, all files hashes are computed
{
opts: &PackageDepsOptions{
PackagePath: "my-pkg",
},
expected: map[turbopath.AnchoredUnixPath]string{
"committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033",
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
"package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b",
"dir/nested-file": "bfe53d766e64d78f80050b73cd1c88095bc70abb",
},
},
// with inputs, only the specified inputs are hashed
{
opts: &PackageDepsOptions{
PackagePath: "my-pkg",
InputPatterns: []string{"uncommitted-file"},
},
expected: map[turbopath.AnchoredUnixPath]string{
"package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b",
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
},
},
// inputs with glob pattern also works
{
opts: &PackageDepsOptions{
PackagePath: "my-pkg",
Expand All @@ -292,16 +316,19 @@ func TestGetPackageDeps(t *testing.T) {
expected: map[turbopath.AnchoredUnixPath]string{
"committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033",
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
"package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b",
"dir/nested-file": "bfe53d766e64d78f80050b73cd1c88095bc70abb",
},
},
// inputs with another glob pattern works
{
opts: &PackageDepsOptions{
PackagePath: "my-pkg",
InputPatterns: []string{"**/{uncommitted,committed}-file"},
},
expected: map[turbopath.AnchoredUnixPath]string{
"committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033",
"package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b",
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
},
},
Expand Down