trufflesuite / truffle

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat?utm_source=github&utm_medium=referral&utm_campaign=2023_Sep_truffle-sunset-2023_announcement_
MIT License
14.02k stars 2.32k forks source link

Compile Error #3798

Closed daqingsu19 closed 3 years ago

daqingsu19 commented 3 years ago

I wanted to try out Truffle/Ganache. So I followed

https://ethereum.org/en/developers/tutorials/create-and-deploy-a-defi-app/

-I am having version issues with Truffle. This is what "truffle version" shows: Truffle v5.1.66 (core: 5.1.66) Solidity - 0.7.4 (solc-js) Node v15.5.1 Web3.js v1.2.9 I specified solc 0.7.4 in truffle-config.js because the openzeppelin that I use asks for pragma between 0.6 and 0.8. I specified "pragma solidity >=0.4.22 <0.9.0;" in my contracts. But when I do "truffle compile", I got this:

Compiling your contracts... ✔ Fetching solc version list from solc-bin. Attempt #1 ✔ Downloading compiler. Attempt #1. TypeError: Cannot read property 'imports' of undefined at Object. (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:98:1) at Generator.next () at fulfilled (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:5:42) Truffle v5.1.66 (core: 5.1.66) Node v15.5.1

Seems like I am not able to make truffle, solidity, and openzeppelin all happy wrt versions.

Eliseowzy commented 3 years ago

I also have the same problem

xternet commented 3 years ago

Downgrade truffle to: 5.1.65 Set contracts and truffle-config.js solc version to: >=0.6.0 <0.8.0

daqingsu19 commented 3 years ago

Downgrade truffle to: 5.1.65 Set contracts and truffle-config.js solc version to: >=0.6.0 <0.8.0

Yes downgrading to 5.1.65 works. Thanks a lot!

Eliseowzy commented 3 years ago

Downgrade truffle to: 5.1.65 Set contracts and truffle-config.js solc version to: >=0.6.0 <0.8.0

Thank you sooooo much!😀

zacel commented 3 years ago

Thank you for this solution but why did you close the issue? it seems like if downgrading to a older version is the fix then it should still be open because it's still broken in 5.1.66 which is what new people will install by default

khushjammu commented 3 years ago

I'm having a similar issue. The initial build of my contracts works fine (when artifacts folder is empty). However, every subsequent time I try to truffle compile, I get a weird error.

Compiling your contracts...
===========================
TypeError: Cannot read property 'imports' of undefined
    at Object.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:98:1)
    at Generator.next (<anonymous>)
    at fulfilled (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:5:42)

I'm attempting to build with solc version 0.8.1

fernandezpaco commented 3 years ago

Same problem here

ehildenb commented 3 years ago

We had the same issue, commenting here to track progress.

jha-prateek commented 3 years ago

Downgrade doesn't work

image

jha-prateek commented 3 years ago

I'm having a similar issue. The initial build of my contracts works fine (when artifacts folder is empty). However, every subsequent time I try to truffle compile, I get a weird error.

Compiling your contracts...
===========================
TypeError: Cannot read property 'imports' of undefined
    at Object.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:98:1)
    at Generator.next (<anonymous>)
    at fulfilled (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:5:42)

I'm attempting to build with solc version 0.8.1

have you found any solution to this?

zacel commented 3 years ago

I have been getting the same error and found 2 solutions. First is to use a older solidity version (it works for me with 0.6.6) or to use a different tool then truffle (like hardhat)

jha-prateek commented 3 years ago

I have been getting the same error and found 2 solutions. First is to use a older solidity version (it works for me with 0.6.6) or to use a different tool then truffle (like hardhat)

I tried with Truffle v5.1.65 (core: 5.1.65) and Solc 0.6.6, but nothing works🤯

zacel commented 3 years ago

for me I had to delete truffle globally, delete node_modules in my project and then after reinstalling everything it worked

sofianeOuafir commented 3 years ago

downgrading to 5.1.65 worked for me. curious to know why 5.1.66 didn't work

jha-prateek commented 3 years ago

truffle compile --all

This works

Eliseowzy commented 3 years ago

Compiling your contracts...

