sim icon indicating copy to clipboard operation
sim copied to clipboard

[BUG] Variable substitution breaks nullish coalescing operator (??) in function blocks

Open PlaneInABottle opened this issue 10 months ago • 2 comments

Bug Description

When a block reference cannot be resolved in function/condition blocks, the variable substitution defaults to empty string ("") instead of undefined. This breaks the nullish coalescing operator (??) in user code.

Root Cause

In apps/sim/app/api/function/execute/route.ts, the resolveTagVariables function uses:

let tagValue = getNestedValue(params, tagName) || getNestedValue(blockData, tagName) || ''

This defaults to empty string when a reference cannot be resolved. In user code that uses ??, the empty string is treated as a valid value (not nullish), preventing fallback logic from working:

// User code
const conversationId = webhook1ConvId ?? webhook1ConvIdField ?? webhook1Id ?? startConvId;

When webhook1ConvId is substituted as "" (instead of undefined), the ?? never reaches startConvId.

Impact

  • Function/condition blocks cannot reliably use nullish coalescing for fallback values
  • Workflows that depend on ?? for optional field handling will fail unexpectedly
  • Workaround forces users to use verbose truthy checks instead of modern JavaScript patterns

Example

Webhook trigger missing field:

const value = <webhook.optional_field> ?? 'default'; // Returns '' instead of 'default'

Expected behavior: Returns 'default' Actual behavior: Returns ''

Solution

Change the fallback to use undefined instead of empty string:

let tagValue = getNestedValue(params, tagName) ?? getNestedValue(blockData, tagName)

Affected Code Locations

  • Line 528: Initial substitution should not default to ''
  • Line 542: Second fallback should not default to ''

Files to Update

  • apps/sim/app/api/function/execute/route.ts - resolveTagVariables function

Testing

This should be verified with a workflow that:

  1. Has a webhook trigger with optional fields
  2. Uses ?? to fall back to start trigger values
  3. Confirms the fallback chain works correctly

PlaneInABottle avatar Jan 13 '26 20:01 PlaneInABottle

@Sg312

waleedlatif1 avatar Jan 14 '26 03:01 waleedlatif1

Fixed in latest release

icecrasher321 avatar Jan 23 '26 08:01 icecrasher321