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

Code before:
			},
		})
	}

	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 am wondering if we want to be less consistent but more user-friendly for the deprecation of variables. In this case we can easily define if the value is null or not set at all. So from the consistency perspective it is better to stick with a single approach, otherwise it could help some users to identify the issues earlier.

Code after:
			},
		})
	}

	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(),
	})
}