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

show deprecation warning if -state is used with plan, apply, refresh #35660

Merged
merged 6 commits into from
Sep 5, 2024

Conversation

melsonic
Copy link
Contributor

@melsonic melsonic commented Sep 1, 2024

This PR adds the functionality of showing a deprecation warning when -state is used along with plan, apply, and refresh. So, to add this functionality, I pushed a warning saying state is deprecated in the diags slice if the state field is not nil in all those three files i.e. plan.go, apply.go and refresh.go.

Fixes #35562

Target Release

1.8.x

Draft CHANGELOG entry

  • shows deprecation warning for the -state flag when used with plan, apply, and refresh.

ENHANCEMENTS

  • terraform plan -state=mstate.tfstate shows **Warning**: state is deprecated.
  • terraform refresh -state=mstate.tfstate shows **Warning**: state is deprecated.
  • terraform apply -state=mstate.tfstate shows **Warning**: state is deprecated.

Output

terraform_state_warning

Copy link

hashicorp-cla-app bot commented Sep 1, 2024

CLA assistant check
All committers have signed the CLA.

Comment on lines 59 to 63
if diags.HasWarnings() {
view.Diagnostics(diags)
diags = nil
}

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need this, the warning should still be printed whenever the diagnostics are chosen to be delayed.

Is there something else that is dropping / ignoring the warning diagnostics later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, it is required to print the diagnostics before, since on line number 73 planFile, diags := c.LoadPlanFile(args.PlanPath) returns a new diagnostic slice, it doesn't append to the already available diags variable.

Copy link
Member

Choose a reason for hiding this comment

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

Cool, I think we can change the other part instead of doing this 👍 It's more in keeping with the way the diagnostics are intended to work to just gather them up and report once.

You can change line 73 to something like:

	planFile, moreDiags := c.LoadPlanFile(args.PlanPath)
        diags = diags.Append(moreDiags)
	if moreDiags.HasErrors() {
		view.Diagnostics(diags)
		return 1
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, I think we can change the other part instead of doing this 👍 It's more in keeping with the way the diagnostics are intended to work to just gather them up and report once.

You can change line 73 to something like:

	planFile, moreDiags := c.LoadPlanFile(args.PlanPath)
        diags = diags.Append(moreDiags)
	if moreDiags.HasErrors() {
		view.Diagnostics(diags)
		return 1
	}

Yeah, this can be done.

@@ -43,6 +43,9 @@ func ParseApply(args []string) (*Apply, tfdiags.Diagnostics) {
}

cmdFlags := extendedFlagSet("apply", apply.State, apply.Operation, apply.Vars)
if apply.State != nil {
diags = append(diags, tfdiags.SimpleWarning("state is deprecated"))
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice if this error message could present an alternative instead.

The correct thing to do is update the configuration to point to the chosen state file: #35562 (comment)

Maybe we could make this a proper diagnostic with the detail stating to use the path variable in the local backend?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will update and add a commit. And also, I have found a better way to check if the state flag is specified, will add this also in the next commit.

Copy link
Contributor Author

@melsonic melsonic Sep 3, 2024

Choose a reason for hiding this comment

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

Hi @liamcervante
image
Does this align with your thoughts? I also added the link to the resource for easier navigation.

@crw crw added the enhancement label Sep 3, 2024
Copy link
Member

@liamcervante liamcervante left a comment

Choose a reason for hiding this comment

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

Thanks! Looking good, just some recommendations over the content of the error!

Comment on lines 63 to 64
"state is deprecated",
fmt.Sprintf("use path variable of local backend instead : https://developer.hashicorp.com/terraform/language/settings/backends/local\n"),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"state is deprecated",
fmt.Sprintf("use path variable of local backend instead : https://developer.hashicorp.com/terraform/language/settings/backends/local\n"),
"Deprecated flag: -state",
fmt.Sprintf("Use `path` attribute within the `local` backend instead: https://developer.hashicorp.com/terraform/language/v1.10.x/settings/backends/local#path"),

Just a couple of nits over the language here.

I'd want to call out that it's the flag specifically that is deprecated.

Also, the documentation can change over time so I think it's better for us to link to the v1.10.x documentation directly given we're adding this deprecation warning for the 1.10 release. That way if we change the local backend in the future, people using this version will get a direct link to something that makes sense.

Can you copy these changes over to the other places we raise the warning?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that makes sense.
Thanks

@liamcervante
Copy link
Member

Oh yeah, the consistency checks make the point that you don't need to use Sprintf for the detail part of the error since the string is constant.

Copy link
Member

@liamcervante liamcervante left a comment

Choose a reason for hiding this comment

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

Nice, looks good! Will merge after the checks have finished!

@liamcervante liamcervante merged commit 2a9a8c2 into hashicorp:main Sep 5, 2024
6 checks passed
Copy link
Contributor

github-actions bot commented Sep 5, 2024

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

@liamcervante
Copy link
Member

Merged, and CHANGELOG updated. Will be released as part of 1.10.0, thanks for your contribution!

@melsonic
Copy link
Contributor Author

melsonic commented Sep 5, 2024

Merged, and CHANGELOG updated. Will be released as part of 1.10.0, thanks for your contribution!

Thanks for your guidance throughout the PR :)

Copy link
Contributor

github-actions bot commented Oct 6, 2024

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2024
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.

show deprecation warning if -state is used in conjunction with plan or apply
3 participants