TypeError: Cannot read property 'imports' of undefined...

  • I reinstalled Truffle: npm install –g truffle@~5.1.65
  • The OS of my device is Windows 10, and truffle, solidity and node.js versions are as follows: Truffle v5.1.65 (core: 5.1.65) Solidity v0.5.16 (solc-js) Node v12.16.1
  • Then I declared pragma solidity ^0.5.16; at the begining of every contract. I also modified the fruffle-config.js file of the project into:
    module.exports = {
    // See <http://truffleframework.com/docs/advanced/configuration>
    // for more about customizing your Truffle configuration!
    networks: {
    development: {
    host: "127.0.0.1",
    port: 7545,
    network_id: "*" // Match any network id
    },
    develop: {
    port: 8545
    }
    },
    compilers: { //I added this parameter.
    solc: {
    version: "0.5.16"
    }
    }
    };
  • Then under the project root directory(project_name/): I tried the commandtruffle test again, it worked for me.
ehildenb commented 3 years ago

Downgrading to truffle@5.1.65 worked for me. npm install truffle@5.1.65

snwokenk commented 3 years ago

@jha-prateek answer of using "truffle compile --all" worked for me.

It seems most are making changes or adding new contracts and forgetting to recompile

haltman-at commented 3 years ago

OK, it seems a lot of people are having this problem. Does anyone have a reproduction example we could check out? Otherwise I'll attemtp to reproduce this myself, but I'm hoping someone has an example I can use directly. Thank you!

haltman-at commented 3 years ago

I have a suspicion that PR #3802 might fix this problem. However, I really need a reproduction example to be able to check...

sshmaxime commented 3 years ago

Try to delete your build folder, it worked for me.

ehildenb commented 3 years ago

@haltman-at

Reproduction:

git clone 'https://github.com/runtimeverification/firefly-demo'
cd firefly-demo
git checkout f6c72e0
npm install
npm install ganache-cli

In one terminal:

./node_modules/.bin/ganache-cli -p 8145

In another terminal:

./node_modules/.bin/truffle test

Results in this error:

Using network 'development'.

Compiling your contracts...
===========================
TypeError: Cannot read property 'imports' of undefined
    at Object.<anonymous> (/home/dev/src/firefly-demo/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:98:1)
    at Generator.next (<anonymous>)
    at fulfilled (/home/dev/src/firefly-demo/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:5:42)
Truffle v5.1.66 (core: 5.1.66)
Node v10.23.0

Downgrading to 5.1.65 and the error goes away and tests run successfully.

I'm on Node 10.23.0, if it matters.

haltman-at commented 3 years ago

@ehildenb Thanks! I'll check it out.

haltman-at commented 3 years ago

Hm, I can't seem to reproduce this at the moment. I'll have to try some more combinations later, I guess. That said, I have a suspicion that #3802 might fix this anyway...

jha-prateek commented 3 years ago

@jha-prateek answer of using "truffle compile --all" worked for me.

It seems most are making changes or adding new contracts and forgetting to recompile

Yes, but it's strange because truffle compile should work even without using --all flag. Truffle docs says --all: Compile all contracts instead of only the contracts changed since last compile.

jha-prateek commented 3 years ago

Hm, I can't seem to reproduce this at the moment. I'll have to try some more combinations later, I guess. That said, I have a suspicion that #3802 might fix this anyway...

Can you check, how truffle compile --all is working and not truffle compile. This might help you in finding the problem. 😀

haltman-at commented 3 years ago

Ah, good point. I'd been compiling from scratch. But, the example given by @ehildenb above also involves compiling from scratch. So that doesn't help. Does anyone have a more specific reproduction example I could try, including which contracts to compile in what order?

haltman-at commented 3 years ago

OK, could people retry this with 5.1.67 and see if there's still a problem? Thanks all!

RedCuckoo commented 3 years ago

@haltman-at

I confirm that upgrading to 5.1.67 solved it for me, using the example provided here.

SIDE NOTE: This example only compiles successfully, but I can not deploy this contract. Can't find the issue.

Providing output:

[Execute command]
3_deploy_contracts.js
=====================
[Execute command]
[Execute command] Error: Migration LAPTOP\solidity_project\migrations\3_deploy_contracts.js invalid or does not take any parameters
    at Migration._load (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:49:1)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at Migration.run (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:171:1)
    at Object.runMigrations (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:150:1)
    at Object.runFrom (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:110:1)
    at Object.runAll (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:114:1)
    at Object.run (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:79:1)
    at runMigrations (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:262:1)
    at Object.run (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:227:1)
    at Command.run (USER\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\command.js:136:1)
