diff --git a/internal/initwd/from_module.go b/internal/initwd/from_module.go index 01742a141f..f2b52a04d3 100644 --- a/internal/initwd/from_module.go +++ b/internal/initwd/from_module.go @@ -217,9 +217,7 @@ func DirFromModule(ctx context.Context, loader *configload.Loader, rootDir, modu mod, _ := loader.Parser().LoadConfigDir(rootDir) // ignore diagnostics since we're just doing value-add here anyway if mod != nil { for _, mc := range mod.ModuleCalls { - // TODO improve this sourceVal, _ := mc.SourceExpr.Value(nil) - if !sourceVal.IsKnown() { diags = diags.Append(&hcl.Diagnostic{ Severity: hcl.DiagError, diff --git a/internal/initwd/from_module_test.go b/internal/initwd/from_module_test.go index 690f142c78..ca3af4db56 100644 --- a/internal/initwd/from_module_test.go +++ b/internal/initwd/from_module_test.go @@ -12,6 +12,7 @@ import ( "github.com/google/go-cmp/cmp" version "github.com/hashicorp/go-version" + "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform/internal/configs" "github.com/hashicorp/terraform/internal/configs/configload" "github.com/hashicorp/terraform/internal/copy" @@ -358,3 +359,37 @@ func TestDirFromModule_rel_submodules(t *testing.T) { }) assertResultDeepEqual(t, gotTraces, wantTraces) } + +func TestDirFromModule_submodulesWithDynamicSources(t *testing.T) { + fixtureDir := filepath.Clean("testdata/empty") + fromModuleDir, err := filepath.Abs("./testdata/local-module-dynamic-sources") + if err != nil { + t.Fatal(err) + } + + tmpDir, done := tempChdir(t, fixtureDir) + defer done() + + hooks := &testInstallHooks{} + dir, err := filepath.EvalSymlinks(tmpDir) + if err != nil { + t.Error(err) + } + modInstallDir := filepath.Join(dir, ".terraform/modules") + + loader, cleanup := configload.NewLoaderForTests(t) + defer cleanup() + diags := DirFromModule(context.Background(), loader, dir, modInstallDir, fromModuleDir, nil, hooks) + + wantDiags := tfdiags.Diagnostics{}.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Unknown module source", + Detail: "Dynamic module sources cannot be used in conjunction with -from-module", + Subject: &hcl.Range{ + Filename: filepath.Join(dir, "main.tf"), + Start: hcl.Pos{Line: 2, Column: 12, Byte: 27}, + End: hcl.Pos{Line: 2, Column: 27, Byte: 42}, + }, + }) + tfdiags.AssertDiagnosticsMatch(t, diags, wantDiags) +} diff --git a/internal/initwd/testdata/local-module-dynamic-sources/main.tf b/internal/initwd/testdata/local-module-dynamic-sources/main.tf new file mode 100644 index 0000000000..c02abbfbba --- /dev/null +++ b/internal/initwd/testdata/local-module-dynamic-sources/main.tf @@ -0,0 +1,8 @@ +module "name" { + source = "./${var.path}" +} + +variable "path" { + type = string + default = "child" +}