Skip to content

Commit b81d28d

Browse files
authored
fix: Avoid validating *.tfvars outside of modules (hashicorp#1447)
1 parent 2552252 commit b81d28d

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

internal/decoder/decoder.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ func varsPathContext(mod *state.Module) (*decoder.PathContext, error) {
6464
ReferenceOrigins: make(reference.Origins, 0),
6565
ReferenceTargets: make(reference.Targets, 0),
6666
Files: make(map[string]*hcl.File),
67-
Validators: varsValidators,
67+
}
68+
69+
if len(mod.ParsedModuleFiles) > 0 {
70+
// Only validate if this is actually a module
71+
// as we may come across standalone tfvars files
72+
// for which we have no context.
73+
pathCtx.Validators = varsValidators
6874
}
6975

7076
for _, origin := range mod.VarsRefOrigins {

internal/terraform/module/module_ops_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -1405,3 +1405,52 @@ func TestSchemaVarsValidation_SingleFile(t *testing.T) {
14051405
t.Fatalf("expected %d diagnostics, %d given", expectedCount, diagsCount)
14061406
}
14071407
}
1408+
1409+
func TestSchemaVarsValidation_outsideOfModule(t *testing.T) {
1410+
ctx := context.Background()
1411+
ss, err := state.NewStateStore()
1412+
if err != nil {
1413+
t.Fatal(err)
1414+
}
1415+
1416+
testData, err := filepath.Abs("testdata")
1417+
if err != nil {
1418+
t.Fatal(err)
1419+
}
1420+
modPath := filepath.Join(testData, "standalone-tfvars")
1421+
1422+
err = ss.Modules.Add(modPath)
1423+
if err != nil {
1424+
t.Fatal(err)
1425+
}
1426+
1427+
fs := filesystem.NewFilesystem(ss.DocumentStore)
1428+
ctx = lsctx.WithDocumentContext(ctx, lsctx.Document{})
1429+
err = ParseModuleConfiguration(ctx, fs, ss.Modules, modPath)
1430+
if err != nil {
1431+
t.Fatal(err)
1432+
}
1433+
err = LoadModuleMetadata(ctx, ss.Modules, modPath)
1434+
if err != nil {
1435+
t.Fatal(err)
1436+
}
1437+
err = ParseVariables(ctx, fs, ss.Modules, modPath)
1438+
if err != nil {
1439+
t.Fatal(err)
1440+
}
1441+
err = SchemaVariablesValidation(ctx, ss.Modules, ss.ProviderSchemas, modPath)
1442+
if err != nil {
1443+
t.Fatal(err)
1444+
}
1445+
1446+
mod, err := ss.Modules.ModuleByPath(modPath)
1447+
if err != nil {
1448+
t.Fatal(err)
1449+
}
1450+
1451+
expectedCount := 0
1452+
diagsCount := mod.VarsDiagnostics[ast.SchemaValidationSource].Count()
1453+
if diagsCount != expectedCount {
1454+
t.Fatalf("expected %d diagnostics, %d given", expectedCount, diagsCount)
1455+
}
1456+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo = "bar"

0 commit comments

Comments
 (0)