Skip to content
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

main: new global option -chdir #26087

Merged
merged 1 commit into from
Sep 4, 2020
Merged

main: new global option -chdir #26087

merged 1 commit into from
Sep 4, 2020

Conversation

apparentlymart
Copy link
Contributor

@apparentlymart apparentlymart commented Sep 1, 2020

This new option is intended to address the previous inconsistencies where some older subcommands supported partially changing the target directory (where Terraform would use the new directory inconsistently) where newer commands did not support that override at all.

Instead, now Terraform will accept a -chdir command at the start of the command line (before the subcommand) and will interpret it as a request to direct all actions that would normally be taken in the current working directory into the target directory instead. This is similar to options offered by some other similar tools, such as the -C option in make.

terraform -chdir=some/subdirectory apply

The new option is only accepted at the start of the command line (before the subcommand) as a way to reflect that it is a global command (not specific to a particular subcommand) and that it takes effect before executing the subcommand. This also means it'll be forced to appear before any other command-specific arguments that take file paths, which hopefully communicates that those other arguments are interpreted relative to the overridden path.

As a measure of pragmatism for existing uses, the path.cwd object in the Terraform language will continue to return the original working directory (ignoring -chdir), in case that is important in some exceptional workflows. The path.root object gives the root module directory, which will always match the overridden working directory unless the user simultaneously uses one of the legacy directory override arguments, which is not a pattern we intend to support in the long run.

As a first step down the deprecation path, this commit adjusts the documentation to de-emphasize the inconsistent old command line arguments, including specific guidance on what to use instead for the main three workflow commands, but all of those options remain supported in the same way as they were before. In a later commit we'll make those arguments produce a visible deprecation warning in Terraform's output, and then in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the one containing the root module configuration.


This will not be merged at least until the main branch opens for 0.14 development. Even then, this is just a first draft and is likely to see some tweaks as we try out some different permutations and see how less common cases are behaving under this new model.

@codecov
Copy link

codecov bot commented Sep 1, 2020

Codecov Report

Merging #26087 into master will decrease coverage by 0.02%.
The diff coverage is 46.15%.

Impacted Files Coverage Δ
commands.go 1.02% <0.00%> (-0.01%) ⬇️
terraform/context.go 86.46% <ø> (ø)
terraform/evaluate.go 52.33% <33.33%> (-0.73%) ⬇️
main.go 34.70% <41.02%> (+0.64%) ⬆️
command/meta.go 69.19% <100.00%> (+0.13%) ⬆️
help.go 78.00% <100.00%> (+3.00%) ⬆️
configs/hcl2shim/values.go 47.95% <0.00%> (-34.50%) ⬇️
terraform/resource.go 47.14% <0.00%> (-3.63%) ⬇️
terraform/node_resource_plan.go 91.80% <0.00%> (-1.64%) ⬇️
backend/remote-state/http/backend.go 82.17% <0.00%> (-0.67%) ⬇️
... and 7 more

@apparentlymart apparentlymart marked this pull request as ready for review September 4, 2020 16:15
@apparentlymart apparentlymart requested a review from a team September 4, 2020 16:15
website/docs/commands/plan.html.markdown Outdated Show resolved Hide resolved
Copy link
Contributor

@mildwonkey mildwonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 🎉 🎉 Yay!

This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.

Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".

The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.

As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.

As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
@ghost
Copy link

ghost commented Oct 13, 2020

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.

@ghost ghost locked as resolved and limited conversation to collaborators Oct 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants