Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove import console in Script.sol #104

Closed
tonypig opened this issue Jul 4, 2022 · 9 comments
Closed

remove import console in Script.sol #104

tonypig opened this issue Jul 4, 2022 · 9 comments

Comments

@tonypig
Copy link

tonypig commented Jul 4, 2022

Hi guys,

in the Script.sol, console and console2 were imported:

import "./console.sol";
import "./console2.sol";

but then if I want use console.log in the contract, I need use alias like:

import {console as clg} "forge-std/console.sol"

otherwise there would be DeclarationError (because Test.sol import Script).

I don't know why the console imported in Script.sol, and Test.sol not use that.

So I wonder if it's possible to be more autonomous, Thanks for your time

@ZeroEkkusu
Copy link
Collaborator

ZeroEkkusu commented Jul 4, 2022

Gm, @tonypig.

There should be no issues with importing/using console and console2. Can you show me an example setup?

The only case I can think of when you would get a DeclarationError is when you try to import console from console2.sol and vice-versa.

@tonypig
Copy link
Author

tonypig commented Jul 5, 2022

Hi @ZeroEkkusu

My scenario is simple:

two contract:
ContractA.sol and ContractA.t.sol (test)

While import console in ContractA.sol, then forge build/test will throw an DeclarationError

that because forge-std/Test.sol also import the console (through Script.sol)

@ZeroEkkusu
Copy link
Collaborator

ZeroEkkusu commented Jul 5, 2022

@tonypig, like so? I can't reproduce the error.

ContractA.sol

import "forge-std/console.sol";
import "forge-std/console2.sol";

contract ContractA {
    function f() external {
        console.log("gm with console");
        console2.log("gm with console2");
    }
}

ContractA.t.sol

import "forge-std/Test.sol";

import "src/ContractA.sol";

contract ContractATest is Test {

    ContractA cA;

    function setUp() public {
        cA = new ContractA();
    }

    function testExample() public {
        cA.f();
        console.log("test");
        console2.log("test 2");
    }
}

@tonypig
Copy link
Author

tonypig commented Jul 5, 2022

Hi @ZeroEkkusu

Sorry, I just found the real problem.

that because forge is not case sensitive, and I import the console file withs "Console.sol" in ContactA.sol, then the error reproduce.

Maybe it’s not a big problem, thanks for your time

@ranmocy
Copy link

ranmocy commented Sep 20, 2022

I think this still could be an issue for users who use Hardhat console in their production contracts, and use Forge Test in their test contracts.

import "hardhat/console.sol";

contract MyContract {}
import "forge-std/Test.sol";
import "contracts/MyContract.sol";

contract MyContractTest is Test {}

Compile it will throw error[2333]: DeclarationError: Identifier already declared.

Is there a way to import the library so that it only affects current file and it's child-contracts, but not sibling contracts in the inheritance chain?

@ZeroEkkusu
Copy link
Collaborator

ZeroEkkusu commented Sep 21, 2022

@ranmocy, import console from forge-std instead of hardhat in your production contracts:

import "forge-std/console.sol";

Another option is to use selective imports to import only MyContract in your test contracts:

import {MyContract} from "contracts/MyContract.sol";

@heyJonBray
Copy link

I also cannot get console2 to work on a particular project that I built from scratch. I've ran multiple repos locally that have had it implemented already, and have configured my repo in the same way as the ones that work.

foundry.toml remappings

remappings = [
    "forge-std/=lib/forge-std/src/",
    "@openzeppelin/=lib/openzeppelin-contracts/",
    "@uniswap/v2-core/=lib/v2-core/",
    "@uniswap/v3-core/=lib/v3-core/",
    "@uniswap/v3-periphery/=lib/v3-periphery/"
]

having trouble importing in both ContractTest.t.sol and ContractScript.s.sol files:

// have tried
import "forge-std/console2.sol";

// as well as
import {console2} from "forge-std/console2.sol";

// as well as
import {Script} from "forge-std/Script.sol";
import {console2} from "forge-std/console2.sol";
import {MyContract} from "../src/MyContract.sol";

getting this error on forge build:

Error (7576): Undeclared identifier. Did you mean "console"?
script/PriceOracle.s.sol:19:9: DeclarationError: Undeclared identifier. Did you mean "console"?
        console2.log("initializing price oracle...");
        ^------^

@heyJonBray
Copy link

I think the issue I was running into had to do with Solidity and my PriceOracle.sol contract being version ^0.7.6

was able to resolve the error using pragma abicoder v2 on the script/test contracts for PriceOracle

@ZeroEkkusu
Copy link
Collaborator

Btw, no need to use console2; it's only there for legacy support. You can just use console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants