ethereum / solc-js

Javascript bindings for the Solidity compiler
https://soliditylang.org
MIT License
1.45k stars 470 forks source link

Compiling a file errors with :1:1: Error: Expected import directive or contract definition. #90

Closed vincentvanderwalt closed 7 years ago

vincentvanderwalt commented 7 years ago

Afternoon,

I wonder if anyone can help. I'm trying to use solc to compile my contracts. but hitting the following issue ":1:1: Error: Expected import directive or contract definition." The contract compiles successfully in browser-solidity and visual studio code.

as soon as I try and compile using solc the above error is thrown.

Any help appreciated.

Vinny

chriseth commented 7 years ago

Can you provide the exact command you used to compile? The error looks like the compiler got an empty file as input.

vincentvanderwalt commented 7 years ago

let contractsFile = path.join(__dirname, '../contracts/contracts.sol'); var fileContract = fs.readFileSync(contractsFile, { encoding: 'utf-8' }); _**--- fileContract.length = 3532**_ var output = solc.compile(fileContract.toString(), 1); The output shows the following (abbreviated)

{ errors: [ ':1:1: Error: Expected import directive or contract definition.\npragma solidity ^0.4.10; contract

vincentvanderwalt commented 7 years ago

I managed to fix the issue by doing the following.

after that I ran the code in a previous comment and it worked.

Thanks for looking into this @chriseth

Vinny

tiredoak commented 7 years ago

Ran into the same error and tried @vinnytheviking solution but it still didn't work. Are there any other workarounds for this? The code is:

const input = fs.readFileSync('contracts/helloWorld.sol');
console.log(input); // outputs the contract, no issues here
const output = solc.compile(input.toString(), 1);
console.log(output); // returns the error message here
const bytecode = output.contracts['helloWorld'].bytecode;
const abi = JSON.parse(output.contracts['helloWorld'].interface);
Fthi-S3074641 commented 6 years ago

put a semicolon at the end of the directive, see more.

pragma solidity ^0.4.11;
prameshbajra commented 6 years ago

Neither adding semicolon nor changing the character encoding worked for me. Anything else that I could try? It's very irritating. 😠

sidasu92 commented 6 years ago

putting a semicolon it worked for me

Eric-Showers commented 6 years ago

I'm in the same position as @prameshbajra has anyone else found a solution?

Mehathab4eva commented 6 years ago

@prameshbajra @Eric-Showers I had the same issue. I suggest you copy your code and paste it on http://remix.ethereum.org. On this site, the common syntax errors are pointed out. In my case, there was a semicolon, which after removing, I was able to compile the code. Let me know if it works for you. Thank you.

prameshbajra commented 6 years ago

@Mehathab4eva My code works fine in remix. I think it was VS Code + Solc Linter integration that made the error. Not completely sure though.

The error is no more. I don't know how.

I was like one fine morning and all errors were gone 🤣. Poof!!

anistark commented 5 years ago

@tiredoak I've the same code and it works. However, for some contracts, the bytecode is different from what is generated from http://remix.ethereum.org like browser compilers. Thoughts?