[Execute command] Truffle v5.1.67 (core: 5.1.67)
Node v14.15.3
[Execute command] Finished running command
[Azure Blockchain] Deploy failed
RedCuckoo commented 3 years ago

SIDE NOTE: This example only compiles successfully, but I can not deploy this contract. Can't find the issue.

UPD: I did a stupid error by not saving edited file using Visual Studio Code, so it was trying to deploy an empty file probably. The example is working.

cds-amal commented 3 years ago

Oh gosh, @haltman-at , I think I'm running into this issue while running truffle test. Interestingly, it fails on my Arch machine and works on OS X.

update: pulled latest develop and it's fixed in my case.

╰─$ truffle test

Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Compiling ./contracts/ReentrancyAttack.sol
> Compiling ./contracts/ReentrancyGuard.sol
> Compiling ./contracts/ReentrancyMock.sol
> Compiling ./test/TestGuard.sol
> Artifacts written to /tmp/test--1004680-D92aN45Zb2bp
> Compiled successfully using:
   - solc: 0.8.1+commit.df193b15.Emscripten.clang

  TestGuard
    1) "before all" hook: prepare suite for "testTargetIsInitialized"

  0 passing (169ms)
  1 failing

  1) TestGuard
       "before all" hook: prepare suite for "testTargetIsInitialized":
     TypeError: Cannot read property 'imports' of undefined
      at Object.<anonymous> (/home/amal/work/truffle/packages/compile-common/src/profiler/requiredSources.ts:128:45)
      at Generator.next (<anonymous>)
      at fulfilled (/home/amal/work/truffle/packages/compile-common/dist/src/profiler/requiredSources.js:5:58)

The debug trace shows getImports behaving differently, with [__Truffle__NotFound.sol] on failing Arch, and [] for OS X.

Arch

2021-02-21T02:20:04.842Z compile-common:profiler:resolveAllSources resolveAllSources called
2021-02-21T02:20:04.848Z compile-common:profiler:getImports filePath: /home/amal/dev/txlog/reentrant/contracts/Migrations.sol
2021-02-21T02:20:04.859Z compile-common:profiler:getImports imports: [ '__Truffle__NotFound.sol' ]

OS X

2021-02-21T02:20:48.526Z compile-common:profiler:resolveAllSources resolveAllSources called
2021-02-21T02:20:48.536Z compile-common:profiler:getImports filePath: /Users/cds/dev/txlog/reentrant/contracts/Migrations.sol
2021-02-21T02:20:48.558Z compile-common:profiler:getImports imports: []
haltman-at commented 3 years ago

OK, I'm going to close this for now then as it appears to be fixed in 5.1.67. If there's still a problem here we can reoepn this.

elranu commented 3 years ago

Hi @haltman-at , I still have the problem on version 5.1.67. I have created a repo with the code to reproduce it: https://github.com/elranu/truffle-compile-error

PS C:\Dev\repos\gsn-demo2> truffle compile

Compiling your contracts...
===========================
TypeError: Cannot read property 'imports' of undefined
    at Object.<anonymous> (C:\Users\mvica\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\compile-common\dist\src\profiler\requiredSources.js:98:1)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\mvica\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\compile-common\dist\src\profiler\requiredSources.js:5:42)
Truffle v5.1.67 (core: 5.1.67)
Node v14.15.1
haltman-at commented 3 years ago

OK, thanks, I'll check it out!

haltman-at commented 3 years ago

OK, I still can't reproduce this. :-/ I had to fix up the example a bit to make it compile -- turning up the version to 0.6.9, fixing the BaseRelayRecipient import in MetaCoin, installing @openzeppelin/contracts and marking MetaCoin as abstract -- but once I did it compiled just fine. I'm not sure what's going on here.

elranu commented 3 years ago

@haltman-at do not fix it. Just clone, yarn, and truffle compile. I know it does not compile. But instead of getting a compilation error, I got the message I sent you before.

RedCuckoo commented 3 years ago

@haltman-at do not fix it. Just clone, yarn, and truffle compile. I know it does not compile. But instead of getting a compilation error, I got the message I sent you before.

When I did what you described I still can't compile it because of the import errors (openzeppelin) and solc version, and I am getting compilations errors, but not the one you have sent.

Could you, please, provide minimal reproducible setup to check?

elranu commented 3 years ago

