o1-labs / zkapp-cli

CLI to create a zkApp (zero-knowledge app) for Mina Protocol
https://docs.minaprotocol.com/zkapps/how-to-write-a-zkapp
Apache License 2.0
114 stars 43 forks source link

Cannot deploy contracts that use proofs in nested folders #627

Closed emlautarom1 closed 3 weeks ago

emlautarom1 commented 4 months ago

Describe the bug

There is already open issue where a SmartContract cannot be deployed if it's defined in a folder (https://github.com/o1-labs/zkapp-cli/issues/593). A similar issue exists when a SmartContract makes usage of a ZkProgram's Proof as an input, forcing developer to have everything in the top level folder (src)

Steps to Reproduce

  1. Create a SmartContract in the root folder (ex. src/my-smart-contract.ts) that uses a proof generated by a ZkProgram that is defined in a file in a nested folder (ex. src/proofs/my-zk-program.ts).
  2. Configure the deployment of the contract using the zk config command.
  3. Try to deploy the contract using the zk deploy command.

You should get the following stack trace:

✔ Build project
✔ Generate build.json
✔ Choose smart contract
  The 'MySmartContract' smart contract will be used
  for this deploy alias as specified in config.json.
✖ Generate verification key (takes 10-30 sec)
  Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/emlautarom1/dev/myzkapp/build/src/my-zk-program.js' imported from /home/emlautarom1/.nvm/versions/node/v21.6.1/lib/node_modules/zkapp-cli/src/lib/deploy.js
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/emlautarom1/dev/myzkapp/build/src/my-zk-program.js' imported from /home/emlautarom1/.nvm/versions/node/v21.6.1/lib/node_modules/zkapp-cli/src/lib/deploy.js
    at finalizeResolution (node:internal/modules/esm/resolve:263:11)
    at moduleResolve (node:internal/modules/esm/resolve:908:10)
    at defaultResolve (node:internal/modules/esm/resolve:1131:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:322:34)
    at importModuleDynamically (node:internal/modules/esm/translators:159:35)
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:207:14)
    at getZkProgram (file:///home/emlautarom1/.nvm/versions/node/v21.6.1/lib/node_modules/zkapp-cli/src/lib/deploy.js:767:28)
    at async generateVerificationKey (file:///home/emlautarom1/.nvm/versions/node/v21.6.1/lib/node_modules/zkapp-cli/src/lib/deploy.js:549:19)
    at async file:///home/emlautarom1/.nvm/versions/node/v21.6.1/lib/node_modules/zkapp-cli/src/lib/deploy.js:274:7
    at async step (file:///home/emlautarom1/.nvm/versions/node/v21.6.1/lib/node_modules/zkapp-cli/src/lib/helpers.js:17:20)
    at async deploy (file:///home/emlautarom1/.nvm/versions/node/v21.6.1/lib/node_modules/zkapp-cli/src/lib/deploy.js:270:41)
    at async Object.handler (file:///home/emlautarom1/.nvm/versions/node/v21.6.1/lib/node_modules/zkapp-cli/src/bin/index.js:130:30) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///home/emlautarom1/dev/myzkapp/build/src/my-zk-program.js'
}

The issue lies in the following lines:

https://github.com/o1-labs/zkapp-cli/blob/c166e4a46430d89ec96af6ff496188054a7a2d61/src/lib/deploy.js#L741-L743

This function returns the file name (through path.basename) which then gets appended to a fixed path listed here:

https://github.com/o1-labs/zkapp-cli/blob/c166e4a46430d89ec96af6ff496188054a7a2d61/src/lib/deploy.js#L761-L764

The whole process should be rewritten instead to use path.resolve without the need to append any path prefix (which should be done through path.join instead of interpolated strings with /)

System Info

System:
  OS: Linux 6.8 Fedora Linux 39 (Workstation Edition)
  CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
Binaries:
  Node: 21.6.1 - ~/.nvm/versions/node/v21.6.1/bin/node
  Yarn: 1.22.21 - /usr/local/bin/yarn
  npm: 10.2.4 - ~/.nvm/versions/node/v21.6.1/bin/npm
npmPackages:
  o1js: ^0.17.0 => 0.17.0 
npmGlobalPackages:
  zkapp-cli: 0.20.0

Severity

serious, but I can work around it

Additional Information

No response

ymekuria commented 4 months ago

This issue has been in our backlog, but we have not had a chance to update this yet.