TypeChain icon indicating copy to clipboard operation
TypeChain copied to clipboard

Failing to generate types for @account-abstraction/contracts

Open naddison36 opened this issue 1 year ago • 2 comments

I'm trying to use the ERC-4337 contracts in the @account-abstraction/contracts package.

I've created a simple Hardhat project that attempts to compile and generate types for the @account-abstraction contracts. To replicate, please use the typechain branch as that is barebones. https://github.com/naddison36/abstract-accounts/tree/typechain

When I compile the project using yarn compile, I get the following error

Generating typings for: 17 artifacts in dir: src/types/typechain for target: ethers-v5
An unexpected error occurred:

SyntaxError: 'from' expected. (4:24)
  2 | /* tslint:disable */
  3 | /* eslint-disable */
> 4 | import type * as  from './..';
    |                        ^
  5 | export type {  };
  6 | export * as accountAbstraction from './@account-abstraction';
  7 | export * as openzeppelin from './@openzeppelin';
    at D (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/parser-typescript.js:1:17100)
    at LT (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/parser-typescript.js:257:10765)
    at Object.RT [as parse] (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/parser-typescript.js:257:11074)
    at Object.parse (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:7515:23)
    at coreFormat (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:8829:18)
    at formatWithCursor2 (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:9021:18)
    at /Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:38176:12
    at Object.format (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:38190:12)
    at prettierOutputTransformer (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/typechain/src/codegen/outputTransformers/prettier.ts:8:19)
    at /Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/typechain/src/typechain/io.ts:22:33 {
  loc: { start: { line: 4, column: 24 } },
  codeFrame: '\x1B[0m \x1B[90m 2 |\x1B[39m \x1B[90m/* tslint:disable */\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 3 |\x1B[39m \x1B[90m/* eslint-disable */\x1B[39m\x1B[0m\n' +
    "\x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 4 |\x1B[39m \x1B[36mimport\x1B[39m type \x1B[33m*\x1B[39m \x1B[36mas\x1B[39m  \x1B[36mfrom\x1B[39m \x1B[32m'./..'\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n" +
    '\x1B[0m \x1B[90m   |\x1B[39m                        \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 5 |\x1B[39m \x1B[36mexport\x1B[39m type {  }\x1B[33m;\x1B[39m\x1B[0m\n' +
    "\x1B[0m \x1B[90m 6 |\x1B[39m \x1B[36mexport\x1B[39m \x1B[33m*\x1B[39m \x1B[36mas\x1B[39m accountAbstraction \x1B[36mfrom\x1B[39m \x1B[32m'./@account-abstraction'\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n" +
    "\x1B[0m \x1B[90m 7 |\x1B[39m \x1B[36mexport\x1B[39m \x1B[33m*\x1B[39m \x1B[36mas\x1B[39m openzeppelin \x1B[36mfrom\x1B[39m \x1B[32m'./@openzeppelin'\x1B[39m\x1B[33m;\x1B[39m\x1B[0m"
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Interestingly, types are created in the src/types and src/types/types folders. If I just use the Hardhat typechain plug-in defaults, types are created in typechain-types and the project root.

Screenshot 2023-03-15 at 10 54 28 am

I've kept the installed packages to a minimum to try and isolate the problem

npm ls
[email protected] /Users/nicholasaddison/Documents/workspaces/abstract-accounts
├── @account-abstraction/[email protected]
├── @nomiclabs/[email protected]
├── @openzeppelin/[email protected]
├── @typechain/[email protected]
├── @typechain/[email protected]
├── @types/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

The typechain config in Hardhat is

typechain: {
      outDir: "src/types/typechain",
      target: "ethers-v5",
  },

naddison36 avatar Mar 15 '23 00:03 naddison36

This seems to be caused by Typechain normalizing files/directories names by simply removing special symbols; in this case, it's importing from "./..", which after normalized, becomes "". We got a similar issue, but with version-named folders:

import type * as 047 from './0.4.7';

047 is the naive normalization of the path, but isn't a valid identifier, and prettier is rightfully complaining. Whatever function is generating those alias import names, should make sure they're both unique and valid.

andrevmatos avatar Mar 21 '23 00:03 andrevmatos

Thanks for commenting @andrevmatos.

The contracts in @account-abstraction/contracts do use relative paths but so does openzeppelin-contracts which does work with Typechain when imported to a project.

Screenshot 2023-03-21 at 1 53 17 pm

I'll try stripping out the contracts in @account-abstraction/contracts to see if I can isolate which contracts are causing the problem.

naddison36 avatar Mar 21 '23 03:03 naddison36