foundry icon indicating copy to clipboard operation
foundry copied to clipboard

regression in handling of .env

Open sakulstra opened this issue 1 year ago • 4 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (cafc260 2024-05-02T00:22:11.188280000Z)

What command(s) is the bug in?

forge script/test/etc

Operating System

macOS (Apple Silicon)

Describe the bug

Users of our tooling reported a lot of weird issues lately and i think i finally found the culprit which seems to be related to some change in how .env is handled.

In most our projects we seed some empty .env with things like SENDER= Up until recently https://github.com/foundry-rs/foundry/issues/3847#issuecomment-1342616050 this was the behavior in foundry: .env never overwrites global .env. It seems like in a recent nightly the behavior changed and now not only:

  • .env suddenly has precedence over user env but also
  • empty values in .env have precedence over user env

So if you e.g. define a:

# intentionally left empty
MNEMONIC_INDEX=
LEDGER_SENDER=

it will overwrite your MNEMONIC_INDEX you might have defined in global env and leads to weird issues like a value is required for '--mnemonic-indexes <INDEXES>' but none was supplied

sakulstra avatar May 02 '24 13:05 sakulstra

what is "global .env"?

the load .env call that's being used does not replace existing vars

mattsse avatar May 02 '24 18:05 mattsse

@mattsse sorry typo. Wanted to write global env - meaning "environment variables that were already set" (outside of .env)

sakulstra avatar May 03 '24 07:05 sakulstra

hmm this is odd

Loads environment variables from the specified path. If variables with the same names already exist in the environment, then their values will be preserved. Where multiple declarations for the same environment variable exist in your .env file, the first one is applied.

https://docs.rs/dotenvy/latest/dotenvy/fn.from_path.html

Is there a way to reproduce this?

mattsse avatar May 03 '24 11:05 mattsse

sure, so just do: export RPC_MAINNET=someWorkingRPC on any repo with mainnet tests. When you now run forge run test it will work.

Then put RPC_MAINNET= in your .env and do the same. Now it will fail with [FAIL. Reason: setup failed: invalid rpc url: ]

sakulstra avatar May 04 '24 07:05 sakulstra

Unable to reproduce this with the given setup

Setup:

In foundry.toml

[rpc_endpoints]
mainnet = "${RPC_MAINNET}"

With test

contract CounterTest is Test {
    Counter public counter;

    function setUp() public {
        vm.createSelectFork("mainnet");
        counter = new Counter();
        counter.setNumber(0);
    }

    function test_Increment() public {
        counter.increment();
        assertEq(counter.number(), 1);
    }

    function testFuzz_SetNumber(uint256 x) public {
        counter.setNumber(x);
        assertEq(counter.number(), x);
    }
}

Running:

export RPC_MAINNET=<VALID_ALCHEMY_API_KEY>

Defining in .env:

RPC_MAINNET=

Does not yield any errors. In all cases the global environment variable overrides the value defined in .env.

Marking as can't repro, feel free to re-open with a minimal reproduction that shows this behavior

zerosnacks avatar Jul 15 '24 13:07 zerosnacks