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.

Compiling a contract with `import_remappings` fails

See original GitHub issue
  • py-solc Version: 2.1.0
  • solc Version: 0.4.17
  • Python Version: 3.5.2
  • OS: Ubuntu 16.04, 4.4.0-104-generic

What was wrong?

I tried to compile a contract, say like this one:

pragma solidity 0.4.17;

contract Blah {
    uint public a;

    function Blah(uint _a) public {
        a = _a;
    }

    function getA() public view returns(uint) {
        return a;
    }
}

The python compilation code is as follows:

class Chain:
    ...
    @classmethod
    def compileContract(cls, filename):
	with open(filename,'r') as f:
            contract_source_code = f.read()
        compiled_sol = compile_source(contract_source_code, import_remappings=['='])                                                    
        return compiled_sol ['<stdin>:Blah']
    ...

Executing this function returns an error:

Traceback (most recent call last):
  File "compile.py", line 71, in <module>
    compiled = Chain.compileContract(sys.argv[1])
  File "compile.py", line 24, in compileContract
    compiled_sol = compile_source(contract_source_code, import_remappings=["="])
  File "/usr/local/lib/python3.5/dist-packages/solc/main.py", line 106, in compile_source
    stdoutdata, stderrdata, command, proc = solc_wrapper(**compiler_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/solc/utils/string.py", line 85, in inner
    return force_obj_to_text(fn(*args, **kwargs))
  File "/usr/local/lib/python3.5/dist-packages/solc/wrapper.py", line 165, in solc_wrapper
    stderr_data=stderrdata,
solc.exceptions.SolcError: An error occurred during execution
> command: `solc --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,userdoc =`
> return code: `1`
> stderr:

> stdout:

Notes:

  • It fails whatever value I assign to import_remapings.
  • Executing with import_remappings removed works fine (it’s not needed in this example, but I have another case where I’m importing other contracts and I need to use it).
  • Executing solc --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,userdoc = blah.sol directly from the command line works fine too (no errors, no warnings).
  • I tried also running with solc 0.4.19 in docker but problem persisted.
  • All files I have in one directory and all commands were executed inside this directory.

Cute Animal Picture

tumblr_mbyr4z1nm41rzey51o1_500

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
saaperezrucommented, Feb 12, 2018

For anyone else facing this issue, a simple solution is to send an extra dash at the end of the command:

compiled_sol = compile_source(contract_source_code, import_remappings=['=/', '-'])

The problem seems to be related to the way the solc compiler cli behaves when sending the source code via stdin AND sending the remapping arguments. The following command does not work if you try to send the source code via stdin (it returns immediately without waiting for input):

$ solc --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,userdoc =/

However the following DOES work:

$ solc --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,userdoc =/ -

So the simplest solution is to send an extra dash at the end of the command, and the import_remappings arguments provides an easy way to do so 😃

1reaction
karthika-glancecommented, Nov 17, 2018

How does the above change apply when I do compile_files(…)

Read more comments on GitHub >

github_iconTop Results From Across the Web

solidity - Import errors when compiling contracts using truffle
Just like in the note i show above you should use global map and then setup remapping to it they explain it here:...
Read more >
Target location for compiling imported library #1014 - GitHub
json I'm installing @openzeppelin/contracts with pragma 0.7, when trying to compile ContractC it is failing as both imports read from the same target....
Read more >
Compiling a Contract — Vyper documentation - Read the Docs
Compiling for the wrong EVM version can result in wrong, strange and failing behaviour.
Read more >
not found utils/context.sol when i import from openzepplin
Easiest solution in this context (where no other contract imports the Context.sol ) is to copy-paste the Context.sol to a reachable location ...
Read more >
Import Path Resolution — Solidity 0.8.17 documentation
When you compile a file using the command-line interface of the compiler, you provide one or more paths to files containing Solidity code:...
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