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.

Support imports in solcjs CLI

See original GitHub issue

Given the following directory structure:

contracts/
  Source.sol
  Imported.sol

Where Source.sol:

pragma solidity ^0.4.0;

import './Imported.sol';

contract Source {
}

Running the following command:

solcjs --optimize --bin contracts/Source.sol

Based on the docs, I would expect the compiler to resolve the import but instead it throws:

contracts/Source.sol:3:1: Error: Source "contracts/Imported.sol" not found: File not supplied initially.
import './Imported.sol';
^---------------------^

If I include Imported.sol as a source file it will compile without error:

solcjs --optimize --bin contracts/Source.sol contracts/Imported.sol

Could you clarify my understanding here or is this a bug? Thanks! 🙃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

18reactions
arashkianicommented, Mar 8, 2018

so writing import is just a fun activity that solc dont understand ?

5reactions
Suhailcommented, Jan 2, 2018

I solved this by doing the below as things have been improved as of 0.2.1:

var solc = require('solc');
var fs = require('fs');

var inputs = {
    'auction.sol': fs.readFileSync('auction.sol').toString(),
};

// Assumes imported files are in the same folder/local path
function findImports(path) {
    return {
        'contents': fs.readFileSync(path).toString()
    }
}

var compiledCode = solc.compile({sources: inputs}, 1, findImports)

fs.writeFile('compiled.json', JSON.stringify(compiledCode), function(err) {
    if (err) throw err;
    console.log('Compiled & saved');
});

I then read in compiled.json later in a .js file using web3 to use it via a browser using browserify:

compiledCode =  JSON.parse(fs.readFileSync('compiled.json').toString());

//console.log(compiledCode);

abi = JSON.parse(compiledCode.contracts['auction.sol:ContractName'].interface);
bytecode = compiledCode.contracts['auction.sol:ContractName'].bytecode;

Hope this is helpful to someone else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the Compiler — Solidity 0.8.17 documentation
Using solc --help provides you with an explanation of all options. ... The commandline compiler will automatically read imported files from the filesystem, ......
Read more >
solc-js - npm
To see all the supported features, execute: solcjs --help. Note: this commandline interface is not compatible with solc provided by the ...
Read more >
Solidity compiler command-line utility (solcjs) various problems
First of all, I need to supply in the command-line all the files that are imported by the primary file. Otherwise I get...
Read more >
File import callback not supported on MacOS with solcjs
Hi @spizzy,. I am sorry you are having this issue. I believe we only saw this previously with the OpenZeppelin CLI.
Read more >
Web3J | Source "@openzeppelin/contracts/token/ERC721 ...
Related. 1 · I need help to build ERC-721 contract with solidity version 5.0 or higher · 1 · Error compiling OpenZeppelin imported...
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