Skip to content

Commit

Permalink
Only validate a single file on didChange (#1370)
Browse files Browse the repository at this point in the history
* Introduce new RPCContextData for storing rpc info

* Attach RPCContextData to jobs

* Add RPCContextData to walker ctx, fix params

* Check RPCContext on SchemaValidation

* Bump hcl-lang to `485c60`

* Review feedback
  • Loading branch information
dbanck authored and radeksimko committed Sep 13, 2023
1 parent 35f9a21 commit ff9ab5d
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 4 deletions.
35 changes: 31 additions & 4 deletions internal/terraform/module/module_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import (
"io"
"io/fs"
"log"
"path"
"time"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl/v2"
tfjson "github.com/hashicorp/terraform-json"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
idecoder "github.com/hashicorp/terraform-ls/internal/decoder"
"github.com/hashicorp/terraform-ls/internal/decoder/validations"
"github.com/hashicorp/terraform-ls/internal/document"
Expand Down Expand Up @@ -695,10 +698,34 @@ func SchemaValidation(ctx context.Context, modStore *state.ModuleStore, schemaRe
return err
}

diags, rErr := moduleDecoder.Validate(ctx)
sErr := modStore.UpdateModuleDiagnostics(modPath, ast.SchemaValidationSource, ast.ModDiagsFromMap(diags))
if sErr != nil {
return sErr
var rErr error
rpcContext := lsctx.RPCContext(ctx)
isSingleFileChange := rpcContext.Method == "textDocument/didChange"
if isSingleFileChange {
filename := path.Base(rpcContext.URI)
// We only revalidate a single file that changed
var fileDiags hcl.Diagnostics
fileDiags, rErr = moduleDecoder.ValidateFile(ctx, filename)

modDiags, ok := mod.ModuleDiagnostics[ast.SchemaValidationSource]
if !ok {
modDiags = make(ast.ModDiags)
}
modDiags[ast.ModFilename(filename)] = fileDiags

sErr := modStore.UpdateModuleDiagnostics(modPath, ast.SchemaValidationSource, modDiags)
if sErr != nil {
return sErr
}
} else {
// We validate the whole module, e.g. on open
var diags lang.DiagnosticsMap
diags, rErr = moduleDecoder.Validate(ctx)

sErr := modStore.UpdateModuleDiagnostics(modPath, ast.SchemaValidationSource, ast.ModDiagsFromMap(diags))
if sErr != nil {
return sErr
}
}

return rErr
Expand Down
90 changes: 90 additions & 0 deletions internal/terraform/module/module_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
tfjson "github.com/hashicorp/terraform-json"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/registry"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/ast"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
tfaddr "github.com/hashicorp/terraform-registry-address"
Expand Down Expand Up @@ -966,3 +968,91 @@ var randomSchemaJSON = `{
}
}
}`

func TestSchemaValidation_FullModule(t *testing.T) {
ctx := context.Background()
ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}

testData, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
modPath := filepath.Join(testData, "invalid-config")

err = ss.Modules.Add(modPath)
if err != nil {
t.Fatal(err)
}

fs := filesystem.NewFilesystem(ss.DocumentStore)
err = ParseModuleConfiguration(ctx, fs, ss.Modules, modPath)
if err != nil {
t.Fatal(err)
}
ctx = lsctx.WithRPCContext(ctx, lsctx.RPCContextData{
Method: "textDocument/didOpen",
URI: "file:///test/variables.tf",
})
err = SchemaValidation(ctx, ss.Modules, ss.ProviderSchemas, modPath)
if err != nil {
t.Fatal(err)
}

mod, err := ss.Modules.ModuleByPath(modPath)
if err != nil {
t.Fatal(err)
}

expectedCount := 5
diagsCount := mod.ModuleDiagnostics[ast.SchemaValidationSource].Count()
if diagsCount != expectedCount {
t.Fatalf("expected %d diagnostics, %d given", expectedCount, diagsCount)
}
}

func TestSchemaValidation_SingleFile(t *testing.T) {
ctx := context.Background()
ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}

testData, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
modPath := filepath.Join(testData, "invalid-config")

err = ss.Modules.Add(modPath)
if err != nil {
t.Fatal(err)
}

fs := filesystem.NewFilesystem(ss.DocumentStore)
err = ParseModuleConfiguration(ctx, fs, ss.Modules, modPath)
if err != nil {
t.Fatal(err)
}
ctx = lsctx.WithRPCContext(ctx, lsctx.RPCContextData{
Method: "textDocument/didChange",
URI: "file:///test/variables.tf",
})
err = SchemaValidation(ctx, ss.Modules, ss.ProviderSchemas, modPath)
if err != nil {
t.Fatal(err)
}

mod, err := ss.Modules.ModuleByPath(modPath)
if err != nil {
t.Fatal(err)
}

expectedCount := 3
diagsCount := mod.ModuleDiagnostics[ast.SchemaValidationSource].Count()
if diagsCount != expectedCount {
t.Fatalf("expected %d diagnostics, %d given", expectedCount, diagsCount)
}
}
9 changes: 9 additions & 0 deletions internal/terraform/module/testdata/invalid-config/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test {}

variable {

}

locals {
test = 1
}
11 changes: 11 additions & 0 deletions internal/terraform/module/testdata/invalid-config/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable {

}

variable "test" {

}

output {

}

0 comments on commit ff9ab5d

Please sign in to comment.