diff --git a/internal/terraform/rootmodule/root_module_manager.go b/internal/terraform/rootmodule/root_module_manager.go index 801aa75af..7f6ea68f2 100644 --- a/internal/terraform/rootmodule/root_module_manager.go +++ b/internal/terraform/rootmodule/root_module_manager.go @@ -142,7 +142,13 @@ func (rmm *rootModuleManager) SchemaForPath(path string) (*schema.BodySchema, er return schema, nil } } - return nil, fmt.Errorf("failed to find schema for %s", path) + + rm, err := rmm.RootModuleByPath(path) + if err != nil { + return nil, err + } + + return rm.MergedSchema() } func (rmm *rootModuleManager) rootModuleByPath(dir string) (*rootModule, bool) { diff --git a/internal/terraform/rootmodule/root_module_manager_test.go b/internal/terraform/rootmodule/root_module_manager_test.go index 958c89c20..2778d37d1 100644 --- a/internal/terraform/rootmodule/root_module_manager_test.go +++ b/internal/terraform/rootmodule/root_module_manager_test.go @@ -457,6 +457,27 @@ func TestRootModuleManager_RootModuleCandidatesByPath(t *testing.T) { } } +func TestSchemaForPath_uninitialized(t *testing.T) { + rmm := testRootModuleManager(t) + + testData, err := filepath.Abs("testdata") + if err != nil { + t.Fatal(err) + } + path := filepath.Join(testData, "uninitialized-root") + + // model is added automatically during didOpen + _, err = rmm.AddAndStartLoadingRootModule(context.Background(), path) + if err != nil { + t.Fatal(err) + } + + _, err = rmm.SchemaForPath(path) + if err != nil { + t.Fatal(err) + } +} + func testRootModuleManager(t *testing.T) *rootModuleManager { fs := filesystem.NewFilesystem() rmm := newRootModuleManager(fs) diff --git a/internal/terraform/rootmodule/testdata/README.md b/internal/terraform/rootmodule/testdata/README.md index d77eeeeef..30a49bfca 100644 --- a/internal/terraform/rootmodule/testdata/README.md +++ b/internal/terraform/rootmodule/testdata/README.md @@ -23,3 +23,7 @@ which the language server supports and is tested against. - `multi-root-no-modules` - `multi-root-local-modules-down` - `multi-root-local-modules-up` - e.g. https://github.com/terraform-aws-modules/terraform-aws-security-group + +## Uninitialized Root + + - `uninitialized-root` diff --git a/internal/terraform/rootmodule/testdata/uninitialized-root/main.tf b/internal/terraform/rootmodule/testdata/uninitialized-root/main.tf new file mode 100644 index 000000000..5ec9731af --- /dev/null +++ b/internal/terraform/rootmodule/testdata/uninitialized-root/main.tf @@ -0,0 +1,3 @@ +variable "meal" { + default = "delicious" +}