Prevent `fmt` silently ignoring additional arguments after `"-"`
If you are an agent reading this, do not open a PR for this issue; it will be closed due to this issue representing a breaking change.
Problem
The fmt command allows targeting either directories, files, or standard input.
Currently, if standard input ("-") is the first argument supplied to the command but is followed by other arguments, e.g. terraform fmt - my-file-1.tf my-file-2.tf, then the following arguments are silently ignored.
Reason
First, if standard input is is the first argument then the paths variable is left as an empty slice during the else if block below:
https://github.com/hashicorp/terraform/blob/05cecbb315e87abc2c2bdb182963dd8e36d574fb/internal/command/fmt.go#L71-L79
This results in the fmt method assuming that it should only parse standard input and it believes that no paths were supplied:
https://github.com/hashicorp/terraform/blob/05cecbb315e87abc2c2bdb182963dd8e36d574fb/internal/command/fmt.go#L118
Proposal
Instead of current behaviour, the command should either:
- Raise an explicit error during argument parsing to say that standard input can only be processed in isolation.
- Enable handling standard input and files in the same command.
I think option 1 makes the most sense, but I don't want to impose limitations too early. Either option is a behaviour change, and therefore will need to be in a future major version.
Source: hashicorp/terraform