Skip to content

Function call passed as props in #if tag runs even if #if condition is false #16583

@markpsiano

Description

@markpsiano

Describe the bug

If I have an {#if} block where the condition is false, any props passed to a component in the {#if} tag which are calculated by a function will still call the function, resulting in potentially unexpected behavior.

See repl: https://svelte.dev/playground/f1a27ae8e85f478f93e6718dcbe6d4d4?version=5.38.0

Here App displays a Component, which takes in a prop, which is initially defined. Then it's made undefined by the button click, so I would expect anything inside the #if (eg. the rendering of Component2) not to run. But it still triggers modifyProp which is called from inside #if, which attempts to modify a property of an undefined parameter, even though it seems like that would be protected by the {#if} clause guarding against undefined.

modifyProp is not run when not passed as a prop to another component.

Reproduction

https://svelte.dev/playground/f1a27ae8e85f478f93e6718dcbe6d4d4?version=5.38.0

Logs

TypeError
TypeError: Cannot read properties of undefined (reading 'property')
    at modifyProp (playground:output:5179:72)
    at eval (playground:output:5193:34)
    at update_reaction (playground:output:3938:57)
    at execute_derived (playground:output:1684:13)
    at update_derived (playground:output:1700:15)
    at is_dirty (playground:output:3857:7)
    at flush_inspect_effects (playground:output:2543:8)
    at internal_set (playground:output:2524:5)
    at set (playground:output:2448:10)
    at HTMLButtonElement.on_click (playground:output:5234:3)

System Info

System:
    OS: macOS 15.6
    CPU: (10) arm64 Apple M1 Pro
    Memory: 79.20 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.13.1 - /usr/local/bin/node
    npm: 10.5.2 - /usr/local/bin/npm
  Browsers:
    Chrome: 138.0.7204.184
    Safari: 18.6
  npmPackages:
    rollup: ^4.18.0 => 4.45.1 
    svelte: ^5.36.13 => 5.36.13

Severity

annoyance

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