diff --git a/.changeset/grumpy-shoes-return.md b/.changeset/grumpy-shoes-return.md new file mode 100644 index 0000000000..e5bf70c0a5 --- /dev/null +++ b/.changeset/grumpy-shoes-return.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Fixed an issue with the `--successCache` flag for `repo test` where the tree hash for the wrong package directory would sometimes be used to generate the cache key. diff --git a/packages/cli/src/commands/repo/test.ts b/packages/cli/src/commands/repo/test.ts index 1035354eb2..5ce6bca342 100644 --- a/packages/cli/src/commands/repo/test.ts +++ b/packages/cli/src/commands/repo/test.ts @@ -54,26 +54,36 @@ interface GlobalWithCache extends Global { * Use git to get the HEAD tree hashes of each package in the project. */ async function readPackageTreeHashes(graph: PackageGraph) { - const pkgs = Array.from(graph.values()); + const pkgs = Array.from(graph.values()).map(pkg => ({ + ...pkg, + path: relativePath(paths.targetRoot, pkg.dir), + })); const output = await runPlain( 'git', 'ls-tree', - '--object-only', + '--format="%(objectname)=%(path)"', 'HEAD', '--', - ...pkgs.map(pkg => relativePath(paths.targetRoot, pkg.dir)), + ...pkgs.map(pkg => pkg.path), ); - const treeShaList = output.trim().split(/\r?\n/); - if (treeShaList.length !== pkgs.length) { - throw new Error( - `Error listing project git tree hashes, output length does not equal input length`, - ); - } - const map = new Map( - pkgs.map((pkg, i) => [pkg.packageJson.name, treeShaList[i]]), + output + .trim() + .split(/\r?\n/) + .map(line => { + const [itemSha, ...itemPathParts] = line.split('='); + const itemPath = itemPathParts.join('='); + const pkg = pkgs.find(p => p.path === itemPath); + if (!pkg) { + throw new Error( + `Unexpectedly missing tree sha entry for path ${itemPath}`, + ); + } + return [pkg.packageJson.name, itemSha]; + }), ); + return (pkgName: string) => { const sha = map.get(pkgName); if (!sha) {