Skip to content

Conversation

@preminger
Copy link

Details

What

This PR adds a -dryrun flag to Mage. When dryrun mode is active, Mage will print the commands it would have executed (if not for dryrun mode).

Why

While -v and -debug already exist, they both still result in the actual commands being executed; in some situations, one may want to verify the correctness of one's magefile by just seeing what would be running.

How

The main "wrinkle" as far as implementation goes is the fact that the same lines of code in mage/main.go are used to compile the magefile into a binary as are used within that binary to execute other commands. (While there are other lines used to execute commands in Mage - notably, in the sh package - those don't have this "dual issue" property.)

As a result, an indiscriminate dryrun mode would simply echo the magefile-compilation command lines, never compile the magefile into an actual binary, and thus never print the "actual" command lines (from the user's perspective) that the magefile should execute - nullifying any usefulness of the feature.

To resolve this problem, the implementation uses the conjunction (i.e., logical AND) of two elements to determine dryrun. One is, well, whether -dryrun has been specified; but the other is whether the environment variable MAGEFILE_DRYRUN_POSSIBLE has been set. In the "outer" call to Mage, the latter variable will be unset, so that even if dryrun mode was requested, the result of the logical AND will be false. But, as part of this outer run, Mage sets this environment variable; meaning in any "inner" runs, the result of the logical AND will depend purely on whether the -dryrun flag or its corresponding MAGEFILE_DRYRUN environment variable (set in RunCompiled(...), just like the other Mage-related environment variables) are in effect.

The net result is that -dryrun has no effect on the magefile compilation, but works as desired on the command-lines issued by the compiled magefile.

GitHub Issue

Merging this PR would close issue #532

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

Successfully merging this pull request may close these issues.

1 participant