Skip to content

Commit 7d05ba4

Browse files
committed
Include package.json when user specifies inputs
We want to include each workspace's package.json as an input when the user has configured a smaller set of inputs, because the package.json defines the actual task turbo will execute for that workspace (via scripts). If that task definition changes, we want to consider the cache invalid. We only apply this inclusion when inputs are defined, because if inputs are _not_ defined, _all_ git-tracked files are included in the hash, 'which will already include the package.json file.
1 parent b1ae207 commit 7d05ba4

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

cli/internal/hashing/package_deps_hash.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,29 @@ func GetPackageDeps(rootPath turbopath.AbsolutePath, p *PackageDepsOptions) (map
3838
}
3939
result = gitLsTreeOutput
4040
} else {
41+
42+
// Add in package.json to input patterns because if the `scripts` in
43+
// the package.json change (i.e. the tasks that turbo executes), we want
44+
// a cache miss, since any existing cache could be invalid.
45+
// Note this package.json will be resolved relative to the pkgPath.
46+
p.InputPatterns = append(p.InputPatterns, "package.json")
47+
4148
absoluteFilesToHash, err := globby.GlobFiles(pkgPath.ToStringDuringMigration(), p.InputPatterns, nil)
4249
if err != nil {
4350
return nil, errors.Wrapf(err, "failed to resolve input globs %v", p.InputPatterns)
4451
}
52+
4553
filesToHash := make([]turbopath.AnchoredSystemPath, len(absoluteFilesToHash))
4654
for i, rawPath := range absoluteFilesToHash {
4755
relativePathString, err := pkgPath.RelativePathString(rawPath)
56+
4857
if err != nil {
4958
return nil, errors.Wrapf(err, "not relative to package: %v", rawPath)
5059
}
60+
5161
filesToHash[i] = turbopath.AnchoredSystemPathFromUpstream(relativePathString)
5262
}
63+
5364
hashes, err := gitHashObject(turbopath.AbsoluteSystemPathFromUpstream(pkgPath.ToStringDuringMigration()), filesToHash)
5465
if err != nil {
5566
return nil, errors.Wrap(err, "failed hashing resolved inputs globs")

cli/internal/hashing/package_deps_hash_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,24 +239,43 @@ func TestGetPackageDeps(t *testing.T) {
239239

240240
repoRoot := fs.AbsolutePathFromUpstream(t.TempDir())
241241
myPkgDir := repoRoot.Join("my-pkg")
242+
243+
// create the dir first
244+
err := myPkgDir.MkdirAll()
245+
assert.NilError(t, err, "CreateDir")
246+
247+
// create file 1
242248
committedFilePath := myPkgDir.Join("committed-file")
243-
err := committedFilePath.EnsureDir()
244-
assert.NilError(t, err, "EnsureDir")
245249
err = committedFilePath.WriteFile([]byte("committed bytes"), 0644)
246250
assert.NilError(t, err, "WriteFile")
251+
252+
// create file 2
247253
deletedFilePath := myPkgDir.Join("deleted-file")
248254
err = deletedFilePath.WriteFile([]byte("delete-me"), 0644)
249255
assert.NilError(t, err, "WriteFile")
256+
257+
// create file 3
250258
nestedPath := myPkgDir.Join("dir", "nested-file")
251259
assert.NilError(t, nestedPath.EnsureDir(), "EnsureDir")
252260
assert.NilError(t, nestedPath.WriteFile([]byte("nested"), 0644), "WriteFile")
261+
262+
// create a package.json
263+
packageJSONPath := myPkgDir.Join("package.json")
264+
err = packageJSONPath.WriteFile([]byte("{}"), 0644)
265+
assert.NilError(t, err, "WriteFile")
266+
267+
// set up git repo and commit all
253268
requireGitCmd(t, repoRoot, "init", ".")
254269
requireGitCmd(t, repoRoot, "config", "--local", "user.name", "test")
255270
requireGitCmd(t, repoRoot, "config", "--local", "user.email", "[email protected]")
256271
requireGitCmd(t, repoRoot, "add", ".")
257272
requireGitCmd(t, repoRoot, "commit", "-m", "foo")
273+
274+
// remove a file
258275
err = deletedFilePath.Remove()
259276
assert.NilError(t, err, "Remove")
277+
278+
// create another untracked file in git
260279
uncommittedFilePath := myPkgDir.Join("uncommitted-file")
261280
err = uncommittedFilePath.WriteFile([]byte("uncommitted bytes"), 0644)
262281
assert.NilError(t, err, "WriteFile")
@@ -265,25 +284,30 @@ func TestGetPackageDeps(t *testing.T) {
265284
opts *PackageDepsOptions
266285
expected map[turbopath.AnchoredUnixPath]string
267286
}{
287+
// base case. when inputs aren't specified, all files hashes are computed
268288
{
269289
opts: &PackageDepsOptions{
270290
PackagePath: "my-pkg",
271291
},
272292
expected: map[turbopath.AnchoredUnixPath]string{
273293
"committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033",
274294
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
295+
"package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b",
275296
"dir/nested-file": "bfe53d766e64d78f80050b73cd1c88095bc70abb",
276297
},
277298
},
299+
// with inputs, only the specified inputs are hashed
278300
{
279301
opts: &PackageDepsOptions{
280302
PackagePath: "my-pkg",
281303
InputPatterns: []string{"uncommitted-file"},
282304
},
283305
expected: map[turbopath.AnchoredUnixPath]string{
306+
"package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b",
284307
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
285308
},
286309
},
310+
// inputs with glob pattern also works
287311
{
288312
opts: &PackageDepsOptions{
289313
PackagePath: "my-pkg",
@@ -292,16 +316,19 @@ func TestGetPackageDeps(t *testing.T) {
292316
expected: map[turbopath.AnchoredUnixPath]string{
293317
"committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033",
294318
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
319+
"package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b",
295320
"dir/nested-file": "bfe53d766e64d78f80050b73cd1c88095bc70abb",
296321
},
297322
},
323+
// inputs with another glob pattern works
298324
{
299325
opts: &PackageDepsOptions{
300326
PackagePath: "my-pkg",
301327
InputPatterns: []string{"**/{uncommitted,committed}-file"},
302328
},
303329
expected: map[turbopath.AnchoredUnixPath]string{
304330
"committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033",
331+
"package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b",
305332
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
306333
},
307334
},

0 commit comments

Comments
 (0)