Skip to content

{{else customHelper args}} inside {{#if}} / {{#unless}} throws ArgumentNullException (Parameter 'test') at compile time #683

Closed

Description

@o-serhiichyk

Description

#648 added {{else name args}} chaining for block helpers (fixing #263). It intentionally leaves the existing {{#if}} / {{#unless}} chaining untouched, and that path only understands {{else if x}} / {{else unless x}}. Chaining a different block helper after {{#if}}, which handlebars.js supports, fails at compile time:

{{#if a}}A{{else ifEq b "x"}}B{{else}}C{{/if}}

Repro (Handlebars.Net 2.4.3, net10.0)

using HandlebarsDotNet;

var hb = Handlebars.Create();
hb.RegisterHelper("ifEq", new HandlebarsBlockHelper((output, options, context, arguments) =>
{
    if (Equals(arguments[0]?.ToString(), arguments[1]?.ToString())) options.Template(output, context);
    else options.Inverse(output, context);
}));

var data = new Dictionary<string, object> { ["a"] = false, ["b"] = "x" };

// Expected "B" (handlebars.js 4.7.8 with the same helper renders "B").
Console.WriteLine(hb.Compile("{{#if a}}A{{else ifEq b \"x\"}}B{{else}}C{{/if}}")(data));

Actual:

System.ArgumentNullException: Value cannot be null. (Parameter 'test')

The exception is thrown by Compile, before any data is read, and it is not a HandlebarsException. Callers that catch HandlebarsException to handle bad templates therefore miss it.

Template 2.1.6 2.4.3 handlebars.js 4.7.8
{{#if a}}A{{else ifEq b "x"}}B{{else}}C{{/if}} ArgumentNullException ArgumentNullException B
{{#ifEq b "y"}}A{{else ifEq b "x"}}B{{else}}C{{/ifEq}} C (wrong) B B
{{#if a}}A{{else if (eq b "x")}}B{{else}}C{{/if}} (with an eq helper) B B B
{{#if a}}A{{else ifEq b "y"}}B{{/if}} B (wrong) B (wrong) empty

Cause (from reading master at f6e613b)

In Compiler/Lexer/Converter/BlockAccumulators/ConditionalBlockAccumulatorContext.cs:

  1. HandlesChainedElseInternally => true, so the generic desugaring from feat: support else-if chaining for block helpers #648 is skipped for if / unless.
  2. IsElseIfBlock treats an else as a chained condition only when it has exactly two arguments (else if x). {{else ifEq b "x"}} has three, so it is handled as a plain {{else}} and _currentCondition becomes null. The ifEq branch and its arguments are dropped.
  3. At the next {{else}}, HandleElement calls Expression.IfThen(_currentCondition!, ...) with a null test, which throws ArgumentNullException (Parameter 'test'). With only one {{else ifEq ...}} and no final {{else}} there is no exception, but the condition is still dropped and the branch always renders. With a = false and b = "x", {{#if a}}A{{else ifEq b "y"}}B{{/if}} renders B on both 2.1.6 and 2.4.3, where handlebars.js renders an empty string.

Related: GetElseIfTestExpression never checks that the helper named after else is if or unless. So a two-argument {{else someHelper x}} is evaluated as {{else if x}}, ignoring someHelper.

Suggested fix

In ConditionalBlockAccumulatorContext, chain internally only when the else names if or unless with a single condition argument (plus optional hash). For any other {{else name args...}}, desugar it into a nested block the same way #648 does for other block helpers. Otherwise, throw a HandlebarsCompilerException with the template location instead of an ArgumentNullException.

Environment

  • Handlebars.Net 2.1.6 and 2.4.3 (NuGet), .NET 10, Windows
  • master at f6e613b read, not run

Activity

  1. rexm commented on Nov 21, 2026

    @rexm
    Member

    /harness adopt

  2. stencil-harness commented on Nov 21, 2026

    @stencil-harness
    Contributor

    I'm the agent managing intake of issues on Rex's behalf. I understand the request: {{else someHelper args}} chained inside {{#if}}/{{#unless}} is dropped or throws ArgumentNullException at compile time, and should instead chain the named block helper the way handlebars.js does. I'll evaluate whether I can address this autonomously.

  3. stencil-harness commented on Nov 21, 2026

    @stencil-harness
    Contributor

    Fix is in #687, with auto-merge requested. It merges once the required checks pass.

    Changed: Handlebars-Net/Handlebars.Net (only repo touched).

    • Inside {{#if}}/{{#unless}}, only {{else if x}} (optionally with a hash) is still flattened internally. Every other named chain now nests the named block helper the same way feat: support else-if chaining for block helpers #648 does for other blocks. This covers {{else unless x}} and {{else customHelper a b}}, and matches handlebars.js.
    • {{#if a}}A{{else ifEq b "x"}}B{{else}}C{{/if}} now renders B. {{#if a}}A{{else ifEq b "y"}}B{{/if}} with b="x" now renders empty.
    • A second {{else}} after a plain {{else}}, and {{else if a b}}, now throw HandlebarsCompilerException instead of ArgumentNullException.
    • Behaviour changes to note: {{else unless x}} was evaluated as {{else if x}}, so its result was inverted, and it now behaves as unless. {{else someHelper x}} used to ignore someHelper; it now calls it, and throws the usual missing-helper error if it isn't registered. The PR description has a before/after table from actual runs.

    Verified: new ConditionalElseChainTests added; the full suite passes (2259/2259) and the library builds for all targets.

    Shipped: the PR is not merged yet. No NuGet release was made. Suggested release is a patch.

  4. rexm commented on Nov 21, 2026

    @rexm
    Member

    There is an existing PR linked to this issue. Compare both and find the best and most complete path.

  5. stencil-harness commented on Nov 21, 2026

    @stencil-harness
    Contributor

    Understood. I'll compare #684 with #687 on code, behaviour (actual runs against both) and tests, then consolidate on the more complete path and report back here.

  6. stencil-harness commented on Nov 21, 2026

    @stencil-harness
    Contributor

    Comparison of #684 and #687. This is based on #684's description and stated test results, plus actual runs of #687.

    Same core fix. Both replace the per-block HandlesChainedElseInternally flag with a per-else decision. Both route {{else customHelper …}} through the #648 nested-block desugaring, and both build the last conditional branch in AccumulatedBlock. The issue's repro renders B with either.

    Differences

    Case #684 #687
    {{else unless x}} negated inside the flat chain desugared to nested {{#unless}}, same output
    {{else if b c}} / {{else unless b c}} HandlebarsCompilerException HandlebarsCompilerException (same message)
    {{else}}…{{else}} not covered (was ArgumentNullException) HandlebarsCompilerException
    bare {{else if}} (no condition) described as a compile error still a plain {{else}}, as today, so existing templates keep compiling
    {{else if b includeZero=true}} not mentioned works (was ArgumentNullException)
    Tests added to IssueTests capability-named ConditionalElseChainTests (per AGENTS.md)

    Path taken: I've consolidated on #687 and folded in #684's extra coverage: the malformed else if/else unless rows in ExceptionTests, plus else unless under #unless and whitespace control. #687 is a superset, and the one place they diverge (a bare {{else if}}) favours backward compatibility.

    Changed: Handlebars-Net/Handlebars.Net only. I pushed one extra test commit to #687 and updated its description with the comparison.
    Verified: full suite 2270/2270 passing; the library builds for all targets.
    Shipped: #687 still has auto-merge requested, and its checks are running. It is not merged yet, and no release has been made (suggest a patch). #684 is left open for a maintainer to close.

  7. stencil-harness commented on Nov 21, 2026

    @stencil-harness
    Contributor

    Follow-up on the #684 / #687 comparison. This time I ran both PRs' actual code with the same templates and helpers, and ran #684's own tests against the merged #687.

    Result: #687 (merged) covers everything #684 fixes. It also turns the double-{{else}} case into a HandlebarsCompilerException; under #684 it still throws ArgumentNullException. #684 had one test scenario #687 lacked, chaining the built-in {{else with b}}. It already passes on master, and I've added it in #689 (tests only, credited to #684, auto-merge requested).

    One owner decision remains: a bare {{else name}} (nothing after the name). #687 kept the pre-existing behaviour. #684 switches to handlebars.js semantics. Actual output:

    Template Data Current (2.4.3 and master) #684 / handlebars.js semantics
    {{#if a}}A{{else isAdmin}}B{{/if}} a=false, isAdmin=false B ``
    {{#if a}}A{{else items}}[{{this}}]{{/if}} a=false, items=[1,2] [{ a = False, … }] [1][2]
    {{#if a}}A{{else if}}B{{/if}} a=false B HandlebarsCompilerException: #if requires exactly one argument

    Templates like {{else isAdmin}} (an else if with the if left out) render today, but render the wrong thing. Switching silently changes their output in production, and a typo'd {{else if}} would stop compiling.

    Recommendation: leave master as is. If you want handlebars.js parity here, ship the {{else name}} change on its own in a minor release with a compatibility note, and keep a bare {{else if}} / {{else unless}} as a plain {{else}}. Reply if you want me to do that. I won't change it without your go-ahead.

    Changed: Handlebars-Net/Handlebars.Net. #689 adds one test, and the full suite passes 2271/2271. Its checks are running; it is not merged yet. No release has been made. #684 is still open: it is now superseded by #687 apart from the decision above.

  8. rexm commented on Nov 22, 2026

    @rexm
    Member

    If #684 is superseded let's close it and make sure the author is credited for their contributions. Cut a new issue that changes the behavior so the js semantics are on by default and there's an opt-out to go back to the old behavior. We'll cut a new release with that breaking change.

  9. reopened this on Nov 22, 2026
  10. stencil-harness commented on Nov 22, 2026

    @stencil-harness
    Contributor

    Done, apart from closing #684 itself.

    Changed: no code changes in this step; the only repo affected is Handlebars-Net/Handlebars.Net, where #695 was opened. Nothing shipped or released.

  11. stencil-harness commented on Nov 22, 2026

    @stencil-harness
    Contributor

    #684 is now closed as superseded. Everything requested on this issue is done:

    Changed: Handlebars-Net/Handlebars.Net only, with no new code in this step. Nothing is pending on the robot/issue-683 branch. No release has been made. That waits for #695, per your note.

  12. added a commit that references this issue on Nov 25, 2026
    3b3aa59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions