From b915bc40070fbe1cd434a78f81824c0a3b8ec34a Mon Sep 17 00:00:00 2001 From: Nathan Hammond Date: Tue, 13 Sep 2022 14:26:38 +0800 Subject: [PATCH] Enable inputs to be relative again. --- cli/internal/hashing/package_deps_hash.go | 17 ++++++++-- .../hashing/package_deps_hash_test.go | 33 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/cli/internal/hashing/package_deps_hash.go b/cli/internal/hashing/package_deps_hash.go index 21ad409cad80f..f7d9d635a89b6 100644 --- a/cli/internal/hashing/package_deps_hash.go +++ b/cli/internal/hashing/package_deps_hash.go @@ -32,7 +32,7 @@ func GetPackageDeps(rootPath turbopath.AbsolutePath, p *PackageDepsOptions) (map // Add all the checked in hashes. var result map[turbopath.AnchoredUnixPath]string - // make a copy of the inputPatterns array, becase we may be appending to it later. + // make a copy of the inputPatterns array, because we may be appending to it later. calculatedInputs := make([]string, len(p.InputPatterns)) copy(calculatedInputs, p.InputPatterns) @@ -50,7 +50,20 @@ func GetPackageDeps(rootPath turbopath.AbsolutePath, p *PackageDepsOptions) (map // Note this package.json will be resolved relative to the pkgPath. calculatedInputs = append(calculatedInputs, "package.json") - absoluteFilesToHash, err := globby.GlobFiles(pkgPath.ToStringDuringMigration(), calculatedInputs, nil) + // The input patterns are relative to the package. + // However, we need to change the globbing to be relative to the repo root. + // Prepend the package path to each of the input patterns. + prefixedInputPatterns := make([]string, len(calculatedInputs)) + for index, pattern := range calculatedInputs { + rerooted, err := rootPath.PathTo(pkgPath.Join(pattern)) + if err != nil { + return nil, err + } + prefixedInputPatterns[index] = rerooted + } + + absoluteFilesToHash, err := globby.GlobFiles(rootPath.ToStringDuringMigration(), prefixedInputPatterns, nil) + if err != nil { return nil, errors.Wrapf(err, "failed to resolve input globs %v", calculatedInputs) } diff --git a/cli/internal/hashing/package_deps_hash_test.go b/cli/internal/hashing/package_deps_hash_test.go index 2eaedf570e649..0f764b56ef938 100644 --- a/cli/internal/hashing/package_deps_hash_test.go +++ b/cli/internal/hashing/package_deps_hash_test.go @@ -230,6 +230,7 @@ func requireGitCmd(t *testing.T, repoRoot turbopath.AbsolutePath, args ...string func TestGetPackageDeps(t *testing.T) { // Directory structure: // / + // new-root-file <- new file not added to git // my-pkg/ // committed-file // deleted-file @@ -280,6 +281,11 @@ func TestGetPackageDeps(t *testing.T) { err = uncommittedFilePath.WriteFile([]byte("uncommitted bytes"), 0644) assert.NilError(t, err, "WriteFile") + // create an untracked file in git up a level + rootFilePath := repoRoot.Join("new-root-file") + err = rootFilePath.WriteFile([]byte("new-root bytes"), 0644) + assert.NilError(t, err, "WriteFile") + tests := []struct { opts *PackageDepsOptions expected map[turbopath.AnchoredUnixPath]string @@ -320,6 +326,20 @@ func TestGetPackageDeps(t *testing.T) { "dir/nested-file": "bfe53d766e64d78f80050b73cd1c88095bc70abb", }, }, + // inputs with traversal work + { + opts: &PackageDepsOptions{ + PackagePath: "my-pkg", + InputPatterns: []string{"../**/*-file"}, + }, + expected: map[turbopath.AnchoredUnixPath]string{ + "../new-root-file": "8906ddcdd634706188bd8ef1c98ac07b9be3425e", + "committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033", + "uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976", + "package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b", + "dir/nested-file": "bfe53d766e64d78f80050b73cd1c88095bc70abb", + }, + }, // inputs with another glob pattern works { opts: &PackageDepsOptions{ @@ -332,6 +352,19 @@ func TestGetPackageDeps(t *testing.T) { "uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976", }, }, + // inputs with another glob pattern + traversal work + { + opts: &PackageDepsOptions{ + PackagePath: "my-pkg", + InputPatterns: []string{"../**/{new-root,uncommitted,committed}-file"}, + }, + expected: map[turbopath.AnchoredUnixPath]string{ + "../new-root-file": "8906ddcdd634706188bd8ef1c98ac07b9be3425e", + "committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033", + "package.json": "9e26dfeeb6e641a33dae4961196235bdb965b21b", + "uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976", + }, + }, } for _, tt := range tests { got, err := GetPackageDeps(repoRoot, tt.opts)