Truffle: artifacts.require of npm dependency artifacts does not work

Created on 11 Oct 2019  路  7Comments  路  Source: trufflesuite/truffle

artifacts.require('<dependency>/<Contract>') is supposed to create a Truffle contract class based on node_modules/<dependency>/build/contracts/<Contract>.json. We are not able to get this to work in tests or migrations.

After some debugging we realized that the resolver is searching for this path under the working directory, which in tests and migrations is set to a directory like /tmp/test-119911-21025-1qqm8cp.8l6k that does not have a node_modules subdirectory.

We were also unable to pass a custom search path, which would've helped. Resolver has this functionality but it's not exposed, as shown below. I'm not sure if this is intentional.

https://github.com/trufflesuite/truffle/blob/c0aab909fa9a53a2c114d7a07aae26c59b2f2ab5/packages/core/lib/test.js#L199

Some context in https://github.com/OpenZeppelin/openzeppelin-test-helpers/issues/81.

We tested this in Truffle 5.0.29 and 5.0.39. cc @nventuro

bug needs reproduced

Most helpful comment

So in other words you are talking about importing JSON files with the artifact information instead of Solidity files. I'll take a look into this today. I'll get back to you about what I find, thanks!

All 7 comments

Thanks for raising this! We'll look into it.

@frangio Do you have a project where this is not working that I could clone to reproduce your issue? As far as I know, the temporary folder that Truffle creates for the artifact files should also get populated with the dependencies of your contracts. In other words, if you import any artifacts as dependencies they should get compiled into that temp folder as well.

Unfortunately I don't have a project ready to share, but we were able to reproduce this easily on a freshly initialized project with @openzeppelin/contracts installed via npm.

if you import any artifacts as dependencies they should get compiled into that temp folder as well.

Hm, I'm not sure if we're talking about the same thing. I'm talking about contracts that are precompiled in the dependency, and that are not imported in any Solidity file. So there is nothing that Truffle should compile here. It only needs to give access to the artifact present in the dependency directory.

So in other words you are talking about importing JSON files with the artifact information instead of Solidity files. I'll take a look into this today. I'll get back to you about what I find, thanks!

Hey @frangio can you give me a link to some docs describing what you are doing or give me a small snippet demonstrating what you want to do? I'm trying to figure out how to test and reproduce your error.

I tried using an ENS library to recreate your problem. I installed @ensdomains/ens and am able to successfully instantiate instances of ENSRegistry in both tests and migrations by doing the following...

const registry = artifacts.require("@ensdomains/ens/ENSRegistry");

If you have a couple minutes and are able to provide a simple repro example then let me know! I would love to try and help you get to the bottom of the issue.

Oh I see. It seems we are putting our artifacts under the wrong directory, in build/*.json instead of build/contracts/*.json.

@eggplantzzz Thanks for looking into this! We'll fix our package

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timothywangdev picture timothywangdev  路  3Comments

rjl493456442 picture rjl493456442  路  4Comments

ysfAskri picture ysfAskri  路  4Comments

TOMOAKI12345 picture TOMOAKI12345  路  3Comments

jleeh picture jleeh  路  3Comments