Skip to content

fix(installer): upgrade 时刷新桌面快捷方式图标#111

Closed
ACCSCI wants to merge 1 commit into
masterfrom
fix/desktop-shortcut-icon-cache
Closed

fix(installer): upgrade 时刷新桌面快捷方式图标#111
ACCSCI wants to merge 1 commit into
masterfrom
fix/desktop-shortcut-icon-cache

Conversation

@ACCSCI

@ACCSCI ACCSCI commented Jul 2, 2026

Copy link
Copy Markdown
Owner

问题

升级 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 字段引用它。

脚本逻辑:

  1. 检测升级路径:$INSTDIR/${APP_EXECUTABLE_FILENAME}.exe 仍存在 = 升级;不存在 = 全新安装
  2. 升级路径下,在 installer 重建 .lnk 之前先删 $DESKTOP/AgentDock.lnk$SMPROGRAMS/AgentDock.lnk
  3. electron-builder 接着按默认流程创建新 .lnk,explorer 读新 .exe 的图标资源

验证

  • npx electron-vite build 通过
  • electron-builder --win --x64 实际打包(需要本地 Windows + NSIS)
  • 在已装 v0.2.1 的机器上跑 v0.2.2 安装包,确认桌面图标刷新

此次改动仅影响 NSIS 升级流程。全新安装路径($INSTDIR 不存在 .exe)下脚本什么都不做,走默认行为。

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.
Copilot AI review requested due to automatic review settings July 2, 2026 06:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +29 to +35
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}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

在 NSIS 中,SetShellVarContext 是一个全局状态设置。在这里硬编码 SetShellVarContext current 会带来两个严重问题:

  1. 破坏“所有用户(Per-Machine / All Users)”安装模式:如果用户选择为“所有用户”安装,快捷方式会被创建在公共目录(All Users Desktop / Start Menu)下。硬编码 current 会导致升级时去尝试删除当前用户的对应目录,从而无法清除公共目录下的旧快捷方式,导致图标刷新失效。
  2. 污染后续安装步骤的上下文:由于 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}

@ACCSCI ACCSCI closed this Jul 2, 2026
@ACCSCI ACCSCI deleted the fix/desktop-shortcut-icon-cache branch July 2, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants