question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Truffle compiler option 'contracts_build_directory' inconsistent behavior

See original GitHub issue

Truffle compiler option contracts_build_directory is described here.

I’m having trouble with Truffle’s inconsistent behavior on that configuration parameter.

I’m setting it as follows:

contracts_build_directory: "../../artifacts"

When I execute truffle compile, all artifacts are created in a folder named “artifacts” which is located two levels above the project’s root directory.

When I execute truffle migrate --network=development, I get the following error message:

Running migration: 1_initial_migration.js
fs.js:904
  return binding.readdir(pathModule._makeLong(path), options.encoding);
                 ^

Error: ENOENT: no such file or directory, scandir '<see_below>\artifacts'

Where <see_below> is a full path which ends only one level above the project’s root directory.

I can therefore resolve this problem as follows:

  1. Run truffle compile
  2. Change contracts_build_directory from "../../artifacts" to "../../../artifacts"
  3. Run truffle migrate --network=development

This solution is inappropriate of course.

My project directory structure is as follows:

project folder:
    contracts folder:
        sol files
    migrations folder:
        1_initial_migration.js file
        2_deploy_contracts.js file
    test folder:
        js files
    truffle-config.js file

My truffle configuration (in truffle-config.js) is as follows:

// See <http://truffleframework.com/docs/advanced/configuration>
module.exports = {
    contracts_build_directory: "../../artifacts",
    networks: {
        development: {
            host:       "127.0.0.1",
            network_id: "*",
            port:       8545,
            gas:        4712388,      // Gas limit used for deploys
            gasPrice:   100000000000, // Gas price used for deploys
        },
    },
    mocha: {
        useColors: true,
        bail:      true,   // Abort after first test failure
        reporter:  "list", // See <https://mochajs.org/#reporters>
    },
    solc: {
        optimizer: {
            enabled: true,
            runs:    5000000,
        },
    },
};

To my understanding, the problem stems from the fact that truffle migrate “steps into” the migrations folder and runs from there (hence an additional ..\ is required in the value of the contracts_build_directory parameter).

UPDATE:

If I delete all the artifacts, and then run truffle migrate without preceding it with truffle compile (i.e., letting truffle migrate do the compilation), then it works only when the contracts_build_directory option is NOT specified. Any other way (even setting it to the default value ./build/contracts or to the “trivial” value ./) - the migration fails.

So in short, there seem to be a “collision” between truffle migrate and contracts_build_directory.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
lazyc97commented, Apr 2, 2018

I got problem at return binding.readdir(pathModule.toNamespacedPath(path), options.encoding) instead. I’m on Linux but toNamespacedPath seems to only work on Windows, might this be a problem with OS detection? I’m using Arch.

I found a workaround:

var path = require('path');
module.exports = {
    contracts_build_directory: path.join(__dirname, /* your output dir */)
}
1reaction
archerstar221commented, Sep 22, 2021

I have get the problem like this. So How to set directory?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it safe to commit truffle build files to Github (open source)?
There are two problems with Truffle compilation output (JSON files), which make it difficult to keep them under version-control:.
Read more >
Compile contracts - Truffle Suite
Upon first run, all contracts will be compiled. Upon subsequent runs, Truffle will compile only the contracts that have been changed since the...
Read more >
Ganache problems not completely resetting contracts ... But ...
Get a fresh Truffle project with your contracts. Compile the contracts, this makes your build/contracts/contract.json artifact files.
Read more >
truffle-deployments - npm
Start using truffle-deployments in your project by running `npm i ... compile the contracts and merge the deployment address of each ...
Read more >
Creating Ethereum Smart Contracts | by Michael Whittle
Just as a note, unit tests in Truffle can use Solidity or Mocha/Chai. ~/metacoin# truffle test. Using network 'test'.Compiling your contracts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found