TypeChain icon indicating copy to clipboard operation
TypeChain copied to clipboard

Typechain fails where there are no contracts in a shared parent directory

Open alexdupre opened this issue 2 years ago • 4 comments

Here is a sample artifacts hierarchy structure to reproduce the bug:

contracts/1/interfaces/IOne.sol/IOne.json
contracts/2/interfaces/ITwo.sol/ITwo.json

Running typechain on a single directory works fine, typechain --target ethers-v5 --out-dir './typechain' './contracts/1/**/*.json' generates the following files:

./typechain/common.ts
./typechain/factories/index.ts
./typechain/factories/IOne__factory.ts
./typechain/index.ts
./typechain/IOne.ts

On the other hand, trying to generate the types for both contracts concurrently with typechain --target ethers-v5 --out-dir './typechain' './contracts/**/*.json' generates the following error:

Error occured:  'from' expected. (4:24)
  2 | /* tslint:disable */
  3 | /* eslint-disable */
> 4 | import type * as  from './..';
    |                        ^
  5 | export type {  };
  6 | export * as 1 from './1';
  7 | export * as 2 from './2';

and some files are generated outside the requested out-dir:

./1/interfaces/IOne.ts
./2/interfaces/ITwo.ts
./typechain/common.ts

alexdupre avatar Sep 15 '22 10:09 alexdupre

The issue seems to be in the lowestCommonPath function that returns the wrong directory.

In particular this line is wrong: https://github.com/dethcrypto/TypeChain/blob/master/packages/typechain/src/utils/files/lowestCommonPath.ts#L3

because in the above scenario it returns contracts/interfaces as the lowest common path (a directory that doesn't even exist) instead of contracts.

alexdupre avatar Sep 19 '22 16:09 alexdupre

I encountered this bug when working with HardHat, and it took me a while to realize the problem was not my HardHat settings but actually in TypeChain library. Thx for the fix @alexdupre!

I can confirm the related PR #773 fixes the issue. Can we get it merged, please? (@krzkaczor, sry for pinging, but can you do that, please?)

ondratra avatar Jan 31 '23 20:01 ondratra

Same here, this pR #773 fixes it

kazazor avatar Jul 23 '23 15:07 kazazor

@kazazor, thanks for mentioning this. I am merging the fix now.

krzkaczor avatar Jul 24 '23 06:07 krzkaczor