DEV Community

orca forge
orca forge

Posted on Originally published at forge.workstyle.tech

Why electron-builder NSIS Updates Don't Restart Your App

📝 Originally published (in Japanese) at forge.workstyle.tech.

TL;DR: With electron-builder NSIS, an update can replace the files successfully without restarting the app. The /S argument enables silent execution, and by itself it does not meet the template's app launch condition. Pass --updated, /S, and --force-run for a silent update.

TL;DR

If an app won’t launch after an NSIS auto-update, the update may have completed successfully, but /S alone won’t launch the app. For a silent update (one that doesn’t show a UI), pass --updated, /S, and --force-run during the update to launch the app after it’s replaced.

Why Doesn’t the App Launch After an NSIS Update?

The NSIS template in electron-builder has a condition for launching the app after an update:

${ifNot} ${Silent}
${orIf} ${isForceRun}
  doStartApp
${endIf}
Enter fullscreen mode Exit fullscreen mode

This condition launches the app when the installer is not running silently or when isForceRun is enabled.

Passing /S runs the installer silently. /S alone does not satisfy the condition above, so the app won’t launch after it’s replaced.

This left me wondering: if the app exited during the update, had the replacement itself failed too? In this case, though, the replacement succeeded. Launching the app manually starts the new version. The app didn’t come back because the update was missing an instruction to launch it afterward.

Which arguments should you pass during an update?

When running an update with electron-builder’s NSIS, pass these three arguments:

const args = ['--updated', '/S', '--force-run']
Enter fullscreen mode Exit fullscreen mode

These arguments tell the installer how to behave:

  • --updated indicates that this is an update installation.
  • /S runs the installer silently.
  • --force-run launches the app after installation finishes.

I check more than just the NSIS configuration. I also check that these three arguments are passed where the installer is actually launched. Even if NSIS has a launch condition, it won’t be met unless the calling code passes --force-run.

After making a change, I check that the update-calling code hasn’t been left with only /S, and that --updated and --force-run are in the same argument array. Checking both the launch condition and the calling code makes it easier to track down any missing arguments.

What to Do When Silent Execution Fails

Silent execution may not always be possible. Some recorded cases involve installations that require administrator approval or are affected by security software.

In that case, show the UI and run the installer. The implementation retries with only --updated if launching with the usual three arguments fails.

The arguments differ between the UI and silent execution paths. For silent execution, pass all three arguments, including --force-run. For execution with the UI shown, pass --updated.

Manual launch is required when updating from 0.1.11 or earlier

This fix takes effect when the update starts from a version that includes the fix. The older version that is running when the update begins determines which arguments to pass to the installer.

As a result, when updating from an older version, the new installer may include --force-run, but the old version may not pass that argument. If you’re updating from 0.1.11 or earlier, you’ll need to launch the app manually after it’s replaced. Updates from 0.1.12 or later launch automatically.

Preparing a fixed installer alone won’t change the update behavior of older versions that have already been distributed. Check the version you’re updating from as well.

Key takeaways

  • Manually launch the app to check whether the new version runs.
  • Check that the NSIS template launch condition includes ${isForceRun}.
  • Pass --updated, /S, and --force-run when invoking a silent update.
  • If silent execution fails, run the installer with its UI and pass --updated.
  • Check the source version: updates from 0.1.11 or earlier need a manual launch; 0.1.12 and later start automatically.

Top comments (0)