Proposal: Keep the internal variables of Velocity macros in the `$macro` scope

Hello!

Context

In the Velocity code style update discussion, we agreed that a Velocity macro which modifies existing variables, or returns values through new ones, takes those variables as parameters. It’s now part of the Velocity Code Style. This rule covers the values a macro means to hand back, but not the variables it only uses internally, which are still global by default.

We just had a regression caused by exactly that. See PR #6963.

XWiki already enables the $macro and $template scope variables in DefaultVelocityConfiguration, and #set ($macro.something = ...) is used about 50 times in xwiki-platform, but the code style doesn’t say when to use it.

Proposal

Add a rule to the Velocity Code Style:

Variables used only inside a Velocity macro are set in the $macro scope.

Precisions

  • The rule would apply to new and modified code, it wouldn’t require a sweep of all the existing macros.
  • Moving the variables of an existing macro to $macro could break a script that reads them after the call. Since they were never documented as outputs, I think that’s an acceptable break.

Examples

## Not like this:
#macro (displayGreeting $userReference)
  #set ($userName = $xwiki.getPlainUserName($userReference))
  Hello $escapetool.xml($userName)!
#end
## But like this:
#macro (displayGreeting $userReference)
  #set ($macro.userName = $xwiki.getPlainUserName($userReference))
  Hello $escapetool.xml($macro.userName)!
#end

Opinion

+1 from me for adding this rule because:

  • These bugs are hard to trace back and catch when building partially for a PR: the symptom shows up on an unrelated UI, sometimes far from the macro that set the variable.
  • The mechanism is already enabled and used in the codebase, so there’s nothing new to set up.
  • Together with the 2024 rule, it gives a complete picture: what a macro returns goes through its parameters, and everything else stays in the $macro scope.

Conclusion

Do you agree with adding this rule to the Velocity Code Style?

I’ll close this topic in a week and update the code style page according to its result.

Thank you!
Lucas C.

+1, thanks Lucas. This completes the 2024 rule nicely. I’d add a few details to the code style page so that the rule can always be applied as written:

  • $macro is not fully isolated between nested macros. If a key isn’t set in the current scope, Scope.get() looks it up in the parent scope, and the parent is the calling macro’s $macro. So if a nested macro reads $macro.userName before setting it, it silently gets the caller’s value. I checked this on Velocity 2.4.1. The rule should say to initialise each $macro.x before reading it, e.g. #set ($macro.x = $NULL), which does block the lookup.
  • Say what doesn’t need $macro. Macro parameters are already restored by Velocity after the call. #foreach loop variables are saved and restored by the directive itself, and #foreach ($macro.item in ...) isn’t valid syntax anyway.
  • Templates have the same problem. Variables set in a template included with #template/#parse leak the same way, and $template is enabled by the same configuration. I’d make the rule cover both: “Variables used only inside a Velocity macro or template are set in the $macro (resp. $template) scope.”
  • When converting an existing macro, convert all its variables at once. Mixing $macro.x and $x for the same variable fails silently: it prints the literal $x or reads a stale global value.
  • Minor: the “Not like this” example would be more convincing if it showed the actual collision, e.g. a caller whose own $userName gets overwritten by the call.

WDYT?

Thanks for your feedback!

IMO this is too much. I think the “difficult to track/predict” doesn’t apply here since we’re talking about the macros that interact with the one you’re editing/creating so of course you’d check them out. This is an additional rule that makes our code extra sturdy but so far it’s unnecessary. Definitely +1 to add it if we catch some nested macro clashes in the future, but I think it’s too much constraint for now. We already make template macro writing heavier with the main proposal, it’d make it even worst.

+1 :+1: we can give a few common examples, no need to give every single use case, just enough to make it understandable that it’s up to good sense if they need to break the rule.

Same as before, we didn’t hit such bad collisions with templates so there’s no need to make the restrictions larger than what we need. I agree that on paper it’d be best if we apply it too.

One thing that’s different with templates is that at least in XS, we have a lot of templates that actually define and modify global variables, so there’d be a lot of exceptions to the rules. Usually that’s kind of what motivates the creation of a template instead of a single macro. A template sets the global environment up for what comes next.

If this lack of scoping at template level causes reported problems, I’d be +1 for any later codestyle proposal :slight_smile:

The example you gave looks off when trying to match it with the preceding sentence. I agree with the idea that we shouldn’t mix $macro.x and $y, so we can add this detail :+1:
I thought it was already implied in the initial proposal that we’d change all instances of $x in the macro.

Okay, I’ll update the example to take this into consideration when updating the codestyle if the community agrees with this idea. :+1:

That works for the caller, but not for the callee: when you write a macro in a shared place (macros.vm, a UIX, an extension), you can’t know which macros will call it, including ones written later. If it reads $macro.x before setting it, it silently picks up whatever its future callers put there.

Also, the rule costs almost nothing: it only says that reading $macro.x before setting it reads the parent scope, so if that’s not what you want, set it first. Normal code sets before reading and is unaffected. It’s about making the behaviour explicit, so that nobody (human or LLM) assumes $macro is fully isolated, which is the natural assumption.

Agreed, and that’s fine: those aren’t exceptions, they’re the other use case. The rule only covers variables used only inside the template. The point is to separate the two intents:

  • a variable local to the template → $template.x
  • a variable meant to set up the environment for what follows → $x

Both are valid, but today they look the same, so a reader can’t tell whether $x is an intended output or a leak. I don’t think we need to wait for a reported collision to apply this, the same way we didn’t wait for one to adopt the 2024 rule.

I meant the same name in both forms: e.g. #set ($macro.x = ...) followed by $x later in the macro. $x then either prints literally or reads a stale global. Partial conversions tend to produce exactly that.

Thanks for the clarification, definitely +1 on this then :slight_smile:

I wasn’t clear with my intention here, but I meant “modified code” as “the whole macro containing the modified lines”. We agree :+1:

Hi, I think this is a good direction, and I’m +1 to enforce scope isolation like @vmassol suggested. As an added benefit, it would make macro code a lot easier to parse if all local variables are properly defined at the start.

Thanks.

Closing this topic with 3 +1
Updating the velocity code style page with the macro level rules. Leaving the template ones for later if the topic comes up again.