diff --git a/internal/configs/module.go b/internal/configs/module.go index eb0f70e23b..99817bd36b 100644 --- a/internal/configs/module.go +++ b/internal/configs/module.go @@ -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 } diff --git a/internal/configs/testdata/invalid-modules/import-to-moved-from/main.tf b/internal/configs/testdata/invalid-modules/import-to-moved-from/main.tf new file mode 100644 index 0000000000..f0f1a7a56f --- /dev/null +++ b/internal/configs/testdata/invalid-modules/import-to-moved-from/main.tf @@ -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" { +}