Skip to content

Commit ca88f6f

Browse files
deprecation.Validate should only check top-level marks
1 parent 589b2d8 commit ca88f6f

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

internal/deprecation/deprecation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func (d *Deprecations) SuppressModuleCallDeprecation(addr addrs.Module) {
3535
d.suppressedModules.Add(addr)
3636
}
3737

38+
// Validate checks the given value for deprecation marks and returns diagnostics
39+
// for each deprecation found, unless deprecation warnings are suppressed for the given module.
40+
// It does not check for deeply nested deprecation marks.
3841
func (d *Deprecations) Validate(value cty.Value, module addrs.Module, rng *hcl.Range) (cty.Value, tfdiags.Diagnostics) {
3942
var diags tfdiags.Diagnostics
4043
deprecationMarks := marks.GetDeprecationMarks(value)
@@ -67,6 +70,9 @@ func (d *Deprecations) Validate(value cty.Value, module addrs.Module, rng *hcl.R
6770
return notDeprecatedValue, diags
6871
}
6972

73+
// ValidateAsConfig checks the given value for deprecation marks and returns diagnostics
74+
// for each deprecation found, unless deprecation warnings are suppressed for the given module.
75+
// It checks for deeply nested deprecation marks as well.
7076
func (d *Deprecations) ValidateAsConfig(value cty.Value, module addrs.Module) tfdiags.Diagnostics {
7177
var diags tfdiags.Diagnostics
7278
_, pvms := value.UnmarkDeepWithPaths()

internal/lang/marks/marks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func FilterDeprecationMarks(marks cty.ValueMarks) []DeprecationMark {
6464
// GetDeprecationMarks returns all deprecation marks present on the given
6565
// cty.Value.
6666
func GetDeprecationMarks(val cty.Value) []DeprecationMark {
67-
_, marks := val.UnmarkDeep()
67+
_, marks := val.Unmark()
6868
return FilterDeprecationMarks(marks)
6969
}
7070

internal/terraform/context_validate_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,3 +4594,73 @@ locals {
45944594
},
45954595
}))
45964596
}
4597+
4598+
func TestContext2Validate_using_module_with_deprecated_output(t *testing.T) {
4599+
m := testModuleInline(t, map[string]string{
4600+
"mod/main.tf": `
4601+
output "old" {
4602+
deprecated = "Please stop using this"
4603+
value = "old"
4604+
}
4605+
4606+
`,
4607+
"main.tf": `
4608+
module "mod" {
4609+
source = "./mod"
4610+
}
4611+
4612+
locals {
4613+
m = module.mod # OK - deprecated value is not used
4614+
}
4615+
4616+
output "test_output" {
4617+
value = module.mod.old # WARNING
4618+
}
4619+
`,
4620+
})
4621+
4622+
p := new(testing_provider.MockProvider)
4623+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
4624+
ResourceTypes: map[string]*configschema.Block{
4625+
"test_resource": {
4626+
Attributes: map[string]*configschema.Attribute{
4627+
"attr": {
4628+
Type: cty.String,
4629+
Computed: true,
4630+
},
4631+
},
4632+
},
4633+
},
4634+
Actions: map[string]*providers.ActionSchema{
4635+
"test_action": {
4636+
ConfigSchema: &configschema.Block{
4637+
Attributes: map[string]*configschema.Attribute{
4638+
"attr": {
4639+
Type: cty.String,
4640+
Required: true,
4641+
},
4642+
},
4643+
},
4644+
},
4645+
},
4646+
})
4647+
4648+
ctx := testContext2(t, &ContextOpts{
4649+
Providers: map[addrs.Provider]providers.Factory{
4650+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
4651+
},
4652+
})
4653+
4654+
diags := ctx.Validate(m, &ValidateOpts{})
4655+
4656+
tfdiags.AssertDiagnosticsMatch(t, diags, tfdiags.Diagnostics{}.Append(&hcl.Diagnostic{
4657+
Severity: hcl.DiagWarning,
4658+
Summary: "Deprecated value used",
4659+
Detail: "Please stop using this",
4660+
Subject: &hcl.Range{
4661+
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
4662+
Start: hcl.Pos{Line: 11, Column: 13, Byte: 143},
4663+
End: hcl.Pos{Line: 11, Column: 27, Byte: 157},
4664+
},
4665+
}))
4666+
}

0 commit comments

Comments
 (0)