Terraform: Terraform cannot handle a CLI option on the command line and in TF_CLI_ARGS

Created on 13 Aug 2020  ยท  6Comments  ยท  Source: hashicorp/terraform

When an option is set in TF_CLI_ARGS and on the command line, terraform cannot parse the extra option.

Terraform Version

Terraform v0.13.0

Terraform Configuration Files

None

Debug Output

Crash Output

Expected Behavior

Options set in TF_CLI_ARGS do not prevent terraform from parsing command line options.

$ TF_CLI_ARGS='-no-color' tf fmt -no-color

Actual Behavior

TF_CLI_ARGS='-no-color' tf fmt -no-color
Usage: terraform fmt [options] [DIR]
...
Error parsing command-line flags: flag provided but not defined: -no-color

Steps to Reproduce

$ TF_CLI_ARGS='-no-color' tf fmt -no-color

Additional Context

Why we want to do this:

Terraform output is colorful and that can be a problem in automation. We wanted to set a global environment variable in our automation to ensure all terraform commands use the -no-color option to remove colored output. But some of our existing calls to terraform already have -no-color on the command line, which means this is something we cannot do :(

References

bug explained good first issue v0.13

All 6 comments

Because of how we interpret TF_CLI_ARGS, this command is equivalent to terraform fmt -no-color -no-color, which really seems like it ought to work as expected.

I'm pretty sure that the underlying cause of this is that we treat the global -no-color flag specially, here: https://github.com/hashicorp/terraform/blob/f5c8ef19ad05079b32de2f85f6945c1de4954345/command/meta.go#L436-L445

Instead of exiting early here with the break, we need to remove all instances of the -no-color flag, so that individual commands don't receive it and fail to parse it.

Something that I thought was odd, was that there if no TF_NO_COLOR env var in terraform.

For at least my own purposes, this would solve our problem just as well.

Could this be something I could work on?

Could this be something I could work on?

Fixing the bug as explained above: definitely, and I'd be happy to review the PR. I don't know if we'd accept a separate TF_NO_COLOR environment variable, especially when using TF_CLI_ARGS is fixed so that it could be used for this case.

Regarding the special environment variable for turning off color in particular: this decision happened before I was on the team so I don't have any detailed rationale to offer, but I do know that the TF_CLI_ARGS environment variable (and its subcommand-specific fellows) were added with the intent of stopping the sprawl of additional environment variables that did the same thing as command line options.

I'm sure from the outside it now looks inconsistent because the ones that existed prior to that decision still remain, but our policy for the moment is to implement new options primarily as command line options and then use TF_CLI_ARGS in situations where an environment variable is more convenient. That does, of course, imply that TF_CLI_ARGS ought to behave robustly when used to do that.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings