error if import target is move source (#33192)

It is invalid for any import block to have a "to" argument matching any moved block's "from" argument.
This commit is contained in:
kmoe 2023-05-13 00:30:15 +01:00 committed by GitHub
parent bd6ba6cf99
commit 1172d40d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -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) {
@ -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
}

View File

@ -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" {
}