Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions internal/terraform/context_plan_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2513,3 +2513,49 @@ resource "test_object" "a" {}
t.Fatal("Wrong, but impressive - how did you even defer the wrong resource?")
}
}

// This is a regression test for an expansion panic during plan where the
// child import block registers an expansion in the root module, which would
// then cause an "expansion already registered for <resource address>" panic.
func TestContextPlan_import_in_module_matches_root_to_addr(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
module "child" {
source = "./child"
}
import {
to = test_object.samename
id = "test-a"
}
`,
"child/main.tf": `
import {
to = test_object.samename
id = "test-b"
}
`,
})

ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(simpleMockProvider()),
},
})

validateDiags := ctx.Validate(m, nil)

wantErr := "module.child.test_object.samename not found. Only resources within the root module are eligible for config generation."
if !validateDiags.HasErrors() {
t.Errorf("unexpected success from validate\nwant: message containing %q", wantErr)
} else if got, want := validateDiags.Err().Error(), wantErr; !strings.Contains(got, want) {
t.Errorf("wrong error from validate:\ngot: %s\nwant: message containing %q", got, want)
}

// The expected error is only raised from validate, but the panic this regression test
// covers happens during expansion which is occurs in the plan.
//
// Since this isn't valid configuration we only care that plan doesn't panic.
ctx.Plan(m, states.NewState(), &PlanOpts{
Mode: plans.NormalMode,
})
}
4 changes: 2 additions & 2 deletions internal/terraform/transform_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) er
// generating configuration. Add them to the graph for validation.
for _, i := range importTargets {

log.Printf("[DEBUG] ConfigTransformer: adding config generation node for %s", i.Config.ToResource)
log.Printf("[DEBUG] ConfigTransformer: adding config generation node for %s", i.AbsToConfigResource)

// TODO: if config generation is ever supported for for_each
// resources, this will add multiple nodes for the same
// resource
abstract := &NodeAbstractResource{
Addr: i.Config.ToResource,
Addr: i.AbsToConfigResource,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@mildwonkey tagging you hoping you might have more context on whether this was the right fix (or if there might be other places to check for errors like this).

Originally I thought maybe i.Config.ToResource was incorrect and needed to be fixed, but I saw we recently added i.AbsToConfigResource and that made me think that i.Config.ToResource was kept to be relative purposefully 🤔

importTargets: []*ImportTarget{i},
generateConfigPath: t.generateConfigPathForImportTargets,
}
Expand Down