Truffle: Truffle should work when filename does not match contract name, example from the docs

Created on 1 Sep 2017  Â·  20Comments  Â·  Source: trufflesuite/truffle

Issue

According to Getting started guide, the name of the contract does not have to match filename.

The name specified does not have to match the filename of your contract source files

It does not work for me

Steps to Reproduce

Follow the steps described in the docs

truffle init

create file ./contracts/Contracts.sol

source:

contract ContractOne {
}

contract ContractTwo {
}

run:

truffle test

Expected Behavior

All tests should pass.

Actual Results

tests fail with

    1) "before all" hook: prepare suite

  Contract: MetaCoin
    ✓ should put 10000 MetaCoin in the first account (39ms)
    ✓ should call a function that depends on a linked library (75ms)
    ✓ should send coin correctly (153ms)


  3 passing (613ms)
  1 failing

  1) TestMetacoin "before all" hook: prepare suite:
     Error: Could not find artifacts for /Users/marek/projects/ethcore/bridge/truffle/contracts/Contracts.sol from any sources
      at Resolver.require (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:49072:9)
      at TestResolver.require (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:89733:30)
      at TestResolver.require (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:89733:30)
      at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:214313:49
      at Array.forEach (native)
      at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:214312:24
      at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:218209:15
      at process._tickCallback (internal/process/next_tick.js:103:7)

Environment

  • Operating System: macOS Sierra 10.12.6
  • Truffle version: Truffle v3.4.9
  • Ethereum client: testrpc
  • node version: v7.4.0
  • npm version: 5.0.4

Most helpful comment

hi @tcoulter !

This issue is not related with loading artifacts for js tests. It's about a bug in default solidity tests loader. The bug is somehow related with loading .sol files containing multiple tests by a default test .sol file.

If I remove default .sol tests, everything works fine.

All 20 comments

Hi there.

The correct way to get the artifacts for each individual contract is like this:

var ContracOne = artifacts.require("ContractOne");
var ContractTwo = artifacts.require("ContractTwo");

Note the .sol extension isn't needed (especially in this case, since there are multiple contracts in each file).

Thanks for the ticket. Will use this to beef up our documentation.

Cheers!

hi @tcoulter !

This issue is not related with loading artifacts for js tests. It's about a bug in default solidity tests loader. The bug is somehow related with loading .sol files containing multiple tests by a default test .sol file.

If I remove default .sol tests, everything works fine.

Stumbled upon this bug as well. Seems like it makes it impossible to test multiple contracts that are in the same file through Solidity tests.

PR for docs update: https://github.com/trufflesuite/trufflesuite.com/pull/10

This should hopefully clear up the confusion.

@bmmpxf the pr for the docs is totally unrelated. Please look at 'steps to reproduce' again.

Looks like there might be a different issue here, but we'll need to investigate. Would also be great to get more information.

@t4t5 @debris Did you add an artifacts.require() statement in any of your tests related to Contracts.sol, ContractOne or ContractTwo?

Did you add an artifacts.require() statement in any of your tests related to Contracts.sol, ContractOne or ContractTwo?

No ;)

Any news on this ? I just managed to have the same problem. I'll post my code and config tomorrow to see if it can help.

Note that running each test file separately doesn't generate the failures on my side.

I ran into this problem as well, with truffle v4.0.5. I have a contract called Foos in a file called Foo.sol. If my migration used any of the following lines, it fails;

  • var Foo = artifacts.require("./Foo.sol"); (despite it working for ./Migrations.sol)
  • var Foo = artifacts.require("Foo.sol");
  • var Foo = artifacts.require("Foo");

and if I changed it to var Foo = artifacts.require("Foos"); it worked.

truffle migrate

also have the same issue. there was an older YouTube that shows this used to work with Solidity 0.4.11 where the name of the contracts does not match the file Youtube look at time 21:28 (min:sec), and you will see the demo show truffle migrate can do this.

Instead when I tried it I go the following error:

/usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-resolver/index.js:37
  throw new Error("Could not find artifacts for " + import_path + " from any sources");
^
Error: Could not find artifacts for ./Elections from any sources
    at Resolver.require (/usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-resolver/index.js:37:1)
    at Object.require (/usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-core/~/truffle-migrate/index.js:234:1)
    at ResolverIntercept.require (/usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-core/~/truffle-migrate/resolverintercept.js:20:1)
    at /Users/nelsontam/Documents/Work/blockchain/election/migrations/2_deploy_contracts.js:1:26
    at ContextifyScript.Script.runInContext (vm.js:35:29)
    at ContextifyScript.Script.runInNewContext (vm.js:41:15)
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-require/require.js:97:1
    at tryToString (fs.js:456:3)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:443:12)

This happened to me as well. It appears that the contract name "must" match the variable name on import/export (because is the object you are actually importing):

_YourContractFileName.sol_
`pragma solidity ^0.4.4;

contract ContractName {
string public candidate = "Any Name";
constructor () public {
}
function GetMessage() public returns (string) {
return candidate;
}
}`

_Your migration file_
`var ContractName = artifacts.require("./YourContractFileName");

module.exports = function(deployer) {
deployer.deploy(ContractName);
};`

Have related issue with:

1_initial_migration.js

const DMT = artifacts.require("Token");
const DemoCrowdsale = artifacts.require("Crowdsale");

module.exports = function(deployer) {
  deployer.deploy(Token)
    .then(function() {
      return deployer.deploy(Crowdsale);
        });
};

truffle.js

networks: {
  ropsten: {
    provider: function() {
      return new HDWalletProvider(mnemonic, providerUrl);
    },
    network_id: '3',
    gas: 4512388
  }

As result of execution all contracts deployed but:

Running migration: 1_initial_migration.js
  Deploying Token...
  ... 0x
  Token: 0x
  Deploying Crowdsale...
  ... 0x
  Crowdsale: 0x
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: Could not find artifacts for ./Migrations from any sources
    at Resolver.require (/usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-resolver/index.js:37:1)
    at Object.require (/usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-core/~/truffle-migrate/index.js:234:1)
    at ResolverIntercept.require (/usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-core/~/truffle-migrate/resolverintercept.js:20:1)
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-core/~/truffle-migrate/index.js:49:1
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:118:7)

There is no folder /usr/local/lib/node_modules/truffle/build/webpack .

~/truffle-resolver/index.js:37:1 is
throw new Error("Could not find artifacts for " + import_path + " from any sources");

UPD:
Okay, added the 1_initial_migration.js and dummy Migrations.sol contract, moved my contracts/deployments into 2_migration.js
Issue's gone.
But now stuck on 'Saving artifacts' issue:
https://github.com/trufflesuite/truffle-migrate/issues/14

I attempted to duplicate this on the most recent version of truffle and could not do so. Is this still a problem? If not I will close this issue in a few days.

This is still a problem in 4.1.14 yes, albeit with the test command not the compile command.

When file is called 1_test_assert.sol and the contract has TestAssert as name:

$ truffle test test/1_test_assert.sol
Compiling .\contracts\Assert.sol...
Compiling .\contracts\Migrations.sol...
Compiling .\test\1_test_assert.sol...
Compiling .\test\Koans.sol...
Error: Could not find artifacts for ./1_test_assert.sol from any sources
    at Resolver.require (C:\Users\N.vergauwen\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-resolver\index.js:37:1)
    at TestResolver.require (C:\Users\N.vergauwen\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-core\lib\testing\testresolver.js:17:1)
    at C:\Users\N.vergauwen\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-core\lib\test.js:109:1
    at Array.map (<anonymous>)
    at C:\Users\N.vergauwen\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-core\lib\test.js:107:1

Reproduction: https://github.com/kyriediculous/solidity-koans

I think the CLI should not take artifacts but paths, I think you should only be able to use the contractName when explicitly calling artifacts.require() in migrations/test files since you are then calling on the JSON artifacts, not the Solidity file. Does that make sense @eggplantzzz ?

I feel like this is a really simple convention that could clear up alot of confusion around that subject because I've seen many people struggle with that bit.

Its very misleading because the truffle migration file does show a path in artifacts.require() which is wrong because to start with there is no Migrations.sol file in the migrations directory. But perhaps I should make a seperate issue for this last thing.
CORRECT: var Migrations = artifacts.require("Migrations");
WRONG(at least it seems wrong to me?): var Migrations = artifacts.require("./Migrations.sol");

Thank you for raising this issue! It has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. If you would like to keep this issue open, please respond with information about the current state of this problem.

I would upgrade to version 5 as that is the newest stable version of Truffle. As far as I can tell this issue does not exist there but let me know if you encounter problems!
https://github.com/trufflesuite/truffle/releases/tag/v5.0.1

Thanks for your response! This issue is no longer considered stale and someone from the Truffle team will try to respond as soon as they can.

Closing since this no longer appears to be an issue on the latest version of truffle (#1878). Happy to reopen if it still is!

Happening to me on v5.0.25

@PochenriederAlex can you provide reproducible steps?

Was this page helpful?
0 / 5 - 0 ratings