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

command: Fix state mv for only resource in module #25523

Merged
merged 1 commit into from
Aug 11, 2020

Conversation

alisdair
Copy link
Contributor

@alisdair alisdair commented Jul 8, 2020

When moving a resource block with multiple instances to a new address within the same module, we need to ensure that the target module is present as late as possible. Otherwise, deleting the resource from the original address triggers pruning, and the module is removed just before we try to add the resource to it, which causes a crash.

Includes regression test which panics without this code change.

Fixes #25520

@alisdair alisdair added this to the v0.13.1 milestone Jul 8, 2020
@alisdair alisdair requested a review from a team July 8, 2020 21:19
@alisdair alisdair self-assigned this Jul 8, 2020
@codecov
Copy link

codecov bot commented Jul 8, 2020

Codecov Report

Merging #25523 into master will decrease coverage by 0.38%.
The diff coverage is 100.00%.

Impacted Files Coverage Δ
command/state_mv.go 42.72% <100.00%> (+0.26%) ⬆️
internal/providercache/lock_file.go 14.58% <0.00%> (-16.67%) ⬇️
internal/providercache/dir.go 62.19% <0.00%> (-13.67%) ⬇️
internal/providercache/installer.go 19.90% <0.00%> (-10.73%) ⬇️
backend/local/backend_refresh.go 35.41% <0.00%> (-6.10%) ⬇️
internal/providercache/dir_modify.go 76.47% <0.00%> (-5.89%) ⬇️
configs/resource.go 76.51% <0.00%> (-5.31%) ⬇️
backend/local/backend_local.go 36.89% <0.00%> (-4.34%) ⬇️
internal/providercache/package_install.go 21.05% <0.00%> (-1.74%) ⬇️
backend/local/backend.go 48.06% <0.00%> (-1.72%) ⬇️
... and 41 more

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.

Excellent, and what an interesting subtle bug!

@@ -223,7 +220,7 @@ func (c *StateMvCommand) Run(args []string) int {

// Update the address before adding it to the state.
rs.Addr = addrTo
stateTo.Module(addrTo.Module).Resources[addrTo.Resource.String()] = rs
stateTo.EnsureModule(addrTo.Module).Resources[addrTo.Resource.String()] = rs
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that this also clears up a bit of redundancy (more like unnecessary code) since EnsureModule is safe to run in place of Module (here, that is)!

When moving a resource block with multiple instances to a new address
within the same module, we need to ensure that the target module is
present as late as possible. Otherwise, deleting the resource from the
original address triggers pruning, and the module is removed just before
we try to add the resource to it, which causes a crash.

Includes regression test which panics without this code change.
@alisdair alisdair merged commit c5377ca into master Aug 11, 2020
@alisdair alisdair deleted the alisdair/fix-state-mv-panic branch August 11, 2020 16:18
@ghost
Copy link

ghost commented Sep 11, 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 and limited conversation to collaborators Sep 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

terraform state mv panic
2 participants