fix(installer): upgrade 时刷新桌面快捷方式图标#111
Conversation
When AgentDock is upgraded in place, the existing .lnk shortcuts in the desktop / Start Menu keep showing the OLD icon even after the new .exe is installed. Cause: electron-builder's default NSIS flow only updates the .exe — it leaves existing .lnk files in place, and Windows caches .lnk icon references in the shell icon cache (IconCache.db). The new .icO resource is read by explorer lazily, but the cache keeps showing the old icon until the user moves/resets the shortcut or restarts explorer. Fix: ship a custom NSIS include (build/installer/custom-install.nsh) that detects the upgrade path (old .exe still present in $INSTDIR) and deletes the existing .lnk files before electron-builder recreates them. The new .lnk is created with a fresh icon reference, forcing explorer to re-read the new icon from the freshly-installed exe. This is the v0.2.2 patch. Existing v0.2.1 users will see the desktop shortcut icon refresh automatically when they upgrade.
There was a problem hiding this comment.
Code Review
This pull request introduces a custom NSIS script to delete old desktop and Start Menu shortcuts during an upgrade, forcing Windows Explorer to refresh the application icon. Feedback on the changes points out that hardcoding SetShellVarContext current can break All Users installation mode and pollute the global state; removing this line allows electron-builder's pre-configured context to be used safely.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| SetShellVarContext current | ||
| ${If} ${FileExists} "$DESKTOP\${SHORTCUT_NAME}.lnk" | ||
| Delete "$DESKTOP\${SHORTCUT_NAME}.lnk" | ||
| ${EndIf} | ||
| ${If} ${FileExists} "$SMPROGRAMS\${SHORTCUT_NAME}.lnk" | ||
| Delete "$SMPROGRAMS\${SHORTCUT_NAME}.lnk" | ||
| ${EndIf} |
There was a problem hiding this comment.
在 NSIS 中,SetShellVarContext 是一个全局状态设置。在这里硬编码 SetShellVarContext current 会带来两个严重问题:
- 破坏“所有用户(Per-Machine / All Users)”安装模式:如果用户选择为“所有用户”安装,快捷方式会被创建在公共目录(All Users Desktop / Start Menu)下。硬编码
current会导致升级时去尝试删除当前用户的对应目录,从而无法清除公共目录下的旧快捷方式,导致图标刷新失效。 - 污染后续安装步骤的上下文:由于
SetShellVarContext的修改是全局生效且持续的,在customInstall执行完后,如果未将其恢复,后续 electron-builder 默认的安装步骤(如创建新快捷方式、写入注册表等)可能会在错误的current上下文中执行,导致安装行为异常。
解决方案:
由于 electron-builder 在调用 customInstall 之前已经根据用户的安装选择(单用户或所有用户)正确设置了 SetShellVarContext,我们完全不需要在脚本中手动设置它。直接移除 SetShellVarContext current 即可,这样 $DESKTOP 和 $SMPROGRAMS 会自动指向当前安装模式对应的正确路径。
${If} ${FileExists} "$DESKTOP\${SHORTCUT_NAME}.lnk"
Delete "$DESKTOP\${SHORTCUT_NAME}.lnk"
${EndIf}
${If} ${FileExists} "$SMPROGRAMS\${SHORTCUT_NAME}.lnk"
Delete "$SMPROGRAMS\${SHORTCUT_NAME}.lnk"
${EndIf}
问题
升级 v0.2.1 后,新 exe 图标变了,但桌面 / 开始菜单的 .lnk 快捷方式图标还是旧的。
根因:electron-builder 默认 NSIS 升级流程只覆盖 .exe,不重建 .lnk;Windows 把 .lnk 的图标引用缓存在 shell icon cache (
IconCache.db),新 .ico 资源装好了 explorer 也不会主动重新读——除非 .lnk 被重建。修复
加
build/installer/custom-install.nsh自定义 NSIS 脚本,electron-builder 在nsis.include字段引用它。脚本逻辑:
$INSTDIR/${APP_EXECUTABLE_FILENAME}.exe仍存在 = 升级;不存在 = 全新安装$DESKTOP/AgentDock.lnk和$SMPROGRAMS/AgentDock.lnk验证
npx electron-vite build通过electron-builder --win --x64实际打包(需要本地 Windows + NSIS)