Repository: opentofu/opentofu
File: internal/tofu/eval_variable.go (Go)

Code before:
		diags = diags.Append(&hcl.Diagnostic{
			Severity:    hcl.DiagError,
			Summary:     errInvalidValue,
			Detail:      fmt.Sprintf("%s\n\nThis was checked by the validation rule at %s.", errorMessage, validation.DeclRange.String()),
			Subject:     config.DeclRange.Ptr(),
			Expression:  validation.Condition,
			EvalContext: hclCtx,
			Extra: &addrs.CheckRuleDiagnosticExtra{
				CheckRule: addr.CheckRule(addrs.InputValidation, ix),
			},
		})
	}

	return checkResult{
		Status:         status,
		FailureMessage: errorMessage,
	}, diags
}

// evalVariableDeprecation checks a variable "deprecated" message
func evalVariableDeprecation(addr addrs.AbsInputVariableInstance, config *configs.Variable, expr hcl.Expression, ctx EvalContext, onlyLocal bool) (diags tfdiags.Diagnostics) {
	if !config.DeprecatedSet {
		log.Printf("[TRACE] evalVariableDeprecation: variable %s is having no deprecation configured", addr)
		return nil
	}
	if onlyLocal && strings.HasPrefix(config.DeclRange.Filename, ".terraform") {
		log.Printf("[TRACE] evalVariableDeprecation: variable %s is part of a remote module and was requested to ignore those", addr)
		return nil
	}
	// if the variable is not given in the module call, do not show a warning
	if expr == nil {
		log.Printf("[TRACE] evalVariableDeprecation: variable %s is marked as deprecated but is not used", addr)
		return nil
	}
	val := ctx.GetVariableValue(addr)
	if val.IsNull() {
		log.Printf("[TRACE] evalVariableDeprecation: variable %s is marked as deprecated null value given", addr)
		return nil
	}
	log.Printf("[TRACE] evalVariableDeprecation: usage of deprecated variable %q detected", addr)
	// Any deprecated
	return diags.Append(&hcl.Diagnostic{
		Severity: hcl.DiagWarning,
		Summary:  fmt.Sprintf(`The variable %q is marked as deprecated by module author`, config.Name),
		Detail:   config.Deprecated,
		Subject:  expr.Range().Ptr(),
	})
}


Review comment (suggestion):
I believe it could be "../.terraform" as well, so this check must be "smarter". I was thinking about a property of a module call, which could be passed into `nodeVariableReferenceInstance`. I would suggest to have this discussion on a separate cli flag PR if you agree with the idea to move that sub-feature out.

Code after:
		diags = diags.Append(&hcl.Diagnostic{
			Severity:    hcl.DiagError,
			Summary:     errInvalidValue,
			Detail:      fmt.Sprintf("%s\n\nThis was checked by the validation rule at %s.", errorMessage, validation.DeclRange.String()),
			Subject:     config.DeclRange.Ptr(),
			Expression:  validation.Condition,
			EvalContext: hclCtx,
			Extra: &addrs.CheckRuleDiagnosticExtra{
				CheckRule: addr.CheckRule(addrs.InputValidation, ix),
			},
		})
	}

	return checkResult{
		Status:         status,
		FailureMessage: errorMessage,
	}, diags
}

// evalVariableDeprecation checks if a variable is deprecated and if so it returns a warning diagnostic to be shown to the user
func evalVariableDeprecation(addr addrs.AbsInputVariableInstance, config *configs.Variable, expr hcl.Expression, ctx EvalContext) tfdiags.Diagnostics {
	if config.Deprecated == "" {
		log.Printf("[TRACE] evalVariableDeprecation: variable %s does not have deprecation configured", addr)
		return nil
	}
	// if the variable is not given in the module call, do not show a warning
	if expr == nil {
		log.Printf("[TRACE] evalVariableDeprecation: variable %s is marked as deprecated but is not used", addr)
		return nil
	}
	val := ctx.GetVariableValue(addr)
	if val == cty.NilVal {
		log.Printf("[TRACE] evalVariableDeprecation: variable %s is marked as deprecated but no value given", addr)
		return nil
	}
	log.Printf("[TRACE] evalVariableDeprecation: usage of deprecated variable %q detected", addr)
	var diags tfdiags.Diagnostics
	return diags.Append(&hcl.Diagnostic{
		Severity: hcl.DiagWarning,
		Summary:  fmt.Sprintf(`The variable %q is marked as deprecated by module author`, config.Name),
		Detail:   fmt.Sprintf("This variable is marked as deprecated with the following message:\n%s", config.Deprecated),
		Subject:  expr.Range().Ptr(),
	})
}