@RedCuckoo I just test it again. And probably is something of my environment. The strange thing is with older versions works fine. My environment is windows 10, npm install truffle -g, Truffle v5.1.67 (core: 5.1.67). Node v14.15.1. Really I don´t know what else I can add. Sorry.

haltman-at commented 3 years ago

@elranu Ah, I see, I did not get that error when compiling it without fixing it; I got ordinary compilation errors. So, I was unable to reproduce it that way either. I can try seeing if the version of Node is relevant, I suppose. If it's a Windows-specific problem that will be harder to test.

haltman-at commented 3 years ago

OK, using Node 14.15.1 didn't change anything on my end, I'm afraid. @elranu, just to be sure, are you absolutely certain that you properly uninstalled Truffle 5.1.66 and installed 5.1.67? (As always, we recommend upgrading via

npm uninstall -g truffle
npm install -g truffle

to prevent any problems.) Or are you sure that you don't possibly have two different versions of Truffle installed, say one globally and one locally? I realize this may be a stupid question, but I want to be certain here...

elranu commented 3 years ago

Hi @haltman-at. Thank you very much for your response. You can check in the repo I sent you, which does not have package.json with a local version. So I don't think it may be that.

Before I was working with version Truffle v5.1.56 and I have upgraded to 5.1.67. That´s the only difference I see. As you quoted I have use installed and uninstalled. What I did not do, is npm clean cache and restart the computer. I´ll do it soon, sorry. I have a deadline. I´ll let you know.

Thank you very much for all the help. You are amazing guys!

Regards, Mariano Vicario

stebou commented 3 years ago

Compiling your contracts...

TypeError: Cannot read property 'imports' of undefined at Object. (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:98:1) at Generator.next () at fulfilled (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:5:42) Truffle v5.2.1 (core: 5.2.0) Node v15.10.0

Hello, i have the same problem with a recent version of truffle .

francovilla commented 3 years ago

Same error Truffle v5.2.2 Solidity 0.6.12 Node v14.15.1 Web3.js v1.2.9

vladjdk commented 3 years ago

Getting the same error when running truffle test:

TypeError: Cannot read property 'imports' of undefined
    at Object.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:98:1)
    at Generator.next (<anonymous>)
    at fulfilled (/usr/local/lib/node_modules/truffle/build/webpack:/packages/compile-common/dist/src/profiler/requiredSources.js:5:42)
Truffle v5.2.3 (core: 5.2.3)
Node v15.4.0

This is on MacOS 11.2.2.

You can reproduce it by following the steps here up until you finish testAdoption.test.js and run truffle test for the first time.

Edit: This was actually fixed by changing pragma solidity ^0.5.16; to pragma solidity >=0.5.16; in all the Solidity files and adding

    solc: {
      version: "^0.5.16"
    }
  }```

instead of 

```compilers: { //I added this parameter.
    solc: {
      version: "0.5.16"
    }
  }

in the truffle-config.js file.

haltman-at commented 3 years ago

@photonized Hm, so it's not Windows-specific, then.

I don't think the change to the config file is relevant, nor the particular pragmas. I think the real reason that making that change caused the problem to go away is that, by updating every source file, you caused the profiler to attempt to compile everything; and we know the problem doesn't occur when compiling everything.

But, I'll see if I can reproduce the problem with your example. I've had no luck with any of the others, but maybe this time will be different...

haltman-at commented 3 years ago

@photonized Sorry, did you leave something out? It seems like you intended to include reproduction steps, but there aren't any there. Was your "here" supposed to be a link? Thank you!

vladjdk commented 3 years ago

Oops, @haltman-at . By here, I meant https://www.trufflesuite.com/tutorials/pet-shop

haltman-at commented 3 years ago

OK, I'll have to try that out later, thanks!

haltman-at commented 3 years ago

OK, unfortunately I was still unable to reproduce the problem in this fashion. Sorry.

RobertoDonPedro commented 3 years ago

FYI, I had the same problem, I fixed it by using sudo to uninstalled the old truffle version and to install the latest truffle version V5.2.3 as follows:

sudo npm uninstall -g truffle
sudo npm install truffle -g
truffle compile

Compiling your contracts...
===========================
.....
> Compiled successfully using:
   - solc: 0.6.12+commit.27d51765.Emscripten.clang

truffle version

Truffle v5.2.3 (core: 5.2.3)
Solidity - 0.6.12 (solc-js)
Node v12.21.0
Web3.js v1.2.9