From 91077132615ecab9df4afc2ea8308d25dd2d50f8 Mon Sep 17 00:00:00 2001 From: Kris Kaczor Date: Mon, 24 Jul 2023 11:09:10 +0400 Subject: [PATCH] Fix the `lowestCommonPath` function, #772 (#858) Co-authored-by: Alex Dupre --- .changeset/ten-pants-learn.md | 5 +++++ .../typechain/src/utils/files/lowestCommonPath.ts | 14 +++++++++++++- .../test/utils/files/lowestCommonPath.test.ts | 10 ++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .changeset/ten-pants-learn.md diff --git a/.changeset/ten-pants-learn.md b/.changeset/ten-pants-learn.md new file mode 100644 index 000000000..7bf5e117a --- /dev/null +++ b/.changeset/ten-pants-learn.md @@ -0,0 +1,5 @@ +--- +'typechain': patch +--- + +Fix the detection of inputs root in some specific scenarios diff --git a/packages/typechain/src/utils/files/lowestCommonPath.ts b/packages/typechain/src/utils/files/lowestCommonPath.ts index 4c3bcdbf9..c660eb790 100644 --- a/packages/typechain/src/utils/files/lowestCommonPath.ts +++ b/packages/typechain/src/utils/files/lowestCommonPath.ts @@ -1,5 +1,17 @@ export function lowestCommonPath(paths: string[]) { const pathParts = paths.map((path) => path.split(/[\\/]/)) - const commonParts = pathParts[0].filter((part, index) => pathParts.every((parts) => parts[index] === part)) + const commonParts = [] as string[] + const maxParts = Math.min.apply( + null, + pathParts.map((p) => p.length), + ) + for (let i = 0; i < maxParts; i++) { + const part = pathParts[0][i] + if (pathParts.slice(1).every((otherPath) => otherPath[i] === part)) { + commonParts.push(part) + } else { + break + } + } return commonParts.join('/') } diff --git a/packages/typechain/test/utils/files/lowestCommonPath.test.ts b/packages/typechain/test/utils/files/lowestCommonPath.test.ts index 0c4d190cb..21ffe88da 100644 --- a/packages/typechain/test/utils/files/lowestCommonPath.test.ts +++ b/packages/typechain/test/utils/files/lowestCommonPath.test.ts @@ -18,6 +18,16 @@ describe(lowestCommonPath.name, () => { expect(actual).toEqual('/TypeChain/contracts/compiled') }) + it('stops at first different sub-path', () => { + const paths = [ + '/TypeChain/contracts/v0.6.4/interfaces/Payable.abi', + '/TypeChain/contracts/v0.8.9/interfaces/Rarity.abi', + ] + + const actual = lowestCommonPath(paths) + expect(actual).toEqual('/TypeChain/contracts') + }) + it('works for Windows paths', () => { const paths = [ 'D:/workspace/TypeChain/packages/hardhat/test/fixture-projects/hardhat-project/artifacts/contracts/EdgeCases.sol/EdgeCases.json',