-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
feat: support environment variables in foundry.toml
#1034
Comments
It should be the case that all things in Edit: Ah, you want to override the environment variable used? |
Yea I want someone to be able to clone a repo, configure a |
+1 not having to change tracked files makes for better DX |
Still unsure about this, I don't know of any tools that allow this other than DappTools because they used a full bash script for configuration, and I don't think there's a clean way to do this in TOML either. Any reason you don't just do the renaming in your environment script that we could autoload instead? |
You could argue hardhat does allow for this: In
Though to your point I don't think there's a way to do this natively in TOML files. I think the easiest way is to define a custom syntax like
Sorry, not sure I follow what environment script forge would autoload. Do you mean e.g. using a makefile to define test commands with env vars and running |
I mean if Forge auto-loaded |
Ahh interesting! Let me make sure I'm understanding: Since every param in I'd say that's an acceptable short term solution, with the reasons I don't love it being that:
|
Yes, that's what I mean. I think the main reason I don't like the env vars in |
Makes sense. Maybe a good solution to mitigate items 1 and 2 is adding a field to the config file called something like Not sure whether it'd be better to specify the env var names specifically there, or the config file field names which foundry would convert to the env var format |
Why not use something like Justfile? eg https://github.com/sambacha/foundry-scripts/blob/master/justfile This loads env file, etc |
Here's one idea:
Example: [default]
src = 'src'
test = 'test'
eth-rpc-url = 'https://mainnet.infura.io/v3/env("INFURA_API")'
[rinkeby]
eth-rpc-url = 'https://rinkeby.infura.io/v3/env("INFURA_API")' Edge case 1Hardhat will error if no env var is present (because |
Personally I like that approach, though I know above @onbjerg mentioned he didn't love the idea of non-standard TOML, but I do think it provides the best and simplest UX. If you specify an env var in your config but it's not present, we should probably error. I suspect most cases (such as fork URLs and API keys) would end up erroring anyway, or resulting in unexpected behavior, if you tried to fall back to some default value. |
@onbjerg curious for any strong opinions re: my comment above? |
I think if we want inline env vars I'd rather go with something standard like |
I'm 👍 with calling it |
This comment might be relevant: #1457 (comment) |
Quoting from my #1457 (comment) comment an enhancement to your suggestion (that's the same thing I wanted to implement on the other thread) would be to also allow the developer to have different
If you specify a profile when running a foundry script, foundry will load the Would this make sense? Side question: how can I specify the {
"scripts": {
"test": "FOUNDRY_PROFILE=rinkeby forge test"
}
} |
I think I prefer the approach mentioned above with
Yep. |
Maybe I didn't understand it, how can I pass locally those env vars without the support of a |
It depends on your shell, but in bash you would prepend the variables like so:
Alternatively you can source your source .env
./my-program |
Ok, so you can't make them to have the same |
I'm not entirely sure what you mean - it is definitely possible to run |
Only for RPC endpoints in the |
Where else do you want it @mds1 ? |
Mainly anywhere you might have API keys, which would also include Related to this—and maybe this is a foundry book issue—I'm unclear on the recommend way to run a portion of tests as fork tests within your test suite. Right now we use |
Tradeoff is that it's easier to figure out what environment variables to set if you use |
They're basically identical, and I prefer the latter to the former because you put all your env vars in the foundry toml and then refer only by aliases in the code. We could also allow |
Heh thought that might be the answer. IMO best practice is to have a committed But the
Etherscan API keys differ by network, so a single Perhaps we want an |
that would be useful indeed, basically the same thing as the rpc_endpoint table. tracking this separately. |
closing this, as .env file is now loaded and we support env vars in [etherscan] and [rpc_endpoints] if we need to support env vars anywhere else, let's track that individually |
To run tests, we have to do `forge test --fork-url <rpc url>`. That works but is not super convenient. Foundry recently added support for auto-sourcing .env. See: foundry-rs/foundry#1034
To run tests, we have to do `forge test --fork-url <rpc url>`. That works but is not super convenient. Recently, Foundry added support for auto-sourcing .env. See: foundry-rs/foundry#1034
To run tests, we have to do `forge test --fork-url <rpc url>`. That works but is not super convenient. Recently, Foundry added support for auto-sourcing .env. See: foundry-rs/foundry#1034
* Setup `BaseSetup.t.sol` to create fork from `RPC_URL` To run tests, we have to do `forge test --fork-url <rpc url>`. That works but is not super convenient. Recently, Foundry added support for auto-sourcing .env. See: foundry-rs/foundry#1034 * Update README Updates the cli command and uses correct env var name. * Add workflow to see if `forge fmt` was run * Run `forge fmt` * Update env variable * Remove boolean eq checks * Add .vscode to gitignore file Personal config files should not be committed. It doesn't seem right for me to commit my personal vscode extension settings which don't make sense for everyone. .vscode should ideally be in global gitignore.
Component
Forge
Describe the feature you would like
A common use case is configuring test params with environment variables, with the RPC URL used for forking tests being a popular example. Right now there is now way to specify in the config file to use a certain environment variable for fork URLs, and instead you'd have to pass this in to the test command, e.g.
forge test --fork-url $MY_RPC_URL
Additional context
No response
The text was updated successfully, but these errors were encountered: