-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Ability to detect different "environments" in scripts/tests #2900
Comments
What foundry version are you on? Reading env vars works as expected for me with the latest nightly,
This isn't natively supported, but you can work around with a command runner/script, e.g. if
Remove the Alternative, put the env var in a
add
I'm not sure I understand the use case here / what you're trying to do 😅 |
Ok, I don't know when I started using the I still think it would be great to be able to detect when |
I'd like to resurrect this issue, being able to detect '--broadcast' can be useful for choosing whether to write to a file or not. |
@mds1 I'd like to chime and say detecting whether If we take a step back and look at the big picture, we write scripts in foundry to automate some processes on the blockchain. As part of these processes we might have local side effects. For example, writing out the addresses of deployed contracts to a file. If I would say there needs to be a "holistic" way of determining a simulation mode and scripts be aware of it. Presence of the As an aside, my team is using the upgrade scripts written by @0xPhaze , and the very first thing we ran into was this issue. See 0xPhaze/upgrade-scripts#1 . |
This seems reasonable to me, I can see the use case. There's currently no script-only cheat codes so maybe setting an env var is the way to go? Is there value to generalizing this so this feature (whether a cheat code, env var that's set, etc.) reports the overall state, including:
I also don't want to overcomplicate it, so maybe just something like
Can you expand on what you mean here, not sure I follow? One last comment is there's also #3298, which is interesting—if you can inject arbitrary output data into the broadcast logs, is there still a use case for writing your own custom output files? The dry runs are automatically nested into a dry-run folder that's gitignore'd, so maybe #3298 removes the need for this feature? |
If we think about the same situation of "run script to deploy contracts, and write results to a file". @0xPhaze mentioned this at the top:
If This can be "worked around" by wrapping calls in a shell script or something else, but I think that introduces extra complexity on the usability of the tool. Regarding #3298, I'll defer to @0xPhaze on the specifics, but my general feeling is that that is a "low-level lever". It gets the information written out somewhere, but may need further processing outside of forge to be useful. I.e. some other script or program that parses those outputs and creates the final "report" or script "output". IMHO in the longer term, if we want an ecosystem of tooling built with/around foundry, we need some higher-level solutions as well, otherwise we can have too many "low-level levers" that everyone has to make work together in their own way. Sorry if that's super vague - I think the those features are great, but there needs to be higher-level strategy to grow the ecosystem to go with them. |
I did think that it could be useful to detect the difference in "mode" between test/script, but there are some workarounds to achieve this. Nonetheless, I think having a I also saw #3298 and thought it was very interesting. I can imagine this would be useful along with The custom output files are there to keep an up-to-date list of all deployments. Say 3/4 contracts haven't changed, and only one needs to be re-deployed, I imagine this would be hard to keep track of over multiple files even with #3298. |
It sounds like we can summarize this issues feature request as adding:
bytes mode = vm.envBytes32("FOUNDRY_MODE");
if (mode == stdMode.Test) doSomething();
else if (mode == stdMode.ScriptDryRun) doSomethingElse();
else if (mode == stdMode.ScriptBroadcast) doAnotherThing(); @onbjerg @mattsse lmk what you think about this There's a few other things discussed above, but @0xPhaze @KholdStare I'd suggest pulling them into separate issues to continue discussion to avoid mixing things here |
I can see how this would be useful. what if we introduce something like enum ForgeContext {
Test,ScriptDryRun,ScriptBroadcast
} and add cheatcodes |
That works for me, I think we'd also want |
@mattsse Any update on this? Was also wondering if it should be more of a "bitmask" of enabled features rather than an enum. I.e. Could have |
This one is not yet closable, the idea is a cheatcode to let you determine what environment you're code is running in, such as: if (vm.forgeContext() == ForgeContext.Script) {
console.log("in a script");
} else if (vm.forgeContext() == ForgeContext.Test) {
console.log("in a test");
} else {
console.log("other");
} We do need some thought on UX here before implementing, e.g. returning an array of contexts, or a bitmask as suggested by @KholdStare above, would probably offer better UX. In these cases we'd probably also want helper cheats, such as: // Vm.sol
enum ForgeContext {
Test,
Script,
Coverage,
Snapshot,
DryRun,
Broadcast
}
function forgeContext() external returns (ForgeContext[] memory contexts);
function isTestContext() external returns (bool isTest);
function isScriptContext() external returns (bool isScript);
// etc. #4844 is part of the way there, but a different goal. Not yet sure if they should be considered independent or if we should try to merge the two. Initial reaction is that they should be independent |
Component
Forge
Describe the feature you would like
I'm working on a more complex setup that includes a bunch of
ffi
sanity checks and post-deploy scripts. It would be great if I could somehow be able to detect when--broadcast
is enabled so that I see the current script run as a "dry-run" and not actually overwrite my current deployments.Other use-cases include disabling a bunch of these sanity checks (which take up a lot of extra time) by passing in some user-argument.
There actually seems to be no easy way to discern different "modes", like (e.g. dry-run, or other user-defined parameters).
Running
DRY_RUN=true && forge script deploy
doesn't let me readDRY_RUN
throughvm.getEnv
, because it's not in the default.env
file. The closest I've gotten is to being able to detect--ffi
through atry vm.ffi() {} catch {}
, which somewhat works for my use-case for now, by always enforcing it to be set to true when broadcasting.Some things that would be helpful for me include:
--broadcast
DRY_RUN=true && forge script deploy
--env .env-test
FOUNDRY_PROFILE=mainnet-dry-run && forge script deploy
)I think most helpful and straightforward would be nr2 or nr3.
Just curious if anyone else has found the need for something like this.
Additional context
Edit:
Decided to add some additional context, because I'm not sure it's really clear what I'm asking for.
Below is my setup, and I'm basically using (abusing)
--ffi
as a way to determineDRY_RUN=true
, since this has to be enabled anyway to run some of my sanity checks.The text was updated successfully, but these errors were encountered: