Skip to content

Commit

Permalink
error if import target is move source (hashicorp#33192)
Browse files Browse the repository at this point in the history
It is invalid for any import block to have a "to" argument matching any moved block's "from" argument.
  • Loading branch information
kmoe authored May 12, 2023
1 parent bd6ba6c commit 1172d40
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
26 changes: 21 additions & 5 deletions internal/configs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ func (m *Module) appendFile(file *File) hcl.Diagnostics {
}
}

// "Moved" blocks just append, because they are all independent of one
// another at this level. (We handle any references between them at
// runtime.)
m.Moved = append(m.Moved, file.Moved...)

for _, i := range file.Import {
for _, mi := range m.Import {
if i.To.Equal(mi.To) {
Expand Down Expand Up @@ -441,14 +446,25 @@ func (m *Module) appendFile(file *File) hcl.Diagnostics {
// will already have been caught.
}

// It is invalid for any import block to have a "to" argument matching
// any moved block's "from" argument.
for _, mb := range m.Moved {
// Comparing string serialisations is good enough here, because we
// only care about equality in the case that both addresses are
// AbsResourceInstances.
if mb.From.String() == i.To.String() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Cannot import to a move source",
Detail: "An import block for ID %q targets resource address %s, but this address appears in the \"from\" argument of a moved block, which is invalid. Please change the import target to a different address, such as the move target.",
Subject: &i.DeclRange,
})
}
}

m.Import = append(m.Import, i)
}

// "Moved" blocks just append, because they are all independent of one
// another at this level. (We handle any references between them at
// runtime.)
m.Moved = append(m.Moved, file.Moved...)

return diags
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {
id = "foo/bar"
to = local_file.foo_bar
}

moved {
from = local_file.foo_bar
to = local_file.bar_baz
}

resource "local_file" "bar_baz" {
}

0 comments on commit 1172d40

Please sign in to comment.