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

change suggested apply command after saving plan file to account for -chdir #35305

Open
EugenKon opened this issue Jun 6, 2024 · 4 comments
Open

Comments

@EugenKon
Copy link

EugenKon commented Jun 6, 2024

Terraform Version

Terraform v1.8.5
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v5.51.1
+ provider registry.terraform.io/hashicorp/external v2.3.3
+ provider registry.terraform.io/hashicorp/local v2.5.1
+ provider registry.terraform.io/hashicorp/null v3.2.2
+ provider registry.terraform.io/hashicorp/random v3.6.2
+ provider registry.terraform.io/hashicorp/tls v4.0.5

Terraform Configuration Files

not relevant

Debug Output

image

Expected Behavior

The plan should be run

Actual Behavior

plan was not found

Steps to Reproduce

  1. terraform -chdir=derived-src/aws/ plan -state=../../state.d/terraform.tfstate -replace "module.private-cloud.aws_instance.server[0]" -out repl
  2. terraform apply "repl"

Though the next command works: terraform -chdir=derived-src/aws/ apply -state=../../state.d/terraform.tfstate "repl"

It would be nice to output additional mandatory option for the plan instead of just terraform plan "repl" (see the debug output above).

Or, the better solution, save -chdir and -state options into the plan and save the plan into the current directory.

Additional Context

No response

References

No response

@EugenKon EugenKon added bug new new issue not yet triaged labels Jun 6, 2024
@jbardin jbardin added question working as designed confirmed as reported and closed because the behavior is intended and removed bug new new issue not yet triaged labels Jun 6, 2024
@jbardin
Copy link
Member

jbardin commented Jun 6, 2024

Hi @EugenKon,

You specified that terraform should run in the derived-src/aws/ directory, and since that was the working directory for the process, that is where the repl file will have been saved.

We use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help.

Thanks!

@jbardin jbardin closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2024
@EugenKon
Copy link
Author

EugenKon commented Jun 6, 2024

@jbardin This is not the question, this is the proposition to improve TF output. Here instead of terraform apply "repl" (see the debug output section) it should print terraform -chdir=derived-src/aws/ apply -state=../../state.d/terraform.tfstate "repl", because terraform knows that directory was changed and state is on the different path.

This is misleading to see the hint which does not work.

@jbardin
Copy link
Member

jbardin commented Jun 6, 2024

@EugenKon, thanks I see what you mean now, and we can file this as an enhancement request. Getting that output might be a bit more difficult than it sounds, since terraform has already changed its working directory before the planning process has even begun.

@jbardin jbardin reopened this Jun 6, 2024
@jbardin jbardin changed the title Error: Failed to load "repl" as a plan file change suggested apply command after saving plan file to account for -chdir Jun 6, 2024
@jbardin jbardin added enhancement cli and removed question working as designed confirmed as reported and closed because the behavior is intended labels Jun 6, 2024
@apparentlymart
Copy link
Member

We do currently "remember" the original working directory so that path.cwd can return it instead of the switched working directory (for backward compatibility with how this used to work before we added -chdir):

// OriginalWorkingDir returns the true, operating-system-originated working
// directory that the current Terraform process was launched from.
//
// This is usually the same as the main working directory, but differs in the
// special case where the user ran Terraform with the global -chdir=...
// option. This is here only for a few backward compatibility affordances
// from before we had the -chdir=... option, so should typically not be used
// for anything new.
func (d *Dir) OriginalWorkingDir() string {
return d.originalDir
}

The command.Meta object has a pointer to this workdir.Dir object inside it, and so in principle we could use filepath.Rel to resolve the current working directory relative to the original working directory and, if it turns out to be anything other than just ., add a -chdir=... option to the message using that result as the option's argument.

However, I think this specific message is being printed by the local backend rather than by package command, so we will probably need to arrange for this information to be included in the operation request object we send to the backend so that the information will be available in the right place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants