Truffle: Problems when contract names conflict with internal node objects.

Created on 31 Aug 2020  路  9Comments  路  Source: trufflesuite/truffle

I've asked for help in the Truffle Gitter and was asked to file an issue here.


Issue

Truffle console crashes if there is a contract called Buffer.

From Gitter:

If you have one named "Buffer" it overwrites the native "Buffer" object. This will cause problems. I assume somewhere in your project there could be a contract named "Buffer"?

Steps to Reproduce

truffle init
yarn add @chainlink/contracts

MyTest.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol";

contract MyTest is ChainlinkClient {}

Expected Behavior

I expect the truffle console (truffle develop) to still work after compiling the contracts.

Actual Results

Truffle console crashes with following exception:

truffle(develop)> version
Truffle v5.1.42 (core: 5.1.42)
Solidity - ^0.6.0 (solc-js)
Node v12.18.2
Web3.js v1.2.1
truffle(develop)> TypeError: Buffer.isBuffer is not a function
    at Function.writeFileSync [as sync] (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/write-file-atomic/index.js:213:1)
    at Configstore.set all [as all] (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/configstore/index.js:58:1)
    at new Configstore (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/configstore/index.js:25:1)
    at Function.getUserConfig (/usr/local/lib/node_modules/truffle/build/webpack:/packages/config/dist/index.js:171:1)
    at Object.send (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/services/analytics/index.js:5:1)
    at Command.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:137:1)
    at Console.interpret (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:155:1)
    at ReplManager.interpret (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/repl.js:129:1)
    at bound (domain.js:426:14)
    at REPLServer.runBound [as eval] (domain.js:439:12)
    at REPLServer.onLine (repl.js:760:10)
    at REPLServer.emit (events.js:315:20)
    at REPLServer.EventEmitter.emit (domain.js:482:12)
    at REPLServer.Interface._onLine (readline.js:329:10)
    at REPLServer.Interface._line (readline.js:658:8)
    at REPLServer.Interface._ttyWrite (readline.js:999:14)
    at REPLServer.self._ttyWrite (repl.js:851:9)
    at ReadStream.onkeypress (readline.js:205:10)
    at ReadStream.emit (events.js:315:20)
    at ReadStream.EventEmitter.emit (domain.js:482:12)
    at emitKeys (internal/readline/utils.js:335:14)
    at emitKeys.next (<anonymous>)

Environment

Operating System: Linux Mint 20 Ulyana
Truffle v5.1.42 (core: 5.1.42)
Solidity - ^0.6.0 (solc-js)
Node v12.18.2
Web3.js v1.2.1

Conclusion

As it turns out, in general, contract names can conflict with node object names. This needs to be circumvented with proper namespacing.

This, however, causes an issue when there are contract names that conflict with Node native objects; in this case the native Buffer object. It would be kind of like if you had a contract named "String". This would overwrite the native String object and it would become inaccessible.

bug priority2 鈿狅笍

All 9 comments

I believe this same issue came up earlier as #2904.

Oh indeed. Couldn't find it when searching.

I say we keep this one open for tracking as it is a bit more general than the other.

I'd also love to see this merged: https://github.com/trufflesuite/trufflesuite.com/pull/775
Maybe it saves others some time.

Ok @mktcode, I'll take a look!

I believe the solution here is:

  • check for existing globals before adding all the contract name identifiers
  • expose artifacts much the same as we would inside a Truffle test script (const Buffer = artifacts.require("Buffer"))

Note that once this issue is resolved we'll have to change the note added to the website in PR https://github.com/trufflesuite/trufflesuite.com/pull/775

@eggplantzzz Sure. In your gitter chat I was told that this might take until the next major release. I don't know your release plan but I thought for the time being it's better to have a note in the docs. I created a reminder issue here: https://github.com/trufflesuite/trufflesuite.com/issues/781

Thanks @mktcode! I might try and fit in a fix for this before the next major release at some point if I can find the time.

Was this page helpful?
0 / 5 - 0 